[SOLVED] how to exclude everything except a pattern with sed
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.