Reputation: 28222
How do I cut everything starting with a given multi-character string using a common shell command?
e.g., given:
foo+=bar
I want:
foo
i.e., cut everything starting with +=
cut
doesn't work because it only takes a single-character delimiter, not a multi-character string:
$ echo 'foo+=bar' | cut -d '+=' -f 1
cut: bad delimiter
If I can't use cut
, I would consider using perl
instead, or if there's another shell command that is more commonly installed.
Upvotes: 1
Views: 1047
Reputation: 786291
cut
only allows single character delimiter.
You may use bash
string manipulation:
s='foo+=bar'
echo "${s%%+=*}"
foo
or use more powerful awk
:
awk -F '\\+=' '{print $1}' <<< "$s"
foo
'\\+='
is a regex that matches +
followed by =
character.
Upvotes: 3
Reputation: 662
You can use 'sed' command to do this:
string='foo+=bar'
echo ${string} | sed 's/+=.*//g'
foo
or if you're using Bash shell, then use the below parameter expansion (recommended) since it doesn't create unnecessary pipeline and another sed process and so is efficient:
echo ${string%%\+\=*}
or
echo ${string%%[+][=]*}
Upvotes: 1