shell - bash script get substring from string -


i need part of string depending on value long string (for example output 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 following subtring after -dhome= :

/home/user/xxx 

position of "-dhome=" not fixed, anywhere in long_string.

i using following command:

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

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

it work when "-cccc=c" netx "-dhome=" :-(

how can path after "-dhome=" not depend on next values e.g. "-cccc=c" ???

is there more effective way or regexp same output ?

thanks m.gi

awk , cut can 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 input on "dhome=" , prints second part.
cut splits on spaces , gets first part. obviously, not work correctly if name of folder contains spaces. usually, home directories don't have spaces in names.


Comments

Popular posts from this blog

PHP while loop dynamic rowspan -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

Spring Boot + JPA + Hibernate: Unable to locate persister -