LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2011, 10:33 PM   #1
chriscbe
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Rep: Reputation: 0
Deleting first line with unique string then all subsequent lines thereafter


Hello-
I am doing molecular dynamics where I have to edit files. I have looked at tutorials for grep and sed but can't find my solution. The files produced in my simulations look something like this:

ATOM 1825 NE2 GLN 112 113.646 27.895 14.456
ATOM 1826 HE21 GLN 112 114.020 26.957 14.490
ATOM 1827 HE22 GLN 112 112.649 28.039 14.388
ATOM 1828 C GLN 112 116.703 32.450 16.133
ATOM 1829 O GLN 112 117.841 32.024 15.931
ATOM 1830 OXT GLN 112 116.518 33.648 16.334
TER
ATOM 1831 Na+ Na+ 113 98.608 27.104 23.903
TER
ATOM 1832 Na+ Na+ 114 90.608 27.104 29.903
TER
ATOM 1833 O WAT 115 123.924 44.364 41.207
ATOM 1834 H1 WAT 115 123.843 43.450 40.937
ATOM 1835 H2 WAT 115 123.510 44.861 40.501
TER
ATOM 1836 O WAT 116 122.056 46.142 43.088
ATOM 1837 H1 WAT 116 121.280 45.662 43.378
ATOM 1838 H2 WAT 116 122.707 45.463 42.914

and then on and on. What I want to do is get rid of the first line that has 'WAT' in it and then all the lines subsequent to that to the end of the file.

Someone suggested grep -v 'WAT' BUT that still leaves all the TER in there.

can someone tell me how to do this?

Thanks for helping a newbie
 
Old 01-20-2011, 10:52 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!


Something like this?
Code:
sed '/WAT/,$d' atoms
ATOM 1825 NE2 GLN 112 113.646 27.895 14.456
ATOM 1826 HE21 GLN 112 114.020 26.957 14.490
ATOM 1827 HE22 GLN 112 112.649 28.039 14.388
ATOM 1828 C GLN 112 116.703 32.450 16.133
ATOM 1829 O GLN 112 117.841 32.024 15.931
ATOM 1830 OXT GLN 112 116.518 33.648 16.334
TER
ATOM 1831 Na+ Na+ 113 98.608 27.104 23.903
TER
ATOM 1832 Na+ Na+ 114 90.608 27.104 29.903
TER


Cheers,
Tink
 
Old 01-20-2011, 11:00 PM   #3
chriscbe
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks Tink! Works great
 
Old 01-21-2011, 12:39 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
You're welcome. Do you understand how it works?



Cheers,
Tink
 
Old 01-21-2011, 11:11 PM   #5
chriscbe
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Original Poster
Rep: Reputation: 0
I understand now- basically sed scans the lines of the file for 'WAT' as used in the delimiter /WAT/ the $ stands for the end of the file and d is for delete.

I'm afraid I'm not yet sure how to use the ,

sed isn't too hard for me with substitutions because I use vi and one often uses- e.g.- s/dd/ee/g

so I understand sed '/WAT/,d$' except the use of the comma.

Thanks for asking.
 
Old 01-21-2011, 11:58 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The comma is helping to define the range, ie start with first line containing WAT and go until $ (end of file)
 
  


Reply



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
[SOLVED] use sed to find string pattern and delete subsequent characters jigg_fly Programming 16 10-10-2013 08:34 AM
find a string in a file and delete that line as well as x lines after tdnnash25 Linux - Server 3 06-24-2010 02:13 PM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
How to grep lines containing a certain string PLUS the line following that line? kmkocot Linux - Newbie 5 09-01-2009 03:54 PM
deleting unique HCL entries pbhj LQ Suggestions & Feedback 7 05-24-2008 07:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:14 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
Open Source Consulting | Domain Registration