使用sed或awk获取标记之间的路径

时间:2011-11-10 12:05:31

标签: parsing sed awk

我想从ps输出中获取路径(/ tmp / deployment / deployment / hostVBox_8080_GSA /):

username@hostVBox:~$ps ax |grep jboss
16291 pts/4    Sl     0:34 java -Dprogram.name=run.sh -Xms128m -Xmx512m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.endorsed.dirs=/tmp/deployment/deployment/hostVBox_8080_GSA/lib/endorsed -classpath /tmp/deployment/deployment/hostVBox_8080_GSA/bin/run.jar:/lib/tools.jar org.jboss.Main

我该怎么做?

BR Kolesar

3 个答案:

答案 0 :(得分:1)

这可能对您有用:

echo "a bunch of stuff ... -classpath /tmp/deployment/deployment/hostVBox_8080_GSA/bin/run.jar:/lib/tools.jar org.jboss.Main' |
sed -e 's#.*-classpath \(\([^/]*/\)*\)bin/run.jar.*#\1#'
/tmp/deployment/deployment/hostVBox_8080_GSA/

答案 1 :(得分:0)

尝试:

命令:

awk -F"dirs=" '
    /java/{
        a=$2
        split(a,path," -")
        sub(/[^/]*\/[^/]*$/,"",path[1])
        print path[1]
    }'

答案 2 :(得分:0)

试试这个,看看它是否有效:

ps ax |grep jboss|awk -F'java.endorsed.dirs=' '{gsub(/lib.*/,"",$2);print $2;exit;}'