LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-22-2010, 02:23 PM   #1
rafaeldeoliveiracosta
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 1
how to exclude everything except a pattern with sed


Hi,

I have a text file with this content:

9999,name,08,date
9999,name,19,date
9999,name,07,date
9999,name,02,date
9999,name,11,date

and i want exclude with sed every line where the code is different than 08 and 07

the desirable output is:
9999,name,08,date
9999,name,07,date

how can i do this ?
 
Old 07-22-2010, 08:44 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
use awk instead. Its easier
Code:
awk -F"," '$3~/^0[78]/' file
 
Old 07-22-2010, 08:50 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,801

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Au contraire!! SED is much easier......

Code:
sed -r '/0[78]/!d' file
 
Old 07-22-2010, 08:51 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,801

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
((We haven't had a good SED vs. AWK smackdown in quite some time.....))
 
Old 07-22-2010, 09:03 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by pixellany View Post
Au contraire!! SED is much easier......

Code:
sed -r '/0[78]/!d' file

note, the awk code uses field 3. In order for sed to do the same and not cause false deletion, more sed code is needed

Code:
$ more file
9999,name,08,date
9999,name08,19,date
9999,name,07,date
9999,name,02,date
9999,name,11,date

$ awk -F"," '$3~/^0[78]/' file
9999,name,08,date
9999,name,07,date

$ sed -r '/0[78]/!d' file
9999,name,08,date
9999,name08,19,date
9999,name,07,date
As i said before, use awk if the data has distinct fields and field delimiters.
 
Old 07-23-2010, 02:15 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,168

Rep: Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775
Plus if we do make the assumption that the naming convention will not move away from the original posted so that the sed works, still awk
to the rescue:
Code:
#sed
sed -r '/0[78]/!d' file

#awk
awk '/0[78]/' file
 
Old 07-23-2010, 05:01 AM   #7
rafaeldeoliveiracosta
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 1
Hi guys,

first of all thanks everyone but i forgot to tell why i should use sed...

the reason is that i want to edit the input file (with -i option of sed) just removing unwanted lines.

input file before run the sed command:
9999,name,08,date
9999,name08,19,date
9999,name,07,date
9999,name,02,date
9999,name,11,date

input file after run the sed command:
9999,name,08,date
9999,name,07,date
 
Old 07-23-2010, 05:46 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,168

Rep: Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775
No probs ... you can redirect the awk output if you wish to use it
 
Old 07-23-2010, 05:51 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,792

Rep: Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923
And, as is usually the case, you are much better stating your problem rather than demanding an answer you think is the best.

Others will usually provide better.
 
Old 07-23-2010, 09:56 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,801

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
The response to my comment about SED vs AWK is of course correct---my comparison was "apples and oranges". And AWK is definitely the right answer in many situations.

The hard thing is to always state precisely the criteria to be used when constructing code to match patterns. It sometimes takes a bit of thought to be sure you know what is to be matched and what is NOT to be matched.

Looking at the data sample in the original post, there is no obvious reason to be concerned about the column position---only the detection of the specific patterns "07" and "08". It's up to the owner of the file to be sure that an **adequate** regex is being used.
 
Old 07-23-2010, 10:10 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,168

Rep: Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775
Quote:
The response to my comment about SED vs AWK is of course correct---my comparison was "apples and oranges".
apples

no wait

oranges

ahhh ... i just can't help a challenge

LOL
 
Old 07-23-2010, 10:13 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by pixellany View Post
Looking at the data sample in the original post, there is no obvious reason to be concerned about the column position---only the detection of the specific patterns "07" and "08".
but then again, if you look at the sample data, a "name" and "date" column might have "08" or "07". eg a user can have account name user08, or john07 or the date column may have things like 2010/07/23. Of course, this is based on "educated" guess, since OP doesn't provide actual data format. its advisable to look at specific column and not the whole line.
 
1 members found this post helpful.
Old 07-23-2010, 11:36 AM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,801

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
correct---the safest thing is to make the code as restrictive as possible. My main point is that the user has to make the judgement as to how hard they need to work to get the results they want----you can often do very simple things IF you know what's in the file.
 
  


Reply

Tags
regexp, script, sed, shell


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed pattern replacement icyrail Programming 3 10-27-2009 05:33 AM
Exclude file pattern from unzip command XeroXer Linux - Newbie 3 01-10-2009 07:10 AM
last pattern with sed? xpto09 Linux - Newbie 6 10-04-2007 08:01 PM
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 03:16 AM
How exclude | from txt.file using awk or sed? sarajevo Programming 2 08-21-2006 07:26 AM


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

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