LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-07-2011, 04:35 PM   #1
quartaela
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.

"
burak:ak:3242:2342:dsa@a.com
gokhan:an:432:4234:da@a.com
"

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, 07:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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:
Code:
name=tim

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

Rep: Reputation: 6
Quote:
Originally Posted by quartaela View Post
Code:
burak:ak:3242:2342:dsa@a.com
gokhan:an:432:4234:da@a.com
If the row definition is:
name
surname
another_field
yet_another_field
email_address

then you could:
[code]
name=gokhan
surname=an

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

[code]

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

Code:
name=gokhan
surname=an
email_address=da@a.com

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


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

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

All times are GMT -5. The time now is 04:30 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