简体   繁体   English

如何使用bash打印列表中的所有项目,包括用逗号分隔的项目?

[英]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.

相关问题 如何匹配以逗号分隔的可变项列表 - How to match a variable list of items separated by commas 除括号中的逗号外的所有逗号的索引 - Index of all commas except those in parentheses 如何提取由&lt;&gt;界定并以逗号分隔的引号引起的元素列表-python,regex? - How do i extract a list of elements encased in quotation marks bounded by <> and delimited by commas - python, regex? 匹配以逗号分隔的列表中的组,忽略括号中的逗号 - Match groups in comma-delimited list, ignoring commas in brackets PHP preg_replace括号内的逗号除外 - PHP preg_replace all commas except those inside parentheses Bash逗号分隔列表提取最后一列 - Bash Comma Delimited List Extracting Last Column 用逗号分隔的整数的正则表达式 - Regex for integers delimited by commas 如何使用正则表达式捕获空格分隔的单词列表? - How to use regex to capture list of space delimited words? 如何制作正则表达式以匹配逗号分隔列表,列表中最多包含5个项目? - How can I craft a regular expression to match a comma delimited list with a maximum of 5 items in the list? Bash / Regex:当某些第一个字段以引号和逗号开头时替换 CSV 文件中的第二个字段 - Bash / Regex: Replacing the second field in a CSV file when some of the first fields start with quotes and commas within those
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM