1

How can we fetch a substring from a string in bash using scripting language?

Example:

fullstring="mnuLOCNMOD.URL = javascript:parent.doC...something"

The substring I want is everything before ".URL" in the full string.

6 Answers 6

5

With Parameter Expansion, you can do:

fullstring="mnuLOCNMOD.URL = javascript:parent.doC...something"
echo ${fullstring%\.URL*}

prints:

mnuLOCNMOD
Sign up to request clarification or add additional context in comments.

7 Comments

I like this solution.
how shall I use this in .sh file
@shraddhaagrawal If you mean, how to assign it then, you can do: substring=${fullstring%\.URL*}
it is not working also with substring=echo ${fullstring%\.URL*} it is not working in my .sh file otherwise it is working in bash shell
Please double-quote the variable reference to promote good habits. While \. to produce a literal . does work in this case, it is not necessary and can mislead people to think that . is special in a parameter expansion, which it isn't. The suffix to strip is a pattern, not a regex; thus: echo "${fullstring%.URL*}" will do. In Bash, the conceptually clearest formulation would be to selectively single-quote the parts to be taken literally: echo "${fullstring%'.URL'*}"
|
1
$ fullstring="mnuLOCNMOD.URL = javascript:parent.doC...something"
$ sed -r 's/^(.*)\.URL.*$/\1/g' <<< "$fullstring"
mnuLOCNMOD
$

Comments

0

You can use grep:

echo "mnuLOCNMOD.URL = javas" | grep -oP '\w+(?=\.URL)'

and assign the result to a string. I used a positive lookahead (?=regex) because it's a zero length assertion, meaning that it'll be matched but won't be displayed.

Run grep --help to find out what o and P flags stand for.

4 Comments

Usage: grep -hblcnsviw pattern file . . .
-o and P are not supported
@shraddhaagrawal are not supported where?
@shraddhaagrawal Are you on a Solaris system?
0

Parameter Expansion is the way to go.

If you are interested in a simple grep:

% fullstring="mnuLOCNMOD.URL = javascript:parent.doC...something"

% grep -o '^[^.]*' <<<"$fullstring"
mnuLOCNMOD

Comments

0
fullstring="mnuLOCNMOD.URL = javascript:parent.doC...something"
menuID=`echo $fullstring | cut -f 1 -d '.'`

here I used dot as a separator this works in .sh files

1 Comment

Yes, this is POSIX-compliant, but it is also far less restrictive than what you asked for; if you really wanted to match before .URL specifically (and ignore other . instances), this would not work. If you only care about the first ., whatever it is followed by, you should have phrased your question like that.
0

To offer yet another alternative: Bash's regular-expression matching operator, =~:

fullstring="mnuLOCNMOD.URL = javascript:parent.doC...something"
echo "$([[ $fullstring =~ ^(.*)'.URL' ]] && echo "${BASH_REMATCH[1]}")"

Note how the (one and only) capture group ((.*)) is reported through element 1 of the special "${BASH_REMATCH[@]}" array variable.

While in this case l3x's parameter expansion solution is simpler, =~ generally offers more flexibility.


awk offers an easy solution as well:

echo "$(awk -F'\\.URL' '{ print $1 }' <<<"$fullstring")"

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.