feeding a series of input args to a command that only takes one

This is a discussion on feeding a series of input args to a command that only takes one within the Linux General forums, part of the Linux Forums category; Sometimes I have commands that are designed only to process a single input argument but I want to call them ...


Go Back   Usenet Forums > Linux Forums > Linux General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-01-2008
Rahul
 
Posts: n/a
Default feeding a series of input args to a command that only takes one

Sometimes I have commands that are designed only to process a single
input argument but I want to call them repeatedly on a list.

For example I generate a command line pipe that outputs a list of ip
addresses:

cmd1 | cmd2 | tr '\n' '\t'

11.0.0.21 11.0.0.176 11.0.0.191

To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
<ip address>

Is there a command line metaphor to feed a command one arg at a time
repeatedly. I guess I could use a for loop but that's somewhat unweildly
on the command line.

--
Rahul
Reply With Quote
  #2 (permalink)  
Old 05-01-2008
Keith Keller
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

On 2008-05-01, Rahul <nospam@nospam.invalid> wrote:
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
><ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.


man xargs

--keith

--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

Reply With Quote
  #3 (permalink)  
Old 05-01-2008
Kees Theunissen
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

Rahul wrote:
> Sometimes I have commands that are designed only to process a single
> input argument but I want to call them repeatedly on a list.
>
> For example I generate a command line pipe that outputs a list of ip
> addresses:
>
> cmd1 | cmd2 | tr '\n' '\t'
>
> 11.0.0.21 11.0.0.176 11.0.0.191
>
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
> <ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.
>

Assuming your "cmd1 | cmd2" pipe above outputs a single IP address
per line, try:

cmd1 | cmd2 | xargs --max-args=1 ip2hex


Regards,

Kees.

--
Kees Theunissen
Reply With Quote
  #4 (permalink)  
Old 05-01-2008
Rahul
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

Kees Theunissen <theuniss@rijnh.nl> wrote in
news:481a2703$0$14343$e4fe514c@news.xs4all.nl:

> Assuming your "cmd1 | cmd2" pipe above outputs a single IP address
> per line, try:
>
> cmd1 | cmd2 | xargs --max-args=1 ip2hex


Thanks Kees and Keith. That's perfect. I had tried xargs before I posted
but did not realize the --max-args option was what I needed!

--
Rahul
Reply With Quote
  #5 (permalink)  
Old 05-01-2008
Janis Papanagnou
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

Rahul wrote:
> Sometimes I have commands that are designed only to process a single
> input argument but I want to call them repeatedly on a list.
>
> For example I generate a command line pipe that outputs a list of ip
> addresses:
>
> cmd1 | cmd2 | tr '\n' '\t'
>
> 11.0.0.21 11.0.0.176 11.0.0.191
>
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
> <ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.
>


xargs -n 1

See 'man xargs' for details.

Janis
Reply With Quote
  #6 (permalink)  
Old 05-01-2008
Chris Mattern
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

On 2008-05-01, Rahul <nospam@nospam.invalid> wrote:
> Sometimes I have commands that are designed only to process a single
> input argument but I want to call them repeatedly on a list.
>
> For example I generate a command line pipe that outputs a list of ip
> addresses:
>
> cmd1 | cmd2 | tr '\n' '\t'
>
> 11.0.0.21 11.0.0.176 11.0.0.191
>
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
><ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.
>


xargs has the -n flag to control how many arguments it uses at a time;
it spawns as many instances of the program as necessary to use up all
the arguments:

other_stuff | xargs -n 1 ip2hex

--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
Reply With Quote
  #7 (permalink)  
Old 05-01-2008
Bill Marcum
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

On 2008-05-01, Rahul <nospam@nospam.invalid> wrote:
>
>
> Sometimes I have commands that are designed only to process a single
> input argument but I want to call them repeatedly on a list.
>
> For example I generate a command line pipe that outputs a list of ip
> addresses:
>
> cmd1 | cmd2 | tr '\n' '\t'
>
> 11.0.0.21 11.0.0.176 11.0.0.191
>
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
><ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.
>

while read ip_address
Reply With Quote
  #8 (permalink)  
Old 05-02-2008
Icarus Sparry
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

On Thu, 01 May 2008 20:06:21 +0000, Rahul wrote:

> Sometimes I have commands that are designed only to process a single
> input argument but I want to call them repeatedly on a list.
>
> For example I generate a command line pipe that outputs a list of ip
> addresses:
>
> cmd1 | cmd2 | tr '\n' '\t'
>
> 11.0.0.21 11.0.0.176 11.0.0.191
>
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
> <ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.


cmd1 | cmd2 | while read ip ; do ip2hex $ip ; done

does not look too bad to me.
Reply With Quote
  #9 (permalink)  
Old 05-02-2008
OldSchool
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

On May 1, 4:06*pm, Rahul <nos...@nospam.invalid> wrote:
> Sometimes I have commands that are designed only to process a single
> input argument but I want to call them repeatedly on a list.
>
> For example I generate a command line pipe that outputs a list of ip
> addresses:
>
> cmd1 | cmd2 | tr '\n' '\t'
>
> 11.0.0.21 * * * 11.0.0.176 * * *11.0.0.191
>
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
> <ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.
>
> --
> Rahul


pipe your output thru xargs...

cmd1 | cmd2 | tr '\n' '\t' | xargs -l ip2hex
Reply With Quote
  #10 (permalink)  
Old 05-02-2008
Sashi
 
Posts: n/a
Default Re: feeding a series of input args to a command that only takes one

On May 1, 4:06 pm, Rahul <nos...@nospam.invalid> wrote:
> Sometimes I have commands that are designed only to process a single
> input argument but I want to call them repeatedly on a list.
>
> For example I generate a command line pipe that outputs a list of ip
> addresses:
>
> cmd1 | cmd2 | tr '\n' '\t'
>
> 11.0.0.21 11.0.0.176 11.0.0.191
>
> To convert to hex I have ip2hex that accepts only 1 arg. ip2hex: ip2hex
> <ip address>
>
> Is there a command line metaphor to feed a command one arg at a time
> repeatedly. I guess I could use a for loop but that's somewhat unweildly
> on the command line.
>
> --
> Rahul


You can use xargs -n1, which limits only one arg at a time. IIRC, this
is GNU xargs and other versions may not support it. Check it out.
Sashi
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:57 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0