[英]How to use bash to print all items in a list, including those delimited by commas?
Okay, so I have file.txt, which has two tab-delimited lists: 好的,我有file.txt,其中包含两个制表符分隔的列表:
A sheep,cow
B pig
C horse
D goat,duck,llama
I would like to print all of the items in the second list in a new file, file2.txt, even the items delimited by commas: 我想将第二个列表中的所有项目打印在一个新文件file2.txt中,甚至包括以逗号分隔的项目:
sheep
cow
pig
horse
goat
duck
llama
I tried doing this: 我尝试这样做:
cat file.txt | awk 'NR>1 {for (i=2; i<=NF; i++) if ($i !=",") print $i}' | sort -d | uniq > file2.txt
...but it just doesn't register that items delimited by , are different items. ...但是它只是没有注册以分隔的项目是不同的项目。
Any ideas? 有任何想法吗? Should I remove the first column, make everything after a comma appear in a new column, and then print all the entries in all the columns? 我应该删除第一列,使逗号后的所有内容都出现在新列中,然后打印所有列中的所有条目吗?
I'd take the easy way out: 我会采取简单的方法:
$ cut -f 2 file.txt | tr ',' '\n'
sheep
cow
pig
horse
goat
duck
llama
It gets the second column, and replaces commas with linefeeds. 它获得第二列,并用换行符替换逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.