Computing.Net > Forums > Unix > Fixed width using spaces

Fixed width using spaces

Reply to Message Icon

Original Message
Name: kcworker
Date: March 21, 2005 at 18:18:00 Pacific
Subject: Fixed width using spaces
OS: Unix
Comment:

I am trying to extract positions 6 and 7 from the following file (These two fields will be merged with another file):

0544,0544,ABC,TW001,AA,9,
0376,0311,ABC,TW001,BA,9,20050409

A majority of the time position 7 will not contain a value. I am having a hard time setting position 7 to spaces when the field does not contain a value. I have tried the following:

grep ${num} aud | awk -F, '{printf "%2s%8.8s", $6, $7}'

but it only pads one space. Does anyone have any suggestions?


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: March 21, 2005 at 23:11:01 Pacific
Reply: (edit)

since in your example data, the 7th field is undefined, this might work:

awk -F, ' { if($7 == "")
printf "%2s%8.8s\n", $6, " "
else
printf "%2s%8.8s\n", $6, $7 } ' datafile


Report Offensive Follow Up For Removal

Response Number 2
Name: Jim Boothe
Date: March 22, 2005 at 06:54:59 Pacific
Reply: (edit)

Actually, your code works as expected for me. Whether field 7 is null or not, my output is 10 characters. For your first example line, I get:

b9bbbbbbbb

where b=space. That's a 2-character right-justified field followed by an 8-character right-justified field.

If you redirect this output to a file, you will see that it has a length of 10 characters (2 + 8) with no line terminator.


Report Offensive Follow Up For Removal

Response Number 3
Name: kcworker
Date: March 24, 2005 at 17:33:17 Pacific
Reply: (edit)

Thanks for both of your help. I actually ended up checking if the value was = "" or " " in a loop and using echo to add the spaces. For some reason I stiil was not able to get the field justified with spaces


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Fixed width using spaces

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge