Problem with blanks, shifting the row when using awk

Hello,
I have files with fixed length fields.

12345  12345  12345671234567        10     1234567    
12345  12345  123456  1234567        10     1234567            

I want to take 1st, 3rd, 4th and 6th string.
Usually 3rd and 4th string are coming as it is on the first row /1234567 and 1234567/, then I can use

awk '{print $1,$3,$5}'

and it works. But the problem comes when the 3rd string is shorter, like it is on the second row. It is being shifted with one column.
I was reading and trying with nawk and substr, but I am not so good with Unix.
Could you please help.
Thanks in advance!

Try

awk 'NF==5{$3=substr($3,1,7) " " substr($3,8); $0=$0} {print $1,$3,$4, $6}' file
12345 1234567 1234567 1234567
12345 123456 1234567 1234567
1 Like

Hello apenkov,

Following may help you in same.

 awk '{print $1 OFS $3 OFS $5}' FIELDWIDTHS="5 7 17 10 12" Input_file
 

Output will be as follows.

12345   12345671234567      1234567
12345   123456  1234567      1234567
 

Thanks,
R. Singh

1 Like

Thank you all!