Print between pattern without spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print between pattern without spaces
# 1  
Old 08-20-2015
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:

Code:
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
Code:
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
Code:
0-1000
1000-10000

.

Thanks.
# 2  
Old 08-20-2015
Hello Jamie_123,

Following may help you in same.
Code:
awk '{match($0,/\=.*\&/);print substr($0,RSTART+1,RLENGTH-2)}' Input_file

Output will be as follows.
Code:
0-1000
1000-10000

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-20-2015
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.

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

Thanks!
# 4  
Old 08-20-2015
Hello Jamie_123,

Following may help you to be more specific. Let me know if this helps.
Code:
awk '{match($0,/\&range.*\&reef/);print substr($0,RSTART+7,RLENGTH-12)}'  Input_file

Output will be as follows.
Code:
0-1000
1000-10000

This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 08-21-2015
Or:
Code:
sed -e 's/^.*range=//' -e 's/&.*//'

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

which will skip lines w/o "range=" entries.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

4. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

5. Shell Programming and Scripting

Script to match a pattern and print only the pattern and after that

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ...... .......xyz: abz: ..... I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns. ... (14 Replies)
Discussion started by: Serena
14 Replies

6. Shell Programming and Scripting

Script to compare pattern and print a different pattern in each line

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ......qrt: .... .......xyz: abz: ......qrt: ... I have tried using awk and cut, but the position of these values keep changing, so I wasn't able to get... (2 Replies)
Discussion started by: Serena
2 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

9. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

10. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies
Login or Register to Ask a Question