0

I Have a single line as follows, I need all the words between select and Done

 vertical on; select blah blah blah contains all special characters including /*?&;  Done
  • Is this in a pipeline? A file? A variable? – Jeff Schaller Jan 29 at 16:20
  • could you have something select blah selection foo Done? or something else is done and Done? – Jeff Schaller Jan 29 at 16:45
  • @jeff compelte line is stored in variable. its solved now. tq – sachin_ghagare Jan 29 at 18:07
0

Using bash regular expression would give (assuming the line in a variable):

$ line="vertical on; select blah blah blah contains all special characters including /*?&;  Done"
$ [[ "$line" =~ select(.*)Done ]] && echo ${BASH_REMATCH[1]}
blah blah blah contains all special characters including /*?&;
0

Try this,

 sed -e 's/.*select//;s/Done.*//'
0

Try

sed -e 's/select\(.*\)Done/\1/'
0

Using Perl-compatible regular expressions (PCRE):

(?<=word1).*?(?=word2)

Applying the above pattern with GNU grep:

$ grep -Po '(?<=select).*?(?=Done)' <<< ' vertical on; select blah blah blah contains all special characters including /*?&;  Done'
 blah blah blah contains all special characters including /*?&;  

Explanation

  • (?<=word1): Match word1 before the current position but don't include it in the result.
  • .*?: Match any string.
  • (?=word2): Match word2 after the current position but don't include it in the result.

The (?<=word1) and (?=word2) patterns are collectively known as lookaround. This feature is not supported by POSIX engines (BRE and ERE) so you need a more powerful engine (such as PCRE).

  • thank you nxnev, little correction in my question. whole line is stored in variable. how to pass variable to it ....its in shell scripting i need it. thank you in advance – sachin_ghagare Jan 29 at 16:43
  • @sachin_ghagare All you need is to pass the content of that variable to grep's standard input. With Bash: grep -Po '(?<=select).*?(?=Done)' <<< "${var}" (note that there will be a trailing newline). With any other POSIX shell (Bash included): printf '%s' "${var}" | grep -Po '(?<=select).*?(?=Done)' (there's no trailing newline by default, but you can add it with \n in the first argument of printf). – nxnev Jan 29 at 16:56
  • @sachin_ghagare Also, if this answer solved your problem, please mark it as accepted by clicking the check mark beside the answer. Or wait to see if another answer better solves the problem. Whatever works for you. – nxnev Jan 29 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.