2

I have the follow data set name a.txt with 9003 rows:

571 43544000424023503222
572 43504442020202303202
573 40340440323043033204
574 40303445340343505242
...       ...

16078 50200000023322000202
16079 33233500320452300252
16080 04200330233532050502
16081 30200400323435434202

I want to get a data file with values in the first column right aligned, the second column beginnig in the same field for all rows and one space between first and second column, this way:

  571 435440004240235032225
  572 435044420202023032022
  573 403404403230430332040
  574 403034453403435052423
...       ...

16078 502000000233220002021
16079 332335003204523002521
16080 042003302335320505023
16081 302004003234354342024

I am trying this code:

awk '{printf "%-5s \n", $1,$2}' a.txt |  # -5 indicate the maximum number of characters in the first column
expand > b.txt

The second column beginnig in the right place, however the first column was left aligned in the output, as below:

571   43544000424023503222
572   43504442020202303202
573   40340440323043033204
574   40303445340343505242
...       ...

16078 50200000023322000202
16079 33233500320452300252
16080 04200330233532050502
16081 30200400323435434202

Can you help me with this?

Thanks in advance.

6

You don't need the - flag (left-align), the default alignment is right-align:

awk '{printf "%5s %s\n",$1,$2}' a.txt
| improve this answer | |
3

You can use printf's string length modifier to format the string right justified. len is the length of the spacing which precedes the first field.

awk '{len=5-length($1); printf "%-*.*s %s %s\n", len,len," ",$1,$2}' a.txt
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.