[英]How to use bash to print all items in a list, including those delimited by commas?
好的,我有file.txt,其中包含兩個制表符分隔的列表:
A sheep,cow
B pig
C horse
D goat,duck,llama
我想將第二個列表中的所有項目打印在一個新文件file2.txt中,甚至包括以逗號分隔的項目:
sheep
cow
pig
horse
goat
duck
llama
我嘗試這樣做:
cat file.txt | awk 'NR>1 {for (i=2; i<=NF; i++) if ($i !=",") print $i}' | sort -d | uniq > file2.txt
...但是它只是沒有注冊以分隔的項目是不同的項目。
有任何想法嗎? 我應該刪除第一列,使逗號后的所有內容都出現在新列中,然后打印所有列中的所有條目嗎?
我會采取簡單的方法:
$ cut -f 2 file.txt | tr ',' '\n'
sheep
cow
pig
horse
goat
duck
llama
它獲得第二列,並用換行符替換逗號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.