Tom Bell
Tom Bell

Reputation: 11

delete nth of lines using awk

At a set interval of records, I need to remove 4 lines/records. Can this be done with awk or sed.

I have a file that has a form feed character every 70th record. This is causing a blank line to be printed in my output.

I want to use awk or sed do delete the four (4) lines before the form feed so my page is now 66 lines long.

That should eliminate my blank pages.

The file I am passing is several thousand records in a spool file and I was hoping for an easy solution. I've tried

sed -n -e '65~68p; 

but I don't think I have that available on my unix box because I get an error when I try running that command.

Upvotes: 1

Views: 938

Answers (3)

alinsoar
alinsoar

Reputation: 15783

sed 'H;g;s:[^\n]::g;/.\{64\}/{N;N;N;s:.::g; x;s:\n::p;d};${x;s:\n::;p};d' FILE

This line will remove the next 3 lines after the line 65, 65*2, ..., 65*K .

If you want to remove more than 3 lines, add more N;N;N;.

Upvotes: 0

piokuc
piokuc

Reputation: 26164

pbhd gave a nice solution before I managed to post mine, but here it is in case you'd like to do it using python:

    import sys
    s = sys.stdin.read()
    for page in s.split('\f'):
        lines = page.splitlines()
        print '\n'.join(lines[:-4]) + '\f'

Upvotes: 0

pbhd
pbhd

Reputation: 4467

awk '{if ((NR%70) <65) print $0;}' inputfile

should do the job.

Upvotes: 2

Related Questions