Deleting first line with unique string then all subsequent lines thereafter
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.