LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   xargs take string as single argument? (https://www.linuxquestions.org/questions/linux-software-2/xargs-take-string-as-single-argument-906260/)

FireRaven 10-03-2011 06:22 PM

xargs take string as single argument?
 
Hi, does anyone know if it's possible for xargs to take it's stdin and create 1 single parameter for it.

Example I want this python script's first argument to say 'hello world' like this:
Code:

$ python -c 'import sys;print sys.argv[1]' 'hello world'
hello world

If however I pipe a 'hello world' string to xargs I only get the 'hello' as the first argument and 'world' as the second, but I want them both as the 1st argument:
Code:

$ echo 'hello world' | xargs python -c 'import sys;print sys.argv[1]'
hello
$ echo 'hello world' | xargs python -c 'import sys;print sys.argv[2]'
world

Is there a way for xargs to take a literal input and use just as the first parameter. The idea is to pipe long messages (from emails etc) to the first parameter of a program, so treading all special characters as literals from xargs is what I want.

corp769 10-03-2011 06:40 PM

Hello,

Would one of the following work?
Code:

echo "\"hello world\"" | xargs python -c 'import sys;print sys.argv[2]'

echo "'hello world'" | xargs python -c 'import sys;print sys.argv[2]'

Cheers,

Josh

FireRaven 10-03-2011 08:23 PM

Both of those work, but I'm getting my input not from the echo command and the input varies, so quoting is not an option.
The input will be something like:
Code:

$ cat emailmessage | xargs python -c 'import sys;print sys.argv[1]'

corp769 10-03-2011 08:30 PM

What you could do is create a bash script to cat whatever file you need to a single variable, and then try to pass the variable over to the xargs python part. I'm not on linux right now, so I can't test this and mess around with it for you.

ntubski 10-03-2011 08:48 PM

xargs can use NUL as an argument separator:
Code:

{ echo 'hello world'; echo -ne '\0' } | xargs -0 python -c 'import sys;print sys.argv[1]'

FireRaven 10-03-2011 09:09 PM

Quote:

Originally Posted by ntubski (Post 4489283)
xargs can use NUL as an argument separator:
Code:

{ echo 'hello world'; echo -ne '\0' } | xargs -0 python -c 'import sys;print sys.argv[1]'

That works and will probably do for what I need.
Thanks guys.

crts 10-03-2011 09:35 PM

alternative
 
Hi,

just another alternative:
Code:

echo 'hello world' | xargs -I{} python -c 'import sys;print sys.argv[1]' '{}'

tange 10-10-2011 08:36 AM

Use GNU Parallel:
Code:

$ echo 'hello world' | parallel python -c 'import sys;print sys.argv[1]'
Watch the intro videos to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ


All times are GMT -5. The time now is 12:12 PM.