4

Consider text.txt with this content:

blablabla    
&&&key word&&&
I want all 
these text and numbers 123
and chars !"£$%&
as output
&&&key word&&&
blablabla

I want to implement a regular Expressions with awk which obtain following output:

I want all 
these text and numbers 123
and chars !"£$%&
as output

I am using this expression:

awk "$1 ~ /^[&&&key word&&&].[&&&key word&&&]/" test.txt

but the result is not what I expect:

&&&key word&&&
&&&key word&&&

I apologize because maybe it is not a good question. I already tried many solutions from here and stackoverflow, I read some tutorials but still not able to solve it. If you can also give me a complete tutorial for this topic, I will be very thankful.

Edit: With $0 as advised, I get much better result:

awk "$0 ~ /[&&&key word&&&]/,/[&&&key word&&&]/" test.txt

results in:

&&&key word&&&
I want all 
these text and numbers 123
and chars !"£$%&
as result
&&&key word&&&

I still need to remove first and last line, I know if condition like awk "{if (NR!=1&&NR!=-1) {print}}" text.txt but I dont know how can I apply it on the output of above

Sadegh
  • 599
  • Keep in mind that awk/gawk generate queries to data based on the result separator. The default result is the line. Since you want your query/regex to span multiple lines, the initial result must be the full set of lines, which is not easy to do unless you know what constitutes the literal end of each result block. If you're not clear, just have awk print out $0, the entire result item. That will show you where your assumptions might err. When I deal with blocks of data for results, I usually use ^$, that is, an empty line, to indicate the separator, but it could be anything. – Lizardx Feb 29 '16 at 22:24
  • @Lizardx thanks for advice.I am not sure if I understood it all, but I tried to use $0. Please see update inside my question – Sadegh Feb 29 '16 at 22:32
  • No, you didn't understand. Just use: awk '{print "result: " $0}' test.file to see what the actual record (sorry, I said, result, it's record, I think) is in the first place. You're expecting the result/record each line of awk works on to be the entire thing, but it's not, it's a single line. You have to change the record separator then figure out how to treat the entire record. that's really hard to explain so I'll leave that to someone else, too complicated. That's why I'm not trying to post an actual 'answer'. – Lizardx Feb 29 '16 at 22:41
  • I would suggest reading up on awk, what you're trying to do is hard and not something I'd try if I was just starting out. Good luck. – Lizardx Feb 29 '16 at 22:45
  • Ok Lizardx,thanks. At least your comment show this question is not duplicate so please some one remove the duplicate. – Sadegh Feb 29 '16 at 22:46

1 Answers1

8
awk 'BEGIN {p=0}; /&&&key word&&&/ { p = ! p ; next } ; p { print }' file

This prints only the lines between two matched instances of &&&key word&&&. The variable p is used as a flag to decide whether to print or not....it starts off as false (don't print) and is logically negated (reversed) every time &&&key word&&& is matched on an input line.

If there are multiple blocks of input lines bounded by &&&key word&&& then ALL such blocks (but nothing else) will be printed. If there is a beginning &&&key word&&& but no ending one to match it, all subsequent lines to the end of the file will be printed.

Output:

I want all 
these text and numbers 123
and chars !"£$%&
as output

If the starting and ending keywords are different (e.g. 'START' and 'STOP'), you probably want to do something more like this:

awk 'BEGIN {p=0};
     /START/ { p = 1 ; next };
     /STOP/ { p = 0 ; next};
     p { print }' file
cas
  • 78,579
  • thanks. I really like the solution, it is simple and effective. for my real data I need 2nd solution, which is more flexible. when I run it, I receive this error: '/START/' is not recognized as an internal or external command, operable program or batch file. '/STOP/' is not recognized as an internal or external command, operable program or batch file. 'p' is not recognized as an internal or external command, operable program or batch file.. I am trying to fix it. first solution works perfect. – Sadegh Mar 01 '16 at 07:57
  • The problem was windows .bat file I created with 2nd code. I just put all lines in sequence and it works fine – Sadegh Mar 01 '16 at 07:59