0

I need get a part of string depending on some value from a long string (for example some output from ps -ef).

for example source string:

long_string1="cmd ... -aaaa=A -bbbb=B -zzzz=Z -DHOME=/home/user/xxx -cccc=C ... etc."

or

long_string2="cmd ... -aaaa=A -DHOME=/home/user/xxx -bbbb=B -zzzz=Z -cccc=C ... etc."

I want extract or get following subtring only after -DHOME= :

/home/user/xxx

Position of "-DHOME=" is not fixed, it could be anywhere in long_string.

I am using following command:

echo $long_string1|sed -e 's/.*-DHOME=//g;s/ -cccc=C.*//g'

it works fine for $long_string1 but it doesn't work for $long_string2.

It will work only when "-cccc=C" will be netx to "-DHOME=" :-(

How can I get path after "-DHOME=" but not depend on next values e.g. "-cccc=C" ???

Is there any more effective way or regexp to get same output ?

Thanks M.Gi

5
  • OR, once you have removed stuff before -DHOME=, search for the first space and delete everything after that? i.e. s/ .*$//'? Good luck. Commented Aug 8, 2016 at 13:53
  • does your shell support var="abc def" ; echo ${var/ *} ? Commented Aug 8, 2016 at 14:22
  • Also, in the future, this is why you see constant streams of comments show us your code . Don't make people guess what you are doing ;-) Commented Aug 8, 2016 at 14:23
  • thank you @shellter both s/ .*$//' and var="abc def" ; echo ${var/ *} working fine, but I will run it on Linux,HPUX,AIX,Sun ... Commented Aug 8, 2016 at 16:25
  • bash is bash on all OS's (excepting those old-line unixen probably have ver 2 installed by default ;-( ) . Again, these are more "requirements" that should have been in your main Q. Good luck. Commented Aug 8, 2016 at 17:20

2 Answers 2

1

awk and cut can help you.

#!/bin/bash
get_home() { awk -F "DHOME=" '{print $2}' <<< "${long_string}" | cut -d ' ' -f1; }

long_string="cmd ... -aaaa=A -bbbb=B -zzzz=Z -DHOME=/home/user/xxx -cccc=C ... etc."
get_home
long_string="cmd ... -aaaa=A -DHOME=/home/user/xxx -bbbb=B -zzzz=Z -cccc=C ... etc."
get_home
long_string="cmd ... -aaaa=A -DHOME=/home/user/xxx"
get_home

Output

/home/user/xxx
/home/user/xxx
/home/user/xxx

awk splits the input on "DHOME=" and prints the second part.
cut then splits on spaces and gets the first part. Obviously, this will not work correctly if the name of the folder contains spaces. Usually, home directories don't have spaces in their names.

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

1 Comment

Good idea about using DHOME= as the FS, but you don't need to pipe to cut, i.e. awk -F 'DHOME=' '{line=$2 ; split(line, arr, " "); print arr[1]} <<< "$long_string" will work too. Good luck to all.
1

Solution in pure-bash style, credits to shelter for suggestions.

#!/bin/bash

long_string="cmd ... -aaaa=A -bbbb=B -zzzz=Z -DHOME=/home/user/xxx -cccc=C ... etc."

tempString1="${long_string##*-DHOME=}"  # Strips off everything upto '-DHOME='
tempString2="${tempString1/ *}"  # Strips off everything until first whitespace

echo "$tempString2"

Running it in console:-

$ ./myScript.sh
/home/user/xxx

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.