使用 bash 读取 CSV 文件中的特定列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27276874/
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 11:56:28  来源:igfitidea点击:

Reading a specific column in a CSV file with bash

bashshellunixcsvscp

提问by Gus

I'm really inexperienced with bash/shell scripts. I have a large volume of files that I need to SCP across servers. I have a list of the files I need to move in a CSV file with three columns. The second column is called file_name, and contains the path of the file.

我对 bash/shell 脚本真的很缺乏经验。我有大量文件需要跨服务器 SCP。我有一个我需要在包含三列的 CSV 文件中移动的文件列表。第二列称为file_name,包含文件的路径。

I need to read every single line of the file, but only process the second column. I found something like this on the internet:

我需要阅读文件的每一行,但只处理第二列。我在网上找到了类似的东西:

#!/bin/bash

csv_file=

while IFS=',' read -r file_name; do
    echo -e "File Name\t: $file_name"
done < $csv_file
IFS=$' \t\n'

But it just seems to output all 3 columns of the file. Any guidance would be greatly appreciated, I really don't know much about bash.

但它似乎只是输出文件的所有 3 列。任何指导将不胜感激,我真的不太了解bash。

Thanks!

谢谢!

回答by Barmar

You need to use a variable for each column of the file being read. The last variable will get all the remaining columns. So it should be:

您需要为正在读取的文件的每一列使用一个变量。最后一个变量将获得所有剩余的列。所以应该是:

while IFS=, read -r col1 file_name col3

回答by zzevannn

If the output being in another singe file is acceptable, his sounds like something well suited for awk, which is a program built for dealing with delimited input line by line.

如果输出在另一个单一文件中是可以接受的,那么他听起来很适合awk,这是一个为逐行处理分隔输入而构建的程序。

For example, the below may help with your problem, if I'm understanding it correctly:

例如,如果我理解正确,以下内容可能有助于解决您的问题:

awk -F',' '{print "File Name:\t"}' inputfile > outputfile

The -F','tells awk the field separator in the file is a comma.

-F','告诉awk在文件中的字段分隔符是一个逗号。

printis awk's command to print output, in this case the string "File Name:\t" followed by the second field in the file, which is denoted in awk by $iwhere i is the field number (NOT index, $0for awk is the entire line) you're interested in.

print是 awk 打印输出的命令,在这种情况下,字符串“文件名:\ t”后跟文件中的第二个字段,在 awk 中用$ii 表示字段编号(不是索引,$0因为 awk 是整行) 你感兴趣。

awkis a very powerful tool and this is just a very simple example of the complex processing it can do - you can read more about it's features and capabilities here: http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_toc.html

awk是一个非常强大的工具,这只是它可以执行的复杂处理的一个非常简单的示例 - 您可以在此处阅读有关其特性和功能的更多信息:http: //www.staff.science.uu.nl/~oostr102/docs /nawk/nawk_toc.html