LinuxQuestions.org
Help answer threads with 0 replies.
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 03-20-2011, 05:36 PM   #1
emcykm
LQ Newbie
 
Registered: Mar 2011
Posts: 10

Rep: Reputation: 0
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.
 
Old 03-20-2011, 05:55 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 03-20-2011, 05:58 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
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.

Last edited by trist007; 03-20-2011 at 05:59 PM.
 
2 members found this post helpful.
Old 03-20-2011, 06:09 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by emcykm View Post
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.....
 
1 members found this post helpful.
Old 03-20-2011, 09:00 PM   #5
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
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.
 
1 members found this post helpful.
Old 03-21-2011, 05:49 AM   #6
emcykm
LQ Newbie
 
Registered: Mar 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Smile 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..
 
  


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
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
Shell Script to Delete part text of a line if pattern matches harsha1980 Programming 36 04-16-2010 03:36 AM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM

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

All times are GMT -5. The time now is 02:54 AM.

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