2

I have a string,

tester_one="update set_tables set abc=7 where bcd=9"

Here I wish to extract only the part between "set" and "where",

abc=7

I tried a couple of Unix commands, but it picked up any occurrences of set or where encountered, before the part where I want it to pick up.

I have an idea on how to do it Java but I am lost in Unix as I am new to this.

5 Answers 5

5

Using sed

$ echo "$tester_one" | sed -E 's/.*set (.*) where.*/\1/'
abc=7

To capture it in a variable:

$ new=$(echo "$tester_one" | sed -E 's/.*set (.*) where.*/\1/')
$ echo $new
abc=7

Using awk

$ echo "$tester_one" | awk '{sub(/.*set /,""); sub(/ where.*/,""); print;}'
abc=7

Using grep -P

If your grep supports the -P (perl-like) option:

$ echo "$tester_one" | grep -oP '(?<=set ).*(?= where)'
abc=7 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @John1024. All of them work!! Just a doubt, are you matching the pattern b/w "set " and " where"?
@Noobie Yes, they all return text between set and ` where`.
Would all these commands still work still work if i replace "set " with " set " as I am assuming for a query like "update abc_set set abc=7 where bcd=9", the line might get parsed wrongly
@Noobie That would be fine: all the commands would work if you add that space.
1

You can get it out with sed. Something like:

echo "$tester_one" | sed 's/.* set \(.*\) where .*/\1/'

3 Comments

Thanks @viraptor!! It works like a Charm!! Might be too much to ask....Could you please explain how this works?
The (.*) catches everything between, and it will be placed in variable \1 (default sed behavior). He then proceeds to substitute the entire input with the \1 variable.
Thanks for this explanation @Liviu Chircu
1

Using Bash Pattern Matching:

#!/bin/bash
tester_one="update set_tables set abc=7 where bcd=9"
pat=".* set (.*) where"
[[ $tester_one =~ $pat ]]
echo "${BASH_REMATCH[1]}"

1 Comment

I always forget that bash can do this. Thanks for the reminder. :)
1

You can also use set and where as field separators and print the field that lies in between them:

$ awk -F"set | where" '{print $2}' <<< "update set_tables set abc=7 where bcd=9"
abc=7

Comments

0

As with your other question, this can be achieved in pure bash, without the use of external tools like sed/awk/grep.

#!/usr/bin/env bash

tester_one="update set_tables set abc=7 where bcd=9"

output="${tester_one#* set }"
output="${output% where }"

echo "$output"

Note the spaces around "set" and "where" in the parameter expansion lines. As you might expect, you'll need to be careful with this if the $tester_one variable contains the distinct "set" or "where" in places that you don't expect them.

That said, I like Jahid's answer better. :-)

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.