Skip to content
Advertisement

I want to extract any text between two symbol

I need to execute the file (a.out) and at the same time, I just need specific parts of the output, not everything. which is either located between these two string [[….]] or ((…..)). and I don’t have any idea where the other text is located (there is no specific pattern) and how the output looks like?

for example: if I run the file

./a.out

I will get this in the console,

[[Execution]]
YES((g: 1.000000))
((x: 5.000000))

115940 -3080
0[[Execution]]
YES((g: 50.000000))N0
NONONO
0 0
0[[Execution]]
YES((g: -1.000000))
0[[Execution]]

Instead, I want a text file that will look like:

Execution
g: 1.000000
x: 5.000000

Execution
g: 50.000000

Execution
-1.000000

Execution

I tried the grep/sed/tr but each time I got something different. I also want to use them at the same time with executing the program

e.g.

./a.out | sed |grep 

Update: I tried different commands and they all gave me the same result without changing my output.

./a.out | grep -o -P '(?<=[[).*(?=s]])'
./a.out | sed -n '/[[/,/]]/p'
./a.out 2>&1 | sed -e 's/.*[[(.*)]]/1/'

It seems like because there are many [[ and ]], So it confused or it wasn’t working on all the line of output

Advertisement

Answer

You may use this sed:

a.out | sed -En 's/.*[[(]{2}(.*)[])]{2}/1/p'

Execution
g: 1.000000
x: 5.000000
Execution
g: 50.000000N0
Execution
g: -1.000000
Execution
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement