khd
khd

Reputation: 1666

How to automate linux cut command using bash

Let suppose I have a text file having 4 column and I have to cut every single column and save it into another text file. Manually I can do this by the following Linux command but I want to automate this process using bash script. Can someone help me with this

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"

Upvotes: 0

Views: 120

Answers (1)

Ed Morton
Ed Morton

Reputation: 203625

Assuming each grep textfile.txt in your question was intended to be cat textfile.txt (and if so google UUOC), all you need is:

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

Upvotes: 1

Related Questions