Convert bounded file to fixed width in Linux

Using the tools available for bash how do I convert delimited data

foo|bbbaaarrr|bazz

      

for fixed width data

foo      bbbaaarrrbazz     EOL   

      

I tried using a column since I assumed I could figure out the width of the column, didn't work. I'm sure this is trivial with sed or awk, but I'm not familiar with them.

+3


source to share


1 answer


The following should work for you:

column -t -s '|' input_file_here

      

It converts the input file to table format. The input record separator is defined -s

. If you need something other than white space in the fields, use -o

to set the output separator. By default, the output separator has two spaces, so there will be two spaces between the pins.

Example:

$ cat input
hello|world|testing
a|b|c
another|test|line

$ column -t -s '|' input
hello    world  testing
a        b      c
another  test   line

      

http://man7.org/linux/man-pages/man1/column.1.html




Edit:

If each field requires a fixed length, you can use for that awk

. You will need to set the input delimiter for your file, but something like this should work:

$ awk -F '|' '{ for (i=1; i<=NF; i++) { printf("%-10s", $i); } print ""; }' input
hello     world     testing
a         b         c
another   test      line

      

Just change the width of the field specified in the instructions printf

.

+10


source







All Articles