[SOLVED] Removing a line from file which is having specific pattern using shell script
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.
Removing a line from file which is having specific pattern using shell script
I'm new to the shell scripting. can any one help in creating shell script for matching the content of the specific variable with file. it should remove that line from the file if line is containing same value as variable and keep the other content as it is.
i used grep -v for accomplishing the same. But grep will remove the pattern which is similar.
For eg. Assume file "test" contain datas : aa
ff
if i used grep -v command for the pattern "a" to this file this will remove content "aa" from the file.
I want the pattern only "a" should remove from the file, if it is existing. otherwise it should throw alert content not exists.
Start by reading the grep man page carefully. It contains a good bit of information about how to match various text patterns. Then get on Google and find yourself a good regular expressions tutorial.
This will scan your file and if it finds a line with "aa" in there it will delete the whole line.
Code:
sed -i '/aa/d' filename
However it's best to leave the "-i" option out to make sure the output is what you want. If it is, include the "-i" to make the changes. Would be bad if it changed your file and it turned out not to be what you wanted. There's no undo button.
I'm new to the shell scripting. can any one help in creating shell script for matching the content of the specific variable with file. it should remove that line from the file if line is containing same value as variable and keep the other content as it is.
i used grep -v for accomplishing the same. But grep will remove the pattern which is similar.
For eg. Assume file "test" contain datas : aa
ff
if i used grep -v command for the pattern "a" to this file this will remove content "aa" from the file.
I want the pattern only "a" should remove from the file, if it is existing. otherwise it should throw alert content not exists.
Ok...now we know what you WANT. Can you show us what you've DONE so far, to accomplish this? This sounds very much like homework, and as David the H. mentioned, reading the man pages will help you quite a bit. We'll be glad to help you, but we're not going to write your script for you.
Also, if it's not homework, does it have to be a shell script? Perl has some nice pattern-matching capabilities.....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.