Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column formatting unix

So the issue I am having is to format the column to give it more space so i am unsure how to give column mode more spaces so everything lines up. now my awk statement just formats it so that each field is separated so that people without middle names does not screw it up lol.

 $ cat file

1 Jackson, Bob D. C0001 book pizza apple 4.00 123as 
2 Filer, Jack C0002 happy apple hat 4.00 124ab 
3 Metro, Jim K. C0003 kindle pizza grape 4.00 125ac

That's the original file so I used the awk statement below to field separate each column so that the middle name wouldn't try to group another column into it

$ echo "#  ""Name             ""LS     ""mode       ""food   ""item   ""num   ""list   "
$ echo "-  ""---------------  ""-----  ""---------  ""-------""-----  ""----  ""-----  "
$ awk -v OFS='~' '{$2=$2" "$3;$3=""} 
        NF==10{$2=$2" "$4;$4=""} 
         NF==9{$3=$3 OFS ""}1' file | column -ts~
#  Name             LS     mode       food   item   num   list   
-  ---------------  -----  ---------  -----  -----  ----  -----  
1  Jackson, Bob D.  C0001  book    pizza  apple  4.00  123as
2  Filer, Jack      C0002  happy   apple  hat    4.00  124ab
3  Metro, Jim K.    C0003  kindle  pizza  grape  4.00  125ac

So what i want this to come out as is. If you have a better way then just echo those items in feel free to edit but if its fine that why it does not really matter i just need the final output to look like this thanks!

#  Name             LS     mode       food   item   num   list   
-  ---------------  -----  ---------  -----  -----  ----  -----  
1  Jackson, Bob D.  C0001  book       pizza  apple  4.00  123as
2  Filer, Jack      C0002  happy      apple  hat    4.00  124ab
3  Metro, Jim K.    C0003  kindle     pizza  grape  4.00  125ac
like image 816
NewEffect Avatar asked Mar 02 '26 21:03

NewEffect


1 Answers

awk to the rescue!

I'll solve the name problem for you in a different way and show how to use printf for formatting, you can add the headers and rest of fields looking up the answer above.

    $ awk 'NF==9{for(i=NF;i>=4;i--) $(i+1)=$i; $4=""}
                {printf "%d %-15s %5s\n", $1,$2" "$3" "$4, $5}' file

1 Jackson, Bob D. C0001
2 Filer, Jack     C0002
3 Metro, Jim K.   C0003
like image 188
karakfa Avatar answered Mar 04 '26 18:03

karakfa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!