Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nawk script not processing all lines in a file

Status
Not open for further replies.

l33tgaijin

Programmer
Mar 5, 2002
3
US
Hello,
Here's a script I'm using to left-pad columns in a tilde-delimited file:

nawk -F~ 'length($1)<15{sub($1,sprintf(&quot;%015u&quot;,$1),$0)}
length($3)<15{sub(FS$3FS,sprintf(&quot;~%015u~&quot;,$3),$0);print >FILENAME}' <Name of file here>

so the file that I'm testing this on is 128 lines, but after running this script, only 86 lines are in the file. I assume that I have some qualification that's preventing the last ~46 lines from being processed.... Any ideas?
 
btw, here are a couple of sample lines from the file being processed:

1~&quot;Presentation - Browser &quot;~3~&quot;01525 &quot;
2~&quot;Presentation - Proprietary &quot;~3~&quot;01525 &quot;
3~&quot;Presentation - Wireless &quot;~3~&quot;01525 &quot;

The only two columns I'm concerned with here are $1 and $3. After being processed they should each be 15 characters in length with the column value at the end of the string.
 
Your program is not printing anything after padding field 1. Is this correct? Also, if you have any lines in the file where field 1 or 3 are >= 15 characters in length, your program will ignore them. CaKiwi
 
Hi, thanks for the reply. I just now figured it out. Actually, it was processing the first 86 lines just fine. I'm using length($column)<16(I forgot that strings in awk aren't quite like strings in C) to make sure that these fields don't exceed 15 characters (if they do, someone else has a big problem). Here's the script now (I can't believe I didn't see this at first...):

nawk -F~ 'length($1)<16{$1=sprintf(&quot;%015u&quot;,$1)}
length($3)<16{$3=sprintf(&quot;%015u&quot;,$3);
print >FILENAME}' <Name of file here>

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top