简体   繁体   English

如何使用shell命令读取.csv文件?

[英]How to read a .csv file with shell command?

I have a .csv file which I need to extract values from. 我有一个.csv文件,需要从中提取值。 It is formatted like this : 格式如下:

First line of the file (no data) 文件的第一行(无数据)

1;Jack;Daniels;Madrid;484016; 1;杰克;丹尼尔斯;马德里; 484016;

2;Alice;Morgan;London;564127; 2;爱丽丝;摩根;伦敦; 564127;

etc... 等等...

I would need a shell command that read all lines of a specific column within a .csv, compare each with a string and return a value whenever it finds a matching line. 我需要一个shell命令,该命令读取.csv中特定列的所有行,将每个行与一个字符串进行比较,并在找到匹配行时返回一个值。 In Java i would define it something like : 在Java中,我将其定义为:

> boolean findMatchInCSV(String valueToFind, int colNumber, String
> colSeparator)

The separator between columns may indeed change that is why I would like a something quite generic if possible :) 列之间的分隔符实际上可能会发生变化,这就是为什么我想要一个比较通用的东西的原因:)

But I need it as a shell command , is that possible ? 但是我需要它作为shell命令 ,这可能吗?

Thanks 谢谢

grep "my_string" file |awk -F ";" '{print $5}'

or 要么

awk -F ";" '/my_string/ {print $5}' file

For 2nd column: 对于第二列:

awk -F ";" '$2 ~ /my_string/ {print $5}' file

For exact matching: 对于完全匹配:

awk -F ";" '$2 == "my_string" {print $5}' file

I assume you're looking for something like 我认为您正在寻找类似的东西

FILE=data.csv
VALUE="$1"
COLNUM=$2
IFS="$3"

while read -r -a myArray
do
    if "$myArray[$COLNUM]"=="$VALUE"; then
        exit 0
    fi
done < tail -n +2 $FILE

exit 1

I would need a shell command that read all lines 我需要一个读取所有行的shell命令

cat 1.csv                         # read the file

of a specific column within a .csv .csv中的特定列

cat 1.csv | cut -f5 -d';'         # keep only the field #5 (use ';' as separator)

compare each with a string 比较每个字符串

# keep only the row where the value of the field is exactly 'foo'
cat 1.csv | cut -f5 -d';' | grep '^foo$'

return a value whenever it finds a matching line. 找到匹配行时返回一个值。

This last one request is unclear. 最后一个请求不清楚。

The code above displays the searched string ( foo ) once for each row where it is the value of column #5 (start counting from 1). 上面的代码为每行显示一次搜索的字符串( foo ),它是列#5的值(从1开始计数)。 The columns are separated by ; 列之间用;隔开; .

Unfortunately, it doesn't handle quoted strings. 不幸的是,它不处理带引号的字符串。 If the value in any field contains the separator ( ; ), the CSV format allows enclosing the field value into double quotes ( " ) to prevent the separator character be interpreted as a separator (forcing its literal value). 如果任何字段中的值都包含分隔符( ; ),则CSV格式允许将字段值括在双引号( " )中,以防止将分隔符字符解释为分隔符(强制使用其文字值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM