Izy- Izy- - 3 months ago 10
Bash Question

Appending lines as comma seperated values in bash

Using a combination of commands, I extract out the following line from a text file (say file1.txt) and store it in another file, say opfile.txt

opfile.txt

1%
12 of 1G
200 of 2G


Now, say file1 values get updated and the next time I run my command, I get the values

2%
10 of 1G
100 of 1G


Now, I want the output to be appended as comma separated values on each line of my opfile.txt like so.

opfile.txt

1%,2%
12 of 1G,10 of 1G
200 of 2G,100 of 1G


How exactly do I append it?

To get the first output into opfile.txt, I use the command

grep "#0" TestOutput | cut -f 4,5,6 | tr '\t' "\n " > opfile.txt


And that what inputs the data into opfile.txt initially.

Now, to get the second bunch of data, I use this command

grep "#0" TestOutput | cut -f 4,5,6 | awk '{print ","$1}{print ","$2" "$3" "$4}{print ","$5" "$6" "$7}'


And that gives me the following

,7.1%
,230.9M of 1G
,700.3M of 2G


Simply using the >> to append it into opfile.txt is giving me the output on all different lines.

How do I get them on the same line?

Answer

You can use paste command for this:

paste -d, opfile.txt <(your_command)

1%,2%
12 of 1G,10 of 1G
200 of 2G,100 of 1G

Where your_command is your command producing second output.

Comments