LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 06: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, 06:55 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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, 06:58 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,004

Rep: Reputation: 67
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 06:59 PM.
 
2 members found this post helpful.
Old 03-20-2011, 07:09 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,996

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
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, 10:00 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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, 06: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05: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 08:57 AM
Shell Script to Delete part text of a line if pattern matches harsha1980 Programming 36 04-16-2010 04:36 AM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 02:39 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration