在 bash 中读取 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12783324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:29:11  来源:igfitidea点击:

Read a CSV file in bash

linuxbashshellcsv

提问by Sriharsha Kalluru

I have a requirement to read the CSV file in shell, Well I am ok with the CSV file having single line in a cell. But if we have multiple lines in cell of CSV file then I am unable to delimit the the CSV file.

我需要在 shell 中读取 CSV 文件,好吧,我对单元格中只有一行的 CSV 文件没问题。但是,如果我们在 CSV 文件的单元格中有多行,那么我将无法分隔 CSV 文件。

Filename            Lines
/etc/hosts          example.test.com
                    example2.test.com
/etc/resolv.conf    nameserver dns.test.com
                    search test.com

I will take input from the user in a CSV file and have to add the given lines to the mentioned files. Here there are multiple lines in each cell of a CSV file and If I try to cat the file it is giving in a different order.

我将在 CSV 文件中从用户那里获取输入,并且必须将给定的行添加到提到的文件中。这里有一个 CSV 文件的每个单元格中的多行,如果我尝试对文件进行分类,它会以不同的顺序给出。

[user2@mon ~]$ cat test2.csv
"Filename","Lines"
"/etc/hosts","example.test.com"
,"example2.test.com"
"/etc/resolv.conf","nameserver dns.test.com"
,"search test.com"

Is there any way we can read the multiple lines from that file and number of lines is not same in all the time.

有什么方法可以从该文件中读取多行并且行数一直不相同。

回答by Jonathan Leffler

This might be what you're after:

这可能是您所追求的:

awk -F, '{ sub(/^"/, "", ); sub(/"$/, "", );
           sub(/^"/, "", ); sub(/"$/, "", );
           printf "%-20s  %s\n", , ;
         }'

It may well be possible to compress the substitute operations if you spend more time manual bashing. This is fragile as a solution (most solutions not using code specialized for dealing with CSV format are fragile); it fails horribly if a comma appears inside any of the quote-enclosed fields.

如果您花更多时间进行手动攻击,则很有可能压缩替代操作。这是一个脆弱的解决方案(大多数不使用专门用于处理 CSV 格式的代码的解决方案都是脆弱的);如果逗号出现在任何引号括起来的字段中,它就会失败。

Applied to your data, it yields:

应用于您的数据,它会产生:

Filename              Lines
/etc/hosts            example.test.com
                      example2.test.com
/etc/resolv.conf      nameserver dns.test.com
                      search test.com

Other possible tools to manipulate CSV format data reliably include:

其他可靠地处理 CSV 格式数据的可能工具包括:

If this is not what you are looking for, please clarify the question.

如果这不是您要查找的内容,请澄清问题。

回答by William Pursell

Assuming your input is as basic as your example, you might be able to get away with simply doing:

假设您的输入与您的示例一样基本,您可能只需执行以下操作即可:

sed 's/^,/ ,/' test2.csv | tr -d \" | column -s, -t