regarding about printing line number

Hello,
I am testing some data to get line number at cursor position 9 and found some problem, the code is below.Assume we got 3 attribute. At second attribute, there are some data(eg.A41/A6) missing like at the fourth and six line

11006                        A41              1888
11006                        A6               2456
11006                        A43              4532
11006                                         3333
11006                        A33              5412
11006                                         8987
11006                        A34              4999

if there is no data missing at second attribute, when i use the code
Code:

awk '{ printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3,$4); }' myfile

it is working perfect and printing all line number at cursor postion 9.

But if there is missing data at second or third attribute,line number print ok ,but the output data mess up like that

11006  1                     A41              1888
11006  2                     A6               2456
11006  3                     A43              4532
11006  4                     3333
11006  5                     A33              5412
11006  6                     8987
11006  7                     A34              4999

Any idea how to fix this problem?

Many thanks

I hope this is enough :

awk -F"!" '{  printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3,$4); }' input_file.txt

( i mean take other than space as a delimiter )

But Line is the problem in this case to print

Try this(Assuming 2nd column in original file satrts with A):

awk '{ if($2 !~ /^A/) { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' filename

Looks your input is fixed width...

awk '{
FIRST=substr($0,1,7);
SECOND=substr($0,30,17);
THIRD=substr($0,47,4);
$1=FIRST
$2=SECOND
$3=THIRD
printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3);
}' input

Hello,
Many thanks for your reply, i have test both of them
for this code,

awk -F"!" '{  printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3,$4); }' input_file.txt

i got this error "bash: !: event not found"

this code is working fine

awk '{ if($2 !~ /^A/) { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' filename

But dennis, second attribute can start with any character, A-Z and 0-9
so tried /^[A-Z]/ and ok , but when i write /^[A-Z][0-9]/ ,it is not working.

Any idea,

Many thanks

Hey, you can simply do this...

awk '{ if($3=="") { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' input

Try:

It will be difficult if the number of columns in the record is not fixed. However, assuming that the last column will always have a value, the below one should work.

awk '{ if (NF==2) { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' filname

Many thanks for all replies, that code solved the problems

awk '{
FIRST=substr($0,1,7);
SECOND=substr($0,30,17);
THIRD=substr($0,47,4);
$1=FIRST
$2=SECOND
$3=THIRD
printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3);
}' input

can you explain what is that $0 for in this
substr($0,1,7);

Many thanks