LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Removing a line from file which is having specific pattern using shell script (https://www.linuxquestions.org/questions/linux-newbie-8/removing-a-line-from-file-which-is-having-specific-pattern-using-shell-script-869851/)

emcykm 03-20-2011 05:36 PM

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.

David the H. 03-20-2011 05:55 PM

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.

trist007 03-20-2011 05:58 PM

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. :)

TB0ne 03-20-2011 06:09 PM

Quote:

Originally Posted by emcykm (Post 4297432)
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.....

grail 03-20-2011 09:00 PM

I would also ask, is it a single 'a' on an entire line or an 'a' on its own anywhere on the line?

As the above have mentioned, the main issue is your regular expression being used is inadequate based on your requirements.

emcykm 03-21-2011 05:49 AM

Its Resolved......
 
Thnks David, Trist, TBone & Grail for your help... i have found my script worked after using grep -x -v option..

Thnks once again to you all..


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