问题描述:

I have an application (myapp) that gives me a multiline output

result:

abc|myparam1|def

ghi|myparam2|jkl

mno|myparam3|pqr

stu|myparam4|vwx

With grep and sed I can get my parameters as below

myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//'

But then want these myparamx values as paramaters of a script to be executed for each parameter.

myscript.sh myparam1

myscript.sh myparam2

etc.

Any help greatly appreciated

网友答案:

Please see xargs. For example:

myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//' | xargs -n 1 myscript.sh
网友答案:

May be this can help -

myapp | awk -F"|" '{ print $2 }' | while read -r line; do /path/to/script/ "$line"; done
网友答案:

I like the xargs -n 1 solution from Dark Falcon, and while read is a classical tool for such kind of things, but just for completeness:

myapp | awk -F'|' '{print "myscript.sh", $2}' | bash

As a side note, speaking about extraction of 2nd field, you could use cut:

myapp | cut -d'|' -f 1 # -f 1 => second field, starting from 0
相关阅读:
Top