1

I need to extract path from a string. I found examples in another post, but missing additional steps.

I have a string as below:

title="test test good dskgkdh hdfyr /rlsmodules/svnrepo/SOURCE/CBL/MQ/BASELINE/MQO000.CBL kdlkfg nsfgf trhrnrt"
cobsrc=$(awk '{match($0,/\/[^"]*/,a);print a[0]}' <<< $title)
echo $cobsrc

Output is

/rlsmodules/svnrepo/SOURCE/CBL/MQ/BASELINE/MQO000.CBL kdlkfg nsfgf trhrnrt

I need only

/rlsmodules/svnrepo/SOURCE/CBL/MQ/BASELINE/MQO000.CBL

What modification is required?

An existing post on similar query: how to extract path from string in shell script

2
  • 2
    Are you saying you want the first substring that starts with the first forward-slash after a space and ends at the next whitespace ? Any particular reason you want/need to use awk for this? Commented Aug 14, 2018 at 2:10
  • 1
    @Dwija, good that you showed us nice sample of input, output with your effort too, give it sometime and try to select any of the answer(s) as correct answer to close the thread completely, cheers and happy learning. Commented Aug 14, 2018 at 2:59

2 Answers 2

6

Four solutions, in order of my own preference.

First option would be simple parameter expansion, in two steps:

$ title="/${title#*/}"
$ title="${title%% *}"
$ echo "$title"
/rlsmodules/svnrepo/SOURCE/CBL/MQ/BASELINE/MQO000.CBL

The first line removes everything up to the first slash (while prepending a slash to replace the one that's stripped", the second line removes everything from the first bit of whitespace that remains.

Or, if you prefer, use a regex:

$ [[ $title =~ ^[^/]*(/[^ ]+)\  ]]
$ echo ${BASH_REMATCH[1]}
/rlsmodules/svnrepo/SOURCE/CBL/MQ/BASELINE/MQO000.CBL

The regex translates as:

  • null at the beginning of the line,
  • a run of zero or more non-slashes,
  • an atom:
    • a slash followed by non-space characters
  • a space, to end the previous atom.

The $BASH_REMATCH array contains the content of the bracketed atom.

Next option might be grep -o:

$ grep -o '/[^ ]*' <<<"$title"

(Result redacted -- you know what it'll be.)

You could of course assign this output to a variable using command substitution, which you already know about.

Last option is another external tool...

$ sed 's:^[^/]*::;s/ .*//' <<<"$title"

This is the same functionality as is handled by the parameter expansion (at the top of the answer) only in a sed script, which requires a call to an external program. Included only for pedantry. :)

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

1 Comment

Evey solutiuon you provided is working and I am spoiled by choice. It seems like a lot to learn.
2

Could you please try following.

echo "$title" | awk 'match($0,/\/.*\/[^ ]*/){print substr($0,RSTART,RLENGTH)}'

Output will be as follows.

/rlsmodules/svnrepo/SOURCE/CBL/MQ/BASELINE/MQO000.CBL

Solution 2nd: Considering that your variable don't have space in between its value then following may help you too.

echo "$title" | awk '{sub(/[^/]* /,"");sub(/ .*/,"")} 1'

1 Comment

Thank you very much for your assistance. I have a lot to learn.

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.