LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to exclude everything except a pattern with sed (http://www.linuxquestions.org/questions/programming-9/how-to-exclude-everything-except-a-pattern-with-sed-821552/)

rafaeldeoliveiracosta 07-22-2010 02:23 PM

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 ?

ghostdog74 07-22-2010 08:44 PM

use awk instead. Its easier
Code:

awk -F"," '$3~/^0[78]/' file

pixellany 07-22-2010 08:50 PM

Au contraire!! SED is much easier......;)

Code:

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

pixellany 07-22-2010 08:51 PM

((We haven't had a good SED vs. AWK smackdown in quite some time.....:)))

ghostdog74 07-22-2010 09:03 PM

Quote:

Originally Posted by pixellany (Post 4042522)
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.

grail 07-23-2010 02:15 AM

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


rafaeldeoliveiracosta 07-23-2010 05:01 AM

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

grail 07-23-2010 05:46 AM

No probs ... you can redirect the awk output if you wish to use it :)

syg00 07-23-2010 05:51 AM

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.

pixellany 07-23-2010 09:56 AM

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.

grail 07-23-2010 10:10 AM

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

ghostdog74 07-23-2010 10:13 AM

Quote:

Originally Posted by pixellany (Post 4043119)
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.

pixellany 07-23-2010 11:36 AM

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.


All times are GMT -5. The time now is 03:53 AM.