Skip to content
Advertisement

Shell : choosing string between two strings using sed

I have a log file in format like this :

pseudo=thierry33    pseudoConcat=thierry33
pseudo=i love you   pseudoConcat=i love you

I want to return all the strings which are between pseudo and pseudoConcat, my desired output is :

thierry33
i love you

How can I do this using sed or awk? I’m trying for a few days in vain. Thanks.

Advertisement

Answer

With GNU grep:

grep -oP '(?<=pseudo=).*?(?= *pseudoConcat)' file

Output without trailing spaces:

thierry33
i love you

With bash:

while read -r line; do [[ $line =~ pseudo=(.*?[^ ]) *pseudoConcat ]] && echo "${BASH_REMATCH[1]}"; done < file
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement