Hisham Hijjawi
Hisham Hijjawi

Reputation: 2415

awk find lines between range

I am trying to use awk to get all lines between two words. My first word is a directory and my second is a sequence of four minus charaters like so: ----

Here is what I have tried:

awk '/\/ua\/Debug\/xeGenReport\/src\//,/----/ errorLog2

Sample input:

/ua/Debug/xeGenReport/src/
++++
16955:../main2.C:492:22: error: something
16959:../main2.C:577:21: error: something
16963:../report2.C:2630:22: error: something
----
/ua/Debug/xeGenReport/src/ // I don't want this line in the final output
----
More text here I don't want

Desired output:

/ua/Debug/xeGenReport/src/
++++
16955:../main2.C:492:22: error: something
16959:../main2.C:577:21: error: something
16963:../report2.C:2630:22: error: something
----

I have tried other permutations, all give me different errors.

Upvotes: 0

Views: 103

Answers (2)

Jotne
Jotne

Reputation: 41446

You need to escape the /

Some like this (exclude start/stop):

awk '/----/{f=0} f; /\/ua\/Debug\/xeGenReport\/src/{f=1}' file
++++
16955:../main2.C:492:22: error: something
16959:../main2.C:577:21: error: something
16963:../report2.C:2630:22: error: something

Or like this (include start/stop): (use exit to not get more than needed)

awk '/\/ua\/Debug\/xeGenReport\/src/{f=1} f; /----/{exit}' file
/ua/Debug/xeGenReport/src/
++++
16955:../main2.C:492:22: error: something
16959:../main2.C:577:21: error: something
16963:../report2.C:2630:22: error: something
----

Edit: This is from my personal archive :)

* Print text between "START/END" not inclusive (replace {f=0} with {exit} to prevent repetision)

exclusive
awk '/START/{f=1;next} /END/{f=0} f'
awk '/END/{f=0} f; /START/{f=1}' #best
inclusive
awk '/START/{f=1} /END/{f=0;print} f'
awk '/START/{f=1} f; /END/{f=0}'
awk '/START/,/END/'
sed -n '/SRART/,/END/p'
Do not print between from START to END #inclusive
awk '/START/{f=1;next} !f; /END/{f=0}'
awk '/START/,/END/{next}1'
sed '/START/,/END/d'

Upvotes: 2

jas
jas

Reputation: 10865

There's a school of thought that range patterns should be avoided, but since Jotne already posted the flag-style version, I'll show this alternative, which follows more closely to your original attempt:

$ awk '/\/ua\/Debug\/xeGenReport\/src/,/----/ {print; if ($0 ~ /----/) exit}' file
/ua/Debug/xeGenReport/src/
++++
16955:../main2.C:492:22: error: something
16959:../main2.C:577:21: error: something
16963:../report2.C:2630:22: error: something
----

Upvotes: 1

Related Questions