Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-20-2007, 09:48 AM   #1
LQ Newbie
Registered: Sep 2007
Posts: 1

Rep: Reputation: 0
Delete first and last lines of a file

Hi! I need to delete the first and last lines of a bunch of files. I *believe* this should be easy, as both the first and last lines all contain the phrase 'GSHES'.

The big problem is that I want to do this to hundreds of files. I need something that will go through and just whack off the first and last lines of every file.

sed '$d' *.* > NOLASTLINES.dat

will lop off all of the last lines just fine, but I still have the first lines, and now there's only ONE first line! So I can't then do, say:

sed '1,1d' *.* > NOFIRSTLINES.dat

Is there a way to do this with grep?

Or another way?

Thanks in advance for any help you can offer!


Old 09-20-2007, 10:04 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
This smells of homework to me, so here is my oblique reply.

Grep will filter based on the contents of lines. You can filter for a pattern, or for not a pattern. The manual page will tell you what option to use for this. Search for "invert".

Sed is rather more flexible than grep. You can specify a range of input lines to operate on, and some operation(s). The thing to know if that it goes through a cycle of reading input, evaluating addresses and executing commands. You can use the q command to quit before other commands are executed, so you can probably imagine how to avoid printing the last line. Also, read what the -n option does in the manual page.
Old 09-20-2007, 07:27 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,198

Rep: Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310
you can also use a combo of wc plus head and tail.
Old 09-27-2007, 09:11 AM   #4
LQ Newbie
Registered: Sep 2007
Posts: 1

Rep: Reputation: 0

sed '2,$ {p}'
Old 09-28-2007, 07:21 AM   #5
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 700

Rep: Reputation: 423Reputation: 423Reputation: 423Reputation: 423Reputation: 423
You can try this approach.
(for fn in $@;
        echo "e $fn";
        echo '1d';
        echo '$d';
        echo 'w';
done ) | ed
and run as
$ ./ <filelist>
Old 09-28-2007, 07:28 AM   #6
LQ Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
I haven't got a terminal to test&try in, but I suspect this could be achieved with head, tail, grep and echo as well. I would probably wrap them into a script and then use find (or something else) to produce a file list and execute the script on each file then (can be done using xargs for example, or just find). The script would use head and tail to grab the first/last line(s), and grep+echo would then write everything except those lines back to the original file. Something like that; I haven't got a ready script, but I imagine it's not too difficult..efficiency is another matter then (is it faster to use this method, or sed, or something else, for a large number of big files..), but anyway this should work, and since those commands are basic tools (grep, head, tail, echo, maybe xargs, find or whatever needed to make it work the way you need), it should be portable and pretty easy to create too.

I tend to start off with the simplest method I can think of, because there is no sense in shooting a fly with a shotgun. Rather use a needle against a bull, if it does the job..


commands, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete lines using awk kkjegan Programming 13 09-11-2007 07:36 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
How to Delete nnn lines from the top of a file with out opening it? rhelpm Linux - Newbie 6 12-07-2006 02:46 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
delete some lines from a file freelinuxcpp Linux - Software 4 01-17-2004 10:28 AM

All times are GMT -5. The time now is 03:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration