need help to position words

Hi,

I would like to have script that can do below,

position words in the lines in a file, from below format

firstname, lastname B200000
.
.
.

into this format -

<5 spaces>first name, lastname<29 spaces from extreme left>B200000
.
.
.

Any idea how I can do this?
:b:

not sure what '29 spaces from the extreme left' means, but here's a start:

echo 'firstname,lastname B200000' | nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length(a[2]),a[1],a[2])}'

thank you for quick reply to help me.

 	 not sure what '29 spaces from the extreme left' means, but here's a start:

29 spaces from left and not from the end character of last name.

Now coming to back to script, I will try what you have suggested but I also need to do it for other lines in the files so some loop will be required further I guess.:b:

EDIT 1,

manual command is working fine, now we will need a loop to take care of all lines in the file, thank you!

EDIT 2,

I tried below,

while read LINE; do awk '{print $1 $2" "$3}'|awk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length(a[2]),a[1],a[2])}'; done < hmm.txt

where hmm.txt contains the "firstname, lastname Number" on every line.

Problem with the output I get from above script is that the "Number" gets positioned at 29 spaces after last name instead I want it from 29 spaces from the beginning of the line
Any help?

nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length(a[2]),a[1],a[2])}' myFile

:smiley: this is cool. but again I want Number to be 29 places from the beginning of that line and not from 29 spaces from last name. Sorry, if I wasn't clear in the first place. :b:

nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length($1)-6,a[1],a[2])}' myFile

Awsome. thanks so much. This worked fine. Now I have issues with some lines where there exists a terribly long first or last name but I 'll take responsibility to take care of that :smiley:

Thank you once again.

good day.

One idea would be to parse the file TWICE.
In the first pass figure out the maximum lengths for the firstname and the lastname fields.
In the second pass format the lines aligning the the last field (B20000 in you example) according to the firstname/lastname maximum lengths.

$.02

something along these lines:

nawk -F, '
   BEGIN {pad=5}
   FNR==NR {
      split($2,a," ")
      maxF=(maxF<length($1))?length($1):maxF
      maxL=(maxL<length(a[1]))?length(a[1]):maxL
      next
  } 
  {
      split($2,a, " ")
      printf("%-*c%s,%-s %*s\n", pad, " ", $1, a[1], maxF+maxL+pad+2-length($1)-length(a[1]),a[2])
  }' myFile myFile

given myFile:

firstname,lastname B200000
firstname1111,lastname1111 B200000
firstname22222,lastname22222 B200000
firstname333333,lastname333333 B200000

produces:

    firstname,lastname             B200000
    firstname1111,lastname1111     B200000
    firstname22222,lastname22222   B200000
    firstname333333,lastname333333 B200000

You are too good ! :b: Many thanks again....

God bless you!