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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn Shell Scripting Question. 2

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
Hi. I'm writing a Korn Shell script, and it does several things. However, I am unable to make it do one thing. I want it to read in a fixed-width file, and output a fixed width file. I want it to read each record of the input file, and determine whether or not bytes 10-20 are blank. If they are, I want a 'N' written to the output file. I they are populated, I want a 'P' written t the output file. So, here's an example:

Input File:

1234567890 MinnesotaMN5555555
0987654321 Nebraska NE6666666
1029384756MinneapolisMinnesotaMN5555555
5674839201Portland Oregon OR1111111
8943027615 NomeAlaska AK2222222
8273645019 Iowa IA4444444

In these cases, I want the output file to read:

1234567890NMinnesotaMN5555555
0987654321NNebraska NE6666666
1029384756PMinnesotaMN5555555
5674839201POregon OR1111111
8943027615PAlaska AK2222222
8273645019NIowaIA4444444

Where bytes 10-20 are replaced by either an 'N' or a 'P'.
Does that make sense to anyone? My logic is solid, I think, I just need to translate it to code. My logic:

While read cur_rec
if (bytes 10-20 ARE populated)
write out cur_rec, insert a 'P'
else
write out cur_rec, insert an 'N'

Anyway, if someone could please help me out here, I would greatly appreciate it. Thanks!


 
given your sample input, you have bytes 11->21 empty (not 10->20).

Something like that with nawk:

nawk -f mwes.awk myFile.txt

#------------------------- mwes.awk
BEGIN {
BYTEbegin=11
BYTEend=21

MARKempty="N"
MARKnonempty="P"

for(i=1; i<= BYTEend - BYTEbegin; i++)
empty = empty &quot; &quot;;
}
match(substr($0, BYTEbegin, BYTEend - BYTEbegin), empty) {
#printf(&quot;found EMPTY->[%d]\n&quot;, NR);
marker=MARKempty;
}

{
printf(&quot;%s%s%s\n&quot;, substr($0, 1, BYTEbegin - 1), marker, substr($0, BYTEend+1
));
marker=MARKnonempty;

}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Wow! I tried it, and it worked PERFECTLY! Thank you so much! One thing: Is there any way I can include the mwes.awk contents in my actual Korn shell script, so I don't have to have this &quot;.awk&quot; file out on my Unix system? You know, so this whole process can be self-contained? Thanks again for the spot-on info.
 
Another solution using sed :
[tt]
sed -e 's/^\(.\{10\}\) \{11\}/\1N/
t
s/^\(.\{10\}\).\{11\}/\1P/' myFile.txt
[/tt]

Jean Pierre.
 
#!/bin/ksh

# stuff here

# more stuff here

nawk '
BEGIN {
BYTEbegin=11
BYTEend=21

MARKempty=&quot;N&quot;
MARKnonempty=&quot;P&quot;

for(i=1; i<= BYTEend - BYTEbegin; i++)
empty = empty &quot; &quot;;
}
match(substr($0, BYTEbegin, BYTEend - BYTEbegin), empty) {
#printf(&quot;found EMPTY->[%d]\n&quot;, NR);
marker=MARKempty;
}

{
printf(&quot;%s%s%s\n&quot;, substr($0, 1, BYTEbegin - 1), marker, substr($0, BYTEend+1
));
marker=MARKnonempty;

}' myFile.txt

# EVEN more shell stuff here

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Perfect... That's outstanding. I'll donate to Tek-Tips in both your names. Invaluable information being passed along here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top