How to print two matched patterns only from each line?

My input looks like this.

# Lot Of CODE Before
AppType_somethinglese=$(cat << EOF
AppType_test1='test-tool/blatest-tool-ear'
AppType_test2='test/blabla-ear'
# Lot Of CODE After

I want to print text betwen 1) _ and = and 2)/ and ' from each line
and exclude lines with "EOF".

Output looks like this

test1  blatest-tool-ear
test2  blabla-ear

How can I do this using preferably sed in bash shell?

---------- Post updated at 11:32 PM ---------- Previous update was at 11:05 PM ----------

I found answer to my problem.

awk -F "/|_|=|'" '/^AppType_/ && !/EOF$/ {print $2 " " $5}'  <code file>

I would like to see how this can be done using sed or GNU grep.

A possible solution in sed:

sed -n 's/^AppType_\(.*\)=.*\/\(.*\).$/\1 \2/p' code.file

Another in gawk:

gawk -F"['|_/=]" '$1 == "AppType" && $0 !~ / /{print $2, $5}' code.file

Another one

sed -n "s|.*_\([^=]*\)=.*/\([^']*\)'.*|\1 \2|p" input

Instead of the greedy matches .*= and .*' it takes the minimum matches by replacing the dot by [^=] and [^'] , respectively.
By using "quotes" one can simply take ' . While within 'ticks' one would need '\'' in order to get a ' for sed.
By using | as a delimiter one can simply take / . While with / delimiters one needs the \/ escape.

1 Like