使用bash从CSV文件中的特定行获取最后一个X字段
我正在尝试将其作为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将执行您的代码似乎正在尝试执行的操作(为给定的字符串
Record2_Data2
打印用户,仅在第二个字段中存在):但是我看不到这与您的问题主题有何关系
使用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):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.最好使用Bash数组,并在需要时加入CSV字符串:
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:
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 thereadarray
builtin, and you can manipulate it and the individual elements however you need.gnu
sed
解决方案,让file.csv
进行输出
说明
:
-n
关闭自动打印,表达式含义如下:1st替换全球“ 使用空字符串IE删除它们,第二个包含record2_data
替代的行(s
)所有内容3rd,
使用空字符串IE删除并打印(p
)(在GNU SED 4.2.2中测试) 。
GNU
sed
solution, letfile.csv
content bethen
gives output
Explanation:
-n
turns off automatic printing, expressions meaning is as follow: 1st substitute globally"
using empty string i.e. delete them, 2nd for line containingrecord2_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)
这可能对您有用(gnu sed):
删除所有记录,除了包含
record2_data
的记录。从第四个字段中删除双引号。
删除任何双引号字段。
This might work for you (GNU sed):
Delete all records except for that containing
record2_data
.Remove double quotes from the fourth field onward.
Remove any double quoted fields.