[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Removeing N lines from a file



I am a little rusty, but I think a pipe with head and tail may do what you
want

head -n < file
will display/select the first n lines

tail -n <file
will display the last n lines

tail +n < file
will display from the the nth line onwards (or may be it is n+1th I told you
I am rusty :-%

For convenience, you can write a shell script (calling it scoop may be )
which takes two command line arguments - the line number from which you want
to scoop and the line number up to which you want to scoop

head -$1 > /tmp/x1
tail +$2 >/tmp/x2
cat /tmp/x1 /tmp/x2

These three lines can be made into a sheel script and the file you want to
do surgery can be redirected to it; the excised lines will be displayed on
screen and you can redirect that to a file.

If you want to select the lines in the middle, rather than discard from the
middle, the following one-liner will be a good starting point (same command
line args)

head -$2 |tail +$1

Just verify that you are not hit by the off by 1 error anywhere
I multiboot win 95 and RH 5.2 Linux; I am in the middle of replacing RH 5.2
with debian. I am now sending this from Win95; so I am not able to offer a
tested solution.

Hope this helps
P Asokan

P S: On re-reading, the note looked too 'teacherish'; pardon me if it was
more detailed than necessary


----- Original Message -----
From: Lance Hoffmeyer <lanceh@ibm.net>
To: Debian-user <debian-user@lists.debian.org>
Sent: Thursday, February 11, 1999 7:37 AM
Subject: Removeing N lines from a file


>How can I use gawk or some other program to remove a number of lines
>from a text file.  Initially, I only need to delete the top 10 lines
>from a file but it might be useful to know how to delete lines from any
>part of the file.  The top 10 lines from each of these files vary in
>what they may contain so I need to indiscriminately delete them.  I
>figured gawk is what I would need to use to perform this task but if
>someone ones of something else that might do this that would be fine
>too.
>
>Thanks
>
>Lance
>
>
>--
>Unsubscribe?  mail -s unsubscribe debian-user-request@lists.debian.org <
/dev/null
>
>


Reply to: