2

My text file looks like this:

city=london&language=en&x=12345&y=6789&z=7
sometext?z=7
city=dublin&language=en&x=12345&y=6789&z=7
city=madrid&language=en&x=12345&y=6789&z=7
sometext?y
sometext?x

I need to filter the text by criteria - if certain line contains:

city, language, x, y, z anywhere in the line

leave this lines.

The output after filtering my data would be:

city=london&language=en&x=12345&y=6789&z=7
city=dublin&language=en&x=12345&y=6789&z=7
city=madrid&language=en&x=12345&y=6789&z=7

What is the simplest way to do it?

| improve this question | | | | |
1

You can straightforwardly combine regular expression tests logically in awk e.g.

$ awk '/city/ && /language/ && /x/ && /y/ && /z/' file.txt
city=london&language=en&x=12345&y=6789&z=7
city=dublin&language=en&x=12345&y=6789&z=7
city=madrid&language=en&x=12345&y=6789&z=7
| improve this answer | | | | |
  • thanks! any idea how to add color to result (highlight city, language, x, y, z)? – vayacondios2015 Aug 24 '16 at 2:12
  • @vayacondios2015 not easily, no - AFAIK you'd need to find the start and length of each match (at least with GNU awk, that should be possible using the match function and RSTART, RLENGTH variables), then insert color codes programatically – steeldriver Aug 24 '16 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.