Using sed (or awk or perl) to delete rows in a file

I have a Unix file with 200,000 records, and need to remove all records from the file that have the character �I' in position 68 (68 bytes from the left). I have searched for similar problems and it appears that it would be possible with sed, awk or perl but I do not know enough about any of these commands to come up with the syntax needed to do what I need to. Any help would be appreciated.

Try:

perl -ne 'print if ! /^.{67}I/' infile > outfile
awk -v FS="" -v OFS="" '$68 != "I"' < infile > outfile

You could also do in-place editing with Perl:

perl -pi.bak -e 's/^.*\n$// if substr($_,67,1) eq "I"' your_file

The ".bak" extension after the "i" switch instructs perl to create a backup file called your_file.bak before it starts editing.

tyler_durden

How about sed...

sed '/^.\{67\}I/d' file

That needs to be anchored to the beginning of the line or it could match an "I" that is not the 68th character.

Regards,
Alister

1 Like

I like choices. I will give these a try. I appreciate the help.

If you have Ruby(1.9.1+)

$ ruby -ne 'print if $_[67]!="I"' file