Alan Ihre
Alan Ihre

Reputation: 75

Grep in bash with regex

I am getting the following output from a bash script:

INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist

and I would like to get only the path(MajorDomo/MajorDomo-Info.plist) using grep. In other words, everything after the equals sign. Any ideas of how to do this?

Upvotes: 2

Views: 644

Answers (5)

Sir Athos
Sir Athos

Reputation: 9877

You could use cut as well:

your_script | cut -d = -f 2-

(where your_script does something equivalent to echo INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist)

If you need to trim the space at the beginning:

your_script | cut -d = -f 2- | cut -d ' ' -f 2-

If you have multiple spaces at the beginning and you want to trim them all, you'll have to fall back to sed: your_script | cut -d = -f 2- | sed 's/^ *//' (or, simpler, your_script | sed 's/^[^=]*= *//')

Upvotes: 3

iruvar
iruvar

Reputation: 23384

Bash

IFS=' =' read -r _ x <<<"INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist"
printf "%s\n" "$x"
MajorDomo/MajorDomo-Info.plist

Upvotes: 0

Sir Athos
Sir Athos

Reputation: 9877

Assuming your script outputs a single line, there is a shell only solution:

line="$(your_script)"
echo "${line#*= }"

Upvotes: 2

anubhava
anubhava

Reputation: 785971

This job suites more to awk:

s='INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist'
awk -F' *= *' '{print $2}' <<< "$s"
MajorDomo/MajorDomo-Info.plist

If you really want grep then use grep -P:

grep -oP ' = \K.+' <<< "$s"
MajorDomo/MajorDomo-Info.plist

Upvotes: 3

Floris
Floris

Reputation: 46435

Not exactly what you were asking, but

echo "INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist" | sed 's/.*= \(.*\)$/\1/'

will do what you want.

Upvotes: 3

Related Questions