LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Delete first and last lines of a file (https://www.linuxquestions.org/questions/programming-9/delete-first-and-last-lines-of-a-file-586069/)

ChainsawPenguin 09-20-2007 09:48 AM

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!

Cheers!

CP

matthewg42 09-20-2007 10:04 AM

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.

chrism01 09-20-2007 07:27 PM

you can also use a combo of wc plus head and tail.

mrball 09-27-2007 09:11 AM

try

sed '2,$ {p}'

firstfire 09-28-2007 07:21 AM

Hello.
You can try this approach.
Code:

#!/bin/bash
# strip-lines.sh
(for fn in $@;
do
        echo "e $fn";
        echo '1d';
        echo '$d';
        echo 'w';
done ) | ed

and run as
Code:

$ ./strip-lines.sh <filelist>

b0uncer 09-28-2007 07:28 AM

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..


All times are GMT -5. The time now is 07:39 AM.