LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Vi or Bash,way to delete everything but first N#lines (http://www.linuxquestions.org/questions/programming-9/vi-or-bash-way-to-delete-everything-but-first-n-lines-746778/)

ADxD_7 08-11-2009 12:01 PM

Vi or Bash,way to delete everything but first N#lines
 
Ok so I have not idea how to do this and hope someone out there has a clue, what I need to be able to do is script a way to delete all BUT the first seven lines in a file, so everything below the seventh line is gone.

Anyone know any way of doing this? Any help is much appreciated :)

Nevahre 08-11-2009 12:12 PM

Hi,

sed could do that: sed '1,7d' yourfile

if you use a new sed you can use -i to change yourfile at once.

eth1 08-11-2009 12:21 PM

Quote:

Originally Posted by Nevahre (Post 3639519)
Hi,

sed could do that: sed '1,7d' yourfile

if you use a new sed you can use -i to change yourfile at once.

The above will delete the 1-7 lines and not what the OP requires.

Something like this would do,

Quote:

sed '8,$d' /root/file.txt

pwc101 08-11-2009 12:22 PM

Code:

head -7 file

Nevahre 08-11-2009 12:24 PM

Quote:

Originally Posted by eth1 (Post 3639529)
The above will delete the 1-7 lines and not what the OP requires.

Something like this would do,

Good intentions, wrong part of the file. Not the way to do your first post :(

Sorry for that!!!

ADxD_7 08-11-2009 02:55 PM

Quote:

Originally Posted by eth1 (Post 3639529)
The above will delete the 1-7 lines and not what the OP requires.

Something like this would do,

Ok - this does work, but I am trying to use it remotely like so

ssh <hostname> /usr/local/bin/sed -i '18,$d' /file/file

Now I test it from <hostname> itself and the command works - but when I use ssh to run it I get this :

/usr/local/bin/sed: -e expression #1, char 3: unexpected `,'

Why would that be?

Nevahre 08-11-2009 03:04 PM

You need to escape the $ in that line:

ssh <hostname> /usr/local/bin/sed -i '18,\$d' /file/file

ADxD_7 08-11-2009 03:16 PM

Quote:

Originally Posted by Nevahre (Post 3639667)
You need to escape the $ in that line:

ssh <hostname> /usr/local/bin/sed -i '18,\$d' /file/file

Ended up having to escape everything like so -


ssh <hostname> /usr/local/bin/sed -i \'18\,\$d\' /file

Works like a charm - Thanks for everyones help


All times are GMT -5. The time now is 01:59 PM.