'I want to grep/extract some website immediately above a particular string

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


Solution 1:[1]

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

Solution 2:[2]

Suggesting try this:

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

Explanations:

grep -B1 "SOLUTION NEEDED" long.log

Extract 1 line before each line matching "SOLUTION NEEDED"

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

From extracted lines. Extract URL value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TuxSax
Solution 2