[SOLVED] Bash How to delete only one line from a file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I would like to delete a single line from a file that contains many lines passing through the same values as the two parameters. Again, I would like to delete a single line and not all those that contain parameters. How can I make bash?
I would like to delete a single line from a file that contains many lines passing through the same values as the two parameters. Again, I would like to delete a single line and not all those that contain parameters. How can I make bash?
So you want a script that takes two parameters, and deletes the lines from a file that do not contain both of the strings that were passed as parameters?
if I understood correctly then there can be also an arbitrary amount of characters between the patterns and the order of the patterns does not matter, right? So these first lines would also be deleted if we use the parameters from your previous post:
Code:
Queen has News of the World <-- delete if first occurence
News of the World Queen <-- delete if first occurence
Here is how you can do it with sed:
Code:
p1=queen
p2='news of the world'
sed -r "/${p1}.*${p2}|${p2}.*${p1}/I {x;/^$/d}" file
Actually it exchanges the occurrences (each of them is replaced by the previous one). Example:
Code:
$ cat file
Something Here
Queen News of the World 1
Beatles Revolver
Queen News of the World 2
Something There
Queen News of the World 3
Goofy
Queen News of the World 4
Queen News of the World 5
Stop Now
$ sed -r "/${p1}.*${p2}|${p2}.*${p1}/I {x;/^$/d}" file
Something Here
Beatles Revolver
Queen News of the World 1
Something There
Queen News of the World 2
Goofy
Queen News of the World 3
Queen News of the World 4
Stop Now
I think adding ;q inside the block will stop processing the file after changing the first occurrence.
{x;/^$/d;q}
Doesn't this stop processing the rest of the file?
Code:
$ cat file
Something Here
Queen News of the World 1
Beatles Revolver
Queen News of the World 2
Something There
Queen News of the World 3
Goofy
Queen News of the World 4
Queen News of the World 5
Stop Now
$ sed -r "/${p1}.*${p2}|${p2}.*${p1}/I {x;/^$/d;q}" file
Something Here
Beatles Revolver
Queen News of the World 1
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.