views:

196

answers:

3

I have a command which lists Weblogic instances directories on a server.I want to display contents of a file in the parent directory of each directory listed.

An additional feature would be to display the name of the file in addition to displaying the contents

/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed -e 's/weblogic.policy//' -e 's/security\///' -e 's/dep\///' | sort

The output of the above command looks like this

/opt/<some_directory>/<domain_name>/<app_name>/
/opt/<some_directory>/<domain_name>/<app_name>/

I want to see the contents of somefile.cf files in the /opt/<some_directory>/<domain_name>/ directories i.e. /opt/<some_directory>/<domain_name>/somefile.cf

To put it simply, I need a sed regex to replace the last directory name from the output.

I tried this as well

/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed -e 's/weblogic.policy//' -e 's/security\///' -e 's/dep\///' | sort | while read -r DIR ; do dirname $DIR ; done | uniq | sed 's/$/\/somefile\.cf/g'

but I am sure the while part and the last sed part can be done away easily using a simple sed command like this

sed -e 's/<regex_for_last_directory_name>/somefile.cf/'
+2  A: 
/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed -e 's/weblogic.policy//' -e 's/security\///' | sort | while read -r DIR
do
  cat "$DIR"/somefile.cf
done

or do you mean the directory one level up??

/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed -e 's/weblogic.policy//' -e 's/security\///' | sort | while read -r DIR
do
  cat "$DIR"/../somefile.cf
done
ghostdog74
What's wrong with $DIR/../somefile.cf ?
Mark Edgar
nothing wrong. just slipped my mind that's all :)
ghostdog74
while read -r DIR ; do DIR=${DIR%*\/} DIR=${DIR%\/*} cat "$DIR"/somefile.cf ; donethrew an error butwhile read -r DIR ; do cat "$DIR"/../somefile.cf ; doneworked.Thanks.Any idea if xargs could also work for this?
Sharjeel Sayed
A: 

cat /usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed -e 's/weblogic.policy//' -e 's/security\///' -e 's/$/somefile.cf/' | sort | uniq

Chris Hansen
Two problems with you answer.1) Syntactically wrong : I get an error cat: auwwx: No such file or directory2) somefile.cf is in the parent directory of the listed directories. sed -e 's/$/somefile.cf/' would append somefile.cf in the directories list and not in the parent directories of the output.
Sharjeel Sayed
A: 

works on 8, may need tweaked for 10.2

cat $(/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed -e 's/weblogic.policy//' -e 's/security\///' -e 's/$/somefile.cf/' | sort | uniq)

Chris Hansen
Where did you place the backticks exactly in you earlier answer.I will try with backticks.Also even if it did work, it would still not find somefile.cf in the parent directory.Your current answer does not work for Weblogic 10.The example I gave in my question works for Weblogic 8 ,9 and 10.Hence I am using that
Sharjeel Sayed
I tweaked your answer to make it more compact and it works for Weblogic 8 and 9cat $(/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed -e 's/weblogic.policy/\.\.\/somefile\.cf/' -e 's/security\///' -e 's/dep\///' | sort)It dint work for weblogic 10 because the <app_name> is not a directory in weblogic 10
Sharjeel Sayed