keep only a string from line

hello everyone

I'm writing a bash script and I have to parse a line that looks as follows:

a/ b:4 word=egg things.?/things2//

no matter what other words or characters exist in the line,I only want to keep the word(or number) that comes after the =
in this case the word is "egg".
Is there a way doing this by using sed?

Does it have to be sed?

echo 'a/ b:4 word=egg things.?/things2//' | perl -nle '/=(\w+)/&&print $1'

no it doesn't have to be sed.The only thing is that is a bash script.
Is this a problem for using perl?

1 Like

Perl will work just fine in a bash script.

1 Like

You can also use the following :


echo "a/ b:4 word=egg things.?/things2//" | awk -F= '{print $2}' | cut -d' ' -f1

If you already have that line in a shell variable, you do not need to resort to sed, awk, or perl. Just use the shell's parameter expansion operators:

x='a/ b:4 word=egg things.?/things2//'
y=${x#*=}
y=${y%% *}

x contains the original line. y contains only the word/number sought.

Regards,
Alister

1 Like