Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines.

I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it.

There are two lines as one Header and one trailer which should remain same.

Can somebody help on how to achieve by ksh script or any other way

It you mean character "W" at position 356 try:

awk 'NR<3; {l=$0; if (substr(l,356,1)=="W") print } END {print l}' infile > outfile
1 Like

Great it worked.

But somehow the top most line was not included by this command. Any reasons why.

For ex:

Topmost is Header line
Second was the body line but it remains there even if at 356 character it is "1" instead of "W"

Can you please help:)

In your 1st message in this thread, you said:

rdrtx1 and I both understood this to mean that there were two header ilnes and one trailer line. If there is just one header line, change NR<3; in rdtx1's script to NR<2; or NR==1; .

If you are trying this on a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , on nawk instead of awk .

If $0 is still defined in the END section (which is not necessarily true for all awk implementations), you can try

awk 'FNR==1; substr($0,356,1)=="W"; END{print}' file

This should with all awk versions

awk '(NR==1 || substr($0,356,1)=="W"); {l=$0} END{print l}' file