Print between pattern without spaces

I seem to have hit a curious problem where sed and awk based regex do not seem to work. Perhaps I am missing a switch to look within the same line and not across lines.

I have input as follows:

2E3DD452DA34CB2E9D003E934CC5E57843F5AF82.04713CF0013BBB0731441B693992F2E625D33589&range=0-1000&reef=121816
2E3DD452DA34CB2E9D003E934CC5E57843F5AF82.04713CF0013BBB0731441B693992F2E625D33589&range=1000-10000&reef=121817

I want to print text between the range tag and the &. However

sed -n '/range/,/reef/p'

does not seem to work in this case. Do I need to escape things differently here?

The expected output is

0-1000
1000-10000

.

Thanks.

Hello Jamie_123,

Following may help you in same.

awk '{match($0,/\=.*\&/);print substr($0,RSTART+1,RLENGTH-2)}' Input_file
 

Output will be as follows.

0-1000
1000-10000
 

Thanks,
R. Singh

1 Like

Hi RavinderSingh13,

Thanks for the quick response. The solution seems to be very specific, depending on = and numbers. It seems to find other matches in the larger file that I have. Is a general solution based on range and reef possible?

---------- Post updated at 03:17 PM ---------- Previous update was at 03:13 PM ----------

I figure something like this might work for me. I am using RSTART+6 to exclude range and the +15 is just to get everything until the end of the line.

awk '{match($0,/range/);print substr($0,RSTART+6,RSTART+15)}' | awk 'BEGIN { FS = "&" } ; { print $1 }'

Thanks!

Hello Jamie_123,

Following may help you to be more specific. Let me know if this helps.

awk '{match($0,/\&range.*\&reef/);print substr($0,RSTART+7,RLENGTH-12)}'  Input_file
 

Output will be as follows.

0-1000
1000-10000
 
1 Like

Or:

sed -e 's/^.*range=//' -e 's/&.*//'

or even:

sed -ne 's/^.*range=\([^&]*\)&.*$/\1/p'

which will skip lines w/o "range=" entries.