3

I have a file which contains below sample line having a common keyword P00001121 in each line.

[H00,P00001121] CHANNEL__OPEN:TCP_ADDRESS='10.32.130.202' TCP_PORT='80'"
[H00,P00001121] COMMS_QUEUING:RECV='01/08/2019 14:33:45.064410' QUEUE_TIME='00000.000' TO_DEV='
[H00,P00001121] FROM_EXCHANGE:HTTP/1.1 200 OK\r\n
Server: Apache-Coyote/1.1\r\n
Content-Type: text/xml;charset=UTF-8\r\n
Content-Length: 878\r\n
Date: Thu, 01 Aug 2019 04:33:45 GMT\r\n
\r\n
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
[H00,P00001121] CONTEXT=PARAMETER : CLIENTID =>
[H00,P00001121] CONTEXT=PARAMETER : TRANSACTIONID => 20190801143344970"
[H00,P00001121] CONTEXT=PARAMETER : KEY_TYPE => SERVICEID
[H00,P00001121] CONTEXT=PARAMETER : ORDERSOURCESYSTEM =>

I want to capture all the line of the keyword (P00001121) and also the following few lines below keyword which has \r\n in it (there may be 1 line with \r\n or 10 lines with \r\n in the following lines). Now all the lines in the file having keyword has those following line with \r\n in it. I am ok to use any command, be it grep, awk, cut etc whichever works.

[Server: Apache-Coyote/1.1\r\n
Content-Type: text/xml;charset=UTF-8\r\n
Content-Length: 878\r\n
Date: Thu, 01 Aug 2019 04:33:45 GMT\r\n
\r\n
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>]
  • Please post the sample output as text delimited text (separated as new lines ) as the one you have given is not line delimited and is being read as one long line. However in the meantime I will provide an answer by delimiting the one you have given and it should get you going. – BarBar1234 Aug 4 at 2:43
  • Interesting, when you go to inspect the element in devtools your text is nicely formatted. I wonder what caused unixexchange to lose its mind (and why parts of my answer got printed in red) – BarBar1234 Aug 4 at 3:02
  • 1
    Why are you editing your question with a new user? If you lost the credentials of your original user, look into the faq on how to regain access. It's not a good idea to create a new user then. Thank you. – Philippos Aug 5 at 7:26
  • Some grep(1) implementations (e.g. the GNU one, universal on Linux) allow you to ask for some lines before/after the matching one. – vonbrand Aug 7 at 19:48
  • If you want all lines with P00001121 and all following lines with \r\n, all the lines of your example, except for the <soap: line should get printed. Is that correct? The second sniplet seems to show the lines that don't contain P00001121, but that is different from what you describe, so why are you showing this part without explanation? – Philippos Aug 13 at 6:17
0

After having inspected the OPs text input (see comment), I was able to get the original nicely formatted text which looks like this:

[H00,P00001121] CHANNEL__OPEN:TCP_ADDRESS='10.32.130.202' TCP_PORT='80'"
[H00,P00001121] COMMS_QUEUING:RECV='01/08/2019 14:33:45.064410' QUEUE_TIME='00000.000' TO_DEV='
[H00,P00001121] FROM_EXCHANGE:HTTP/1.1 200 OK\r\n
Server: Apache-Coyote/1.1\r\n
Content-Type: text/xml;charset=UTF-8\r\n
Content-Length: 878\r\n
Date: Thu, 01 Aug 2019 04:33:45 GMT\r\n
\r\n

[H00,P00001121] CONTEXT=PARAMETER : CLIENTID =&gt;
[H00,P00001121] CONTEXT=PARAMETER : TRANSACTIONID =&gt; 20190801143344970"
[H00,P00001121] CONTEXT=PARAMETER : KEY_TYPE =&gt; SERVICEID
[H00,P00001121] CONTEXT=PARAMETER : ORDERSOURCESYSTEM =&gt;

Assuming the text resembles something like this (I'll update my answer after the provided text is edited-updated)

You can do (assume that your text input is named ex1

cat ex1 | grep -P '\\r\\n|P00001121' 

and you will get:

[H00,P00001121] CHANNEL__OPEN:TCP_ADDRESS='10.32.130.202' TCP_PORT='80'"
[H00,P00001121] COMMS_QUEUING:RECV='01/08/2019 14:33:45.064410' QUEUE_TIME='00000.000' TO_DEV='
[H00,P00001121] FROM_EXCHANGE:HTTP/1.1 200 OK\r\n
Server: Apache-Coyote/1.1\r\n
Content-Type: text/xml;charset=UTF-8\r\n
Content-Length: 878\r\n
Date: Thu, 01 Aug 2019 04:33:45 GMT\r\n
\r\n
[H00,P00001121] CONTEXT=PARAMETER : CLIENTID =&gt;
[H00,P00001121] CONTEXT=PARAMETER : TRANSACTIONID =&gt; 20190801143344970"
[H00,P00001121] CONTEXT=PARAMETER : KEY_TYPE =&gt; SERVICEID
[H00,P00001121] CONTEXT=PARAMETER : ORDERSOURCESYSTEM =&gt;

However, something tells me you might be looking for something else, like getting the selected line text only , or perhaps some additional grouping, if so please update your question.

  • 1
    You don't need to use cat. grep already sends the result to standard output just like awk, sed, and cut. – Nasir Riley Aug 4 at 3:55
0

A couple of tricks I've used with output similar to yours.

1. Filter out matching lines with grep

Notice that the lines you want do not include the string P00001121. Knowing this you can have grep show all the lines where this string is absent:

Example

$ grep -v P00001121 afile
Server: Apache-Coyote/1.1\r\n
Content-Type: text/xml;charset=UTF-8\r\n
Content-Length: 878\r\n
Date: Thu, 01 Aug 2019 04:33:45 GMT\r\n
\r\n
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>

2. Select a range of lines with sed

You can use sed to select all the lines between 2 patterns. Here I use sed -n to suppress sed's default behavior of printing each line of input. Next we tell sed to match any lines between 2 patterns and then and then print them (/p).

$ sed -n '/pattern1/,/pattern2/p' afile

Example

$ sed -n '/^.*Server/,/^\\r\\n/p' afile
Server: Apache-Coyote/1.1\r\n
Content-Type: text/xml;charset=UTF-8\r\n
Content-Length: 878\r\n
Date: Thu, 01 Aug 2019 04:33:45 GMT\r\n
\r\n
0

This can be done with grep, awk, or sed.

grep -v H00 file.txt

That -v switch prints out all of the lines not containing that string.

sed -n '/Server/,/soap/p' file.txt

That prints the lines between the ones containing those strings.

awk '/Server/','/soap/' file.txt

That prints out the lines between the ones containing those strings just like above but uses awk.

They all give your desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.