awk command to get file content until 2 occurrence of pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to get file content until 2 occurrence of pattern match
# 1  
Old 06-26-2016
awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match,


INPUT FILE:

Code:
JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2">
    <person>Romi</person>
    <appName>SAP</appName>
</custOptIn>

JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<custOptIn xmlns="http://com/walm/ta/cu/ccs/xml3">
    <person>Anja</person>
    <appName>SAP</appName>
</custOptIn>

JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<custOptIn xmlns="http://com/walm/ta/cu/ccs/xml1">
    <person>Rohit</person>
    <appName>SAP</appName>
</custOptIn>

JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<custOptIn xmlns="http://com/walm/ta/cu/ccs/xml">
    <person>Roho</person>
    <appName>SAP</appName>
</custOptIn>


Can you help me to get awk command to print lines three occurrence of JMS_BODY_FIELD



Example output should be:

Code:
JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2">
    <person>Romi</person>
    <appName>SAP</appName>
</custOptIn>

JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<custOptIn xmlns="http://com/walm/ta/cu/ccs/xml3">
    <person>Anja</person>
    <appName>SAP</appName>
</custOptIn>

JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<custOptIn xmlns="http://com/walm/ta/cu/ccs/xml1">
    <person>Rohit</person>
    <appName>SAP</appName>
</custOptIn>


Last edited by Don Cragun; 06-26-2016 at 10:07 PM.. Reason: Change B and U tags to ICODE tags; change I tags and I and COLOR tags to CODE tags.
# 2  
Old 06-26-2016
Is there anything you have done, already? Where are you having troubles with?
Please, could you edit your post to surround your examples with the proper CODE tags, in order to preserve any blank spaces?

Last edited by Aia; 06-27-2016 at 02:36 AM.. Reason: correct grammar
# 3  
Old 06-26-2016
I have below awk command but it brings all the lines to single messages when ever it finds JMS_BODY_FIELD, I want them just like that without bringing them to single line.


Code:
awk 'BEGIN{ORS=FS}/JMS_BODY_FIELD/{if(NR>1)$0=RS$0}1;END{print RS}'


Last edited by Don Cragun; 06-26-2016 at 10:09 PM.. Reason: Add CODE tags.
# 4  
Old 06-26-2016
Quote:
Originally Posted by prince1987
I have below awk command but it brings all the lines to single messages when ever it finds JMS_BODY_FIELD, I want them just like that without bringing them to single line.


Code:
awk 'BEGIN{ORS=FS}/JMS_BODY_FIELD/{if(NR>1)$0=RS$0}1;END{print RS}'

Take a look at my quote of your post. This is how it must look when you post. Please, use the CODE tags.

Code:
awk '/^JMS_BODY_FIELD/ && ++c <= 3' ORS="\n\n" RS= input.file

# 5  
Old 06-26-2016
IT works nice, Thanks buddy!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

5. Shell Programming and Scripting

Sorting content between match pattern and move on with awk and sed

S 0.0 0.0 (reg, inst050) k e f d c S 0.0 0.0 (mux, m030) k g r s x v S 0.0 0.0 (reg, inst020) q s n m (12 Replies)
Discussion started by: ctphua
12 Replies

6. Shell Programming and Scripting

awk Pattern Match One File to Another

I want to read from file 1 and pattern match in file two and print field two from the next line. File 1: user1 user2 user3 File 2: name=user1 gud=12345 name=user2 gud=32456 I have this pattern hardcoded but can't work out how to pass file 1 to the pattern match: (6 Replies)
Discussion started by: u20sr
6 Replies

7. Shell Programming and Scripting

Match from one pattern to second occurrence of second pattern

Given an XML file that contains (NOT "consists of"): </dict> <key>system.</key> <dict> <key>rule</key> <string>default</string> </dict> <key>system.burn</key> ... (9 Replies)
Discussion started by: jnojr
9 Replies

8. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

9. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

10. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies
Login or Register to Ask a Question