Home > Software design >  I want to grep/extract some website immediately above a particular string
I want to grep/extract some website immediately above a particular string

Time:04-22

I want some specific websites to be extracted/grep before a string.

Example below:

Lets assume what is below is gibberish that i only want to extract website that is above SOLUTION NEEDED only! Not the other websites.

I only want to extract websites that can be found immediately above SOLUTION NEEDED

[36m[•] URL: abvfrt.com|LOAD
EXCEPTION: HTTPSConnectionPool(host='0.0.0.0', port=443): Read timed out. (read timeout=4)
[36m[•] URL: abc dot com |LOAD: xyz=345
EXCEPTION: HTTPSConnectionPool(host='0.0.0.0', port=443): Read timed out. (read timeout=4)
URL: Example dot com |: ABXCDRTTT
33m SOLUTION NEEDED

CodePudding user response:

You can use grep -B n and -A n for showing you n number of lines Before or After the matched string.

In your case, something like

grep -B 1 "SOLUTION NEEDED" /path/to/file

CodePudding user response:

Suggesting try this:

  grep -B1 "SOLUTION NEEDED" long.log | pgrep -o "(?<=^URL: )[^|]*"

Explanations:

grep -B1 "SOLUTION NEEDED" long.log

Extract 1 line before each line matching "SOLUTION NEEDED"

pgrep -oP "(?<=^URL: )[^|]*"

From extracted lines. Extract URL value.

  • Related