user1098203
user1098203

Reputation: 1

Use each line of piped output as parameter for script

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

Upvotes: 0

Views: 441

Answers (3)

Alexis
Alexis

Reputation: 4499

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

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77095

May be this can help -

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

Upvotes: 2

Dark Falcon
Dark Falcon

Reputation: 44181

Please see xargs. For example:

myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//' | xargs -n 1 myscript.sh

Upvotes: 3

Related Questions