Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

for exmaple, using the command

cat foo.txt | xargs -I{} -n 1 -P 1 sh -c "echo {} | echo"

The foo.txt contains two lines

foo
bar

The above command print nothing.

share|improve this question
add comment

closed as unclear what you're asking by Dennis Williamson, cole, Dennis Kaarsemaker, Jenny D, freiheit Dec 24 '13 at 22:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

cat foo.txt | xargs -J % -n 1 sh -c "echo % | bar.sh" 

Tricky part is that xargs performs implicit subshell invocation. Here sh invoked explicitly and pipe not becomes the part of parent conveyor

share|improve this answer
1  
Thanks, I have updated my question to provide a more concret example. but it does not work as you suggested.. –  Yoga Dec 24 '13 at 9:13
1  
echo cant read from stdin, so piping to it has no sense. compare this: cat foo.bar | wc -l and cat foo.bar | xargs -J % -n 1 sh -c "echo % | wc -l" –  Kondybas Dec 24 '13 at 9:50
add comment

If you want to process all the lines of foo.txt you will have to use a loop. Use & to put the process to background

while read line; do
   echo $line | bar.sh &
done < foo.txt

If your input contain spaces temporarily set the internal field separator to the newline

# save the field separator
OLD_IFS=$IFS

# new field separator, the end of line 
IFS=$'\n'

for line in $(cat foo.txt) ; do
   echo $line | bar.sh &
done

# restore default field separator  
IFS=$OLD_IFS     
share|improve this answer
    
No-no, I'm stuck with it too. TS want to split file into seperate lines and feed them into script independently –  Kondybas Dec 24 '13 at 7:19
1  
I need to use xargs to parallel the process.. –  Yoga Dec 24 '13 at 7:21
    
OK :-) I just looked up the options of xargs the OP used. –  Matteo Dec 24 '13 at 7:25
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.