LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-13-2013, 04:02 AM   #1
icegood
Member
 
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.:

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

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
I think you could google "awk one liners," and you'd find a solution.
 
Old 01-13-2013, 06:46 AM   #3
shivaa
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:-
Code:
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 06:52 AM. Reason: gawk replaced with simple awk
 
Old 01-13-2013, 09:39 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,132

Rep: Reputation: 1318Reputation: 1318Reputation: 1318Reputation: 1318Reputation: 1318Reputation: 1318Reputation: 1318Reputation: 1318Reputation: 1318Reputation: 1318
The -n option for xargs limits how many arguments it uses at a time:
Code:
~/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, 09:35 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Wouldn't it be simpler:
Code:
for i in a b c 
do  
  echo x $i
done
 
  


Reply


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 01:36 PM
How to Make a command run at stratup custangro Linux - Newbie 1 03-03-2007 11:11 AM
bash script: run multiple jobs in bg, & kill them separately? mattengland Programming 1 03-26-2006 05:07 PM
make a command run at startup? cheater1034 Linux - General 2 09-12-2004 05:51 AM
where do i add a command to make it run on boot stuart Linux - Newbie 4 06-21-2004 06:03 PM


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

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