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 ? |
use awk instead. Its easier
Code:
awk -F"," '$3~/^0[78]/' file |
Au contraire!! SED is much easier......;)
Code:
sed -r '/0[78]/!d' file |
((We haven't had a good SED vs. AWK smackdown in quite some time.....:)))
|
Quote:
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 |
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 |
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 |
No probs ... you can redirect the awk output if you wish to use it :)
|
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. |
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. |
Quote:
no wait oranges ahhh ... i just can't help a challenge LOL |
Quote:
|
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 12:58 AM. |