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

时间:2016-08-31 12:19:08

标签: shell csv text string-formatting

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 need to print in data in below format **

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

1 个答案:

答案 0 :(得分:1)

使用printf语句来处理字符串的格式化和放置。当然你需要根据你的要求修改它。你必须根据你的要求玩变量v和j的值。使用命令替换在echo语句中使用了awk语句。

 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