0

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

4
  • Pfff, nearly impossible to post here Commented Dec 14, 2011 at 16:24
  • Useless use of double sed. sed -e 's/^[^|]*//' | sed -e 's/|.*//' could be written as sed -e 's/^[^|]*//' -e 's/|.*//' or even sed -e 's/^[^|]*//; s/|.*//' Commented Dec 14, 2011 at 16:55
  • Yes but sed is already hard to read so the double sed was to make that easier. Thanks anyway Commented Dec 14, 2011 at 17:03
  • hm... "read spaces as pipe symbol, didn't let me post". what's wrong with pipe symbols? Commented Dec 14, 2011 at 17:05

3 Answers 3

3

Please see xargs. For example:

myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//' | xargs -n 1 myscript.sh
Sign up to request clarification or add additional context in comments.

3 Comments

Just found out that xargs indeed seems to be the way to go.
Just found out that xargs indeed seems to be the way to go. Thanks for answering, very helpfull
Just a note, I feel that you should replace both sed with a single cut: cut -f 2 -d '|'
2

May be this can help -

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.