2

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?

2
  • Are you saying "I want everything after the equals sign"? You need sed, not grep for that - editing the line (not just selecting it). Commented Apr 4, 2014 at 19:47
  • Yes. Everything after the equals sign. Commented Apr 4, 2014 at 19:48

5 Answers 5

3

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
Sign up to request clarification or add additional context in comments.

16 Comments

You could make it slightly a question about bash by using awk ... <<<"$s" rather than echo "$s" | awk. :)
...it's also worth noting that grep -P is very, very nonstandard, and not available on several widely-used platforms (including even some Linux distros using GNU grep, since it's a compile-time option not every distro turns on).
Both valid points, I would prefer awk above and over grep any day.
In my opinion, of all the built in text processing commands, awk is the power tool. I thought in this case it might be a slight overkill, but it needs to be in everyone's toolkit. But for this task I gravitate to sed. Maybe because I learnt it first?
Use awk -F'^[^=]*= *' '{print $2}' in that case.
|
3

Not exactly what you were asking, but

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

will do what you want.

5 Comments

Thank you! Althogh, it is still printing out the full output form echo(INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist).
Might need sed -r to enable ERE support.
@CharlesDuffy it might be that my (OSX) version of sed is slightly different. For example, it needs -E not -r for ERE. But yes, those difference can be really annoying in what is supposed to be a standard tool.
Or just use sed 's/[^=]\+=\s*//' and there's no need for the capture.
@Emmet - that's a really good and simple alternative. <headslap>.
3

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/^[^=]*= *//')

3 Comments

Output will have a space also: <space>MajorDomo/MajorDomo-Info.plist
Still doesn't work for echo 'INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist'|cut -d = -f 2- | cut -d ' ' -f 2- case.
Actually, after giving it some thought, based on the OP example, that is a legitimate case and works as intended. If you have more than one space after =, you probably only want to trim the first one, as the subsequent ones are likely part of the filename.
2

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

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

2 Comments

+1. Only thing you might change here is stripping leading whitespace, since the example is using key = value rather than key=value.
Thanks, edited. Also quoted it in case multiple or trailing spaces need to be preserved.
0

Bash

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.