Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-07-2011, 05:35 PM   #1
LQ Newbie
Registered: Apr 2011
Posts: 9

Rep: Reputation: 0
deleting a specific line which contains 2 patterns

hi there i have a problem about deleting a line from a text file which contains two specific patterns. i am using
"sed -i "/$name/ d" peop.txt" but i must use one more variable which is surname.


and this is the code of text file.
and the second question when i use "/$name/ d" it deletes not only the names which are macthing with $name but also all words that contain $name. so how can i fix these problems_?. and thx for your help : )
Old 04-07-2011, 08:37 PM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,643

Rep: Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961
Well the answer to question 1 would be that as you have not told us what is in $name currently and which field in your file relates to names, I would not be able
to tell you how to add surname.

As to questions 2, of course sed looks for all matches to what you have said is a $name. Should you want to match tim but not timothy you need to give sed more information
on where your word stops, ie if it delimited by the start of the line and a colon after it you can use:

sed -i "/^$name:/d" peop.txt
Old 04-07-2011, 09:05 PM   #3
Registered: Jan 2009
Distribution: Debian
Posts: 59
Blog Entries: 2

Rep: Reputation: 6
Originally Posted by quartaela View Post
If the row definition is:

then you could:

sed -i "/^$name:$surname/d" peop.txt


you could mix this up with other "regex" and match on the e-mail in the last field as well:


sed -i "/^$name:$surname:.*:.*:$email_address/d" peop.txt
Search from regex at you favourite search engine.


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
[SOLVED] Copy and replacing specific line from file1 to file2 line by line vjramana Programming 10 03-28-2011 08:49 AM
deleting duplicate lines without deleting first instance of the duplicated line jkeertir Linux - Newbie 2 02-07-2011 07:55 AM
SED how to find multiple patterns on a single line yaazz Programming 9 07-31-2009 05:20 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
Deleting text between two different patterns activeco Linux - General 7 09-13-2005 07:05 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration