Pooja
Pooja

Reputation: 165

how to insert data into file with ',' as a delimiter

cat try_1.txt | grep 'aa|' | cut -d '|' -f 2 >> abc.txt

Using the above I am selecting few fiends and putting it into abc.txt

Data of abc.txt is

aa
bb
ccc
dd

I want data inserted into abc.txt like : expected output :

aa,bb,ccc,dd

Upvotes: 0

Views: 1773

Answers (2)

Ed Morton
Ed Morton

Reputation: 203807

No need for multiple tools and pipes, just use awk":

$ cat try_1.txt
aa|aa
aa|bb
aa|ccc
aa|dd

$ awk -F'|' '/aa\|/{printf "%s%s",s,$2;s=","} END{print ""}' try_1.txt
aa,bb,ccc,dd

Upvotes: 0

William Pursell
William Pursell

Reputation: 212346

paste -s -d, - < try_1.txt

does exactly what you seem to want, but I do not at all understand why you have the grep 'aa|', so I probably don't know what you want.

Ah, now I think I understand what you want:

awk '/aa\|/ {print $2}' FS=\| try_1.txt | paste -s -d, - >> abc.txt

Upvotes: 1

Related Questions