Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-13-2013, 05:02 AM   #1
Registered: Sep 2009
Posts: 60

Rep: Reputation: 15
make run command for each argument separately

I need smth like xargs but don't run one instance for all arguments but rather each instance for each argument. I.e.:

echo "a b c" | xargs echo x:
x: a b c
but i need rather
x: a x: b x: c
Old 01-13-2013, 07:20 AM   #2
Senior Member
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 3,128

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
I think you could google "awk one liners," and you'd find a solution.
Old 01-13-2013, 07:46 AM   #3
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Try this

Can you try:-
echo "a b c" | awk 'BEGIN{ORS=" "}; END{for(i=1;i<=NF;i++) print "x: " $i}' && echo

Last edited by shivaa; 01-13-2013 at 07:52 AM. Reason: gawk replaced with simple awk
Old 01-13-2013, 10:39 AM   #4
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,240

Rep: Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406
The -n option for xargs limits how many arguments it uses at a time:
~/tmp$ echo a b c | xargs -n1 echo x:
x: a
x: b
x: c
@shivaa: why use "END" in your awk code? I'm kind of surprised it works, I guess it uses the values of NF and $i from the last line...
Old 01-14-2013, 10:35 AM   #5
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,713

Rep: Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279
Wouldn't it be simpler:
for i in a b c 
  echo x $i


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a way to make a generic command run recursivley? dsollen Linux - Newbie 6 11-17-2009 02:36 PM
How to Make a command run at stratup custangro Linux - Newbie 1 03-03-2007 12:11 PM
bash script: run multiple jobs in bg, & kill them separately? mattengland Programming 1 03-26-2006 06:07 PM
make a command run at startup? cheater1034 Linux - General 2 09-12-2004 06:51 AM
where do i add a command to make it run on boot stuart Linux - Newbie 4 06-21-2004 07:03 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration