user3688785 user3688785 - 3 months ago 11
Bash Question

Print file output row wise in new CSV file using Shell Script

i have file

> cat abc.txt
> Data is here
ASDF 1234
GHJKL 5678
!@#$% 0011


I am using command "(echo "Random data is:,"; cat abc.txt) | xargs > red1.csv"
I am getting red1.csv file output as

Random data is: Data is here ASDF 1234 GHJKL 5678 !@#$% 0011

I need to print in data in below format

**
Random data is: Data is here
ASDF 1234
GHJKL 5678
!@#$% 0011

Answer

Used printf statement to play around with formatting and placement of strings. of course you need to modify it as per your requirement. you would have to play around value of variables v and j as per your requirement. Awk statement was used inside echo statement using command substitution.

 echo "Random data is : $(awk -v f="%35s\n" -v j="%20s\n" 'NR>1{printf f,$0} NR==1 {printf j,$0}' abc.txt) "
Random data is :         Data is here
                          ASDF 1234
                         GHJKL 5678
                         !@#$% 0011