使用bash从CSV文件中的特定行获取最后一个X字段

发布于 2025-02-12 14:16:13 字数 752 浏览 3 评论 0原文

我正在尝试将其作为bash变量列表,这些列表是我的CSV文件中的用户。问题是用户数量是随机的,可以是1-5。

示例CSV文件:

"record1_data1","record1_data2","record1_data3","user1","user2"
"record2_data1","record2_data2","record2_data3","user1","user2","user3","user4"
"record3_data1","record3_data2","record3_data3","user1"

我想得到类似的

list_of_users="cat file.csv | grep "record2_data2" | <something> "
echo $list_of_users
user1,user2,user3,user4

尝试:

cat file.csv | grep "record2_data2" |  awk -F, -v OFS=',' '{print $4,$5,$6,$7,$8 }' | sed 's/"//g'
    

我的结果是:

user2,user3,user4,,

问题: 从我的结果结束时,如何删除所有“”?有时只是一个,但有时可以是user1 ,,, 我可以更好地做吗?用户总是在文件中的第三列之后开始。

I'm trying to get as bash variable list of users which are in my csv file. Problem is that number of users is random and can be from 1-5.

Example CSV file:

"record1_data1","record1_data2","record1_data3","user1","user2"
"record2_data1","record2_data2","record2_data3","user1","user2","user3","user4"
"record3_data1","record3_data2","record3_data3","user1"

I would like to get something like

list_of_users="cat file.csv | grep "record2_data2" | <something> "
echo $list_of_users
user1,user2,user3,user4

I'm trying this:

cat file.csv | grep "record2_data2" |  awk -F, -v OFS=',' '{print $4,$5,$6,$7,$8 }' | sed 's/"//g'
    

My result is:

user2,user3,user4,,

Question:
How to remove all "," from the end of my result? Sometimes it is just one but sometimes can be user1,,,,
Can I do it in better way? Users always starts after 3rd column in my file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我只土不豪 2025-02-19 14:16:14

这将执行您的代码似乎正在尝试执行的操作(为给定的字符串Record2_Data2打印用户,仅在第二个字段中存在):

$ awk -F',' '{gsub(/"/,"")} $2=="record2_data2"{sub(/([^,]*,){3}/,""); print}' file.csv
user1,user2,user3,user4

但是我看不到这与您的问题主题有何关系使用bash从CSV文件中获取最后一个X记录。

This will do what your code seems to be trying to do (print the users for a given string record2_data2 which only exists in the 2nd field):

$ awk -F',' '{gsub(/"/,"")} $2=="record2_data2"{sub(/([^,]*,){3}/,""); print}' file.csv
user1,user2,user3,user4

but I don't see how that's related to your question subject of Getting last X records from CSV file using bash so idk if it's what you really want or not.

懷念過去 2025-02-19 14:16:14

最好使用Bash数组,并在需要时加入CSV字符串:

#!/usr/bin/env bash
readarray -t listofusers < <(cut -d, -f4- file.csv | tr -d '"' | tr ',' 

cut -d,-f4- file.csv | tr -d'''| tr',$'\ n'| sort -u 是重要的位 - 它首先仅打印出CSV输入文件的第四和以下字段进入新线,然后对产生的用户名进行分类,然后删除使用 readarray 内置的输出的重复项。但是,您需要的个别要素。

\n' | sort -u)) IFS=, printf "%s\n" "${listofusers[*]}"

cut -d,-f4- file.csv | tr -d'''| tr',$'\ n'| sort -u是重要的位 - 它首先仅打印出CSV输入文件的第四和以下字段进入新线,然后对产生的用户名进行分类,然后删除使用readarray内置的输出的重复项。但是,您需要的个别要素。

Better to use a bash array, and join it into a CSV string when needed:

#!/usr/bin/env bash
readarray -t listofusers < <(cut -d, -f4- file.csv | tr -d '"' | tr ',' 

cut -d, -f4- file.csv | tr -d '"' | tr ',' $'\n' | sort -u is the important bit - it first only prints out the fourth and following fields of the CSV input file, removes quotes, turns commas into newlines, and then sorts the resulting usernames, removing duplicates. That output is then read into an array with the readarray builtin, and you can manipulate it and the individual elements however you need.

\n' | sort -u)) IFS=, printf "%s\n" "${listofusers[*]}"

cut -d, -f4- file.csv | tr -d '"' | tr ',' $'\n' | sort -u is the important bit - it first only prints out the fourth and following fields of the CSV input file, removes quotes, turns commas into newlines, and then sorts the resulting usernames, removing duplicates. That output is then read into an array with the readarray builtin, and you can manipulate it and the individual elements however you need.

So尛奶瓶 2025-02-19 14:16:14

gnu sed解决方案,让file.csv进行

"record1_data1","record1_data2","record1_data3","user1","user2"
"record2_data1","record2_data2","record2_data3","user1","user2","user3","user4"
"record3_data1","record3_data2","record3_data3","user1"

输出

sed -n -e 's/"//g' -e '/record2_data/ s/[^,]*,[^,]*,[^,]*,// p' file.csv

说明

user1,user2,user3,user4

-n关闭自动打印,表达式含义如下:1st替换全球“ 使用空字符串IE删除它们,第二个包含record2_data替代的行(s)所有内容3rd 使用空字符串IE删除并打印(p

(在GNU SED 4.2.2中测试) 。

GNU sed solution, let file.csv content be

"record1_data1","record1_data2","record1_data3","user1","user2"
"record2_data1","record2_data2","record2_data3","user1","user2","user3","user4"
"record3_data1","record3_data2","record3_data3","user1"

then

sed -n -e 's/"//g' -e '/record2_data/ s/[^,]*,[^,]*,[^,]*,// p' file.csv

gives output

user1,user2,user3,user4

Explanation: -n turns off automatic printing, expressions meaning is as follow: 1st substitute globally " using empty string i.e. delete them, 2nd for line containing record2_data substitute (s) everything up to and including 3rd , with empty string i.e. delete it and print (p) such changed line.

(tested in GNU sed 4.2.2)

深爱成瘾 2025-02-19 14:16:14
awk -F',' '
  /record2_data2/{
     for(i=4;i<=NF;i++) o=sprintf("%s%s,",o,$i); 
     gsub(/"|,$/,"",o); 
     print o
}' file.csv

user1,user2,user3,user4
awk -F',' '
  /record2_data2/{
     for(i=4;i<=NF;i++) o=sprintf("%s%s,",o,$i); 
     gsub(/"|,$/,"",o); 
     print o
}' file.csv

user1,user2,user3,user4
孤千羽 2025-02-19 14:16:14

这可能对您有用(gnu sed):

sed -E '/record2_data/!d;s/"([^"]*)"(,)?/\1\2/4g;s///g' file

删除所有记录,除了包含record2_data的记录。

从第四个字段中删除双引号。

删除任何双引号字段。

This might work for you (GNU sed):

sed -E '/record2_data/!d;s/"([^"]*)"(,)?/\1\2/4g;s///g' file

Delete all records except for that containing record2_data.

Remove double quotes from the fourth field onward.

Remove any double quoted fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文