如何使用bash自动化linux cut命令

时间:2018-07-22 15:16:24

标签: linux bash awk

假设我有一个包含4列的文本文件,我必须剪切每一列并将其保存到另一个文本文件中。我可以通过以下Linux命令手动执行此操作,但是我想使用bash脚本自动执行此过程。有人可以帮我吗

cut  textfile.txt | cut -d ":" -f1 > output1.txt

cut  textfile.txt | cut -d ":" -f2 > output2.txt

cut  textfile.txt | cut -d ":" -f3 > output3.txt

cut  textfile.txt | cut -d ":" -f4 > output4.txt

Textfile.txt

“ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4” “ text1”:“ text2”:“ text3”:“ text4”

col 1 should store in output1.txt "text1" "text1" "text1" "text1" "text1" "text1" "text1" "text1" "text1" col 2 should store in output2.txt "text2" "text2" "text2" "text2" "text2" "text2" "text2" "text2" "text2" col 3 should store in output3.txt "text3" "text3" "text3" "text3" "text3" "text3" "text3" "text3" "text3" col 4 should store in output4.txt "text4" "text4" "text4" "text4" "text4" "text4" "text4" "text4" "text4"

1 个答案:

答案 0 :(得分:0)

假设您问题中的每个grep textfile.txt应该是cat textfile.txt(如果是Google UUOC,则是这样),您所需要做的就是:

awk -F':' '{for (i=1; i<=NF; i++) print $i > ("output"i".txt")}' textfile.txt