LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 08-11-2009, 01:01 PM   #1
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Rep: Reputation: 23
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
 
Old 08-11-2009, 01:12 PM   #2
Nevahre
LQ Newbie
 
Registered: Aug 2009
Posts: 21

Rep: Reputation: 16
Hi,

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

if you use a new sed you can use -i to change yourfile at once.
 
Old 08-11-2009, 01:21 PM   #3
eth1
Member
 
Registered: May 2008
Posts: 97

Rep: Reputation: 20
Quote:
Originally Posted by Nevahre View Post
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
 
Old 08-11-2009, 01:22 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
head -7 file
 
Old 08-11-2009, 01:24 PM   #5
Nevahre
LQ Newbie
 
Registered: Aug 2009
Posts: 21

Rep: Reputation: 16
Quote:
Originally Posted by eth1 View Post
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!!!
 
Old 08-11-2009, 03:55 PM   #6
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by eth1 View Post
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?
 
Old 08-11-2009, 04:04 PM   #7
Nevahre
LQ Newbie
 
Registered: Aug 2009
Posts: 21

Rep: Reputation: 16
You need to escape the $ in that line:

ssh <hostname> /usr/local/bin/sed -i '18,\$d' /file/file
 
Old 08-11-2009, 04:16 PM   #8
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by Nevahre View Post
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
 
  


Reply


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
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 09:51 PM
how do u delete duplicate lines bharatbsharma Programming 4 10-29-2007 07:04 PM
delete lines from files bharatbsharma Programming 5 10-25-2007 09:43 AM
Delete first and last lines of a file ChainsawPenguin Programming 5 09-28-2007 08:28 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM


All times are GMT -5. The time now is 07:10 PM.

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