view · edit · history · print

Geeking out with the xargs command

# i used this command often
... | xargs -r -I'{}' nslookup {}
# the reason for this is to tell nslookup where to place the output and loop over the nslookup command for each input (ip).


# i also used this command often
... | xargs grep
# the reason for this is that this shows me the filename
$ ls *router* | xargs --verbose grep "echo"
grep echo reload_router_3299 start_router start_router_3299 status_router status_router_3299 stop_router stop_router_3299
start_router:  echo " $x "
status_router:  echo " $x "


# but we can do better on the grep
... | grep -Hn "echo" file1 file2
status_router:4:  echo " $x "
status_router:8: echo "# Processes:"
# this shows file & line number (no need for xargs on that, but xargs is still useful for other reasons here).


# limit parameter/command strings (-s 35)
... | xargs --show-limits -s 35 -t grep "strings"
Your environment variables take up 2111 bytes
POSIX upper limit on argument length (this system): 2617281
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2615170
Size of command buffer we are actually using: 35
..
grep echo reload_router_3299
grep echo start_router
..
# but there are other interesting parameters up-there
# --verbose or -t     Print the command line on the standard error output before executing it.
# --show-limits       Display the limits on the command-line length which are imposed  by  the  operating  system


# limit parameters (-n 1)
... | xargs --show-limits -n 1 -t grep "echo"
Your environment variables take up 2111 bytes
POSIX upper limit on argument length (this system): 2617281
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2615170
Size of command buffer we are actually using: 131072
..
grep echo reload_router_3299
grep echo start_router
  echo " $x "
  echo "log in as saproute98 to start the other saprouter"
grep echo start_router_3299
grep echo status_router
  echo " $x "


# why limiting the parameters? speed!
# here we limit to 1 parameter (-n 1) and allow unlimited parallelisation (-P 0)
... | xargs --show-limits -n 1 -P 0 -t grep -Hn "echo"
Your environment variables take up 2111 bytes
POSIX upper limit on argument length (this system): 2617281
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2615170
Size of command buffer we are actually using: 131072
..
grep echo start_router
grep echo status_router
grep echo stop_router
  status_router:4:  echo " $x "
  stop_router:4:  echo " $x "
# use "grep -Hn" in combination with "xargs -n1 -P0" so you understand where the grep found it's results.


# and finally for complex insertion, you can simplify the parameter insert like this (use the same parameter 2 times in the echo command).
... | xargs -I% echo "__%__%__"

admin · attr · attach · edit · history · print
Page last modified on January 29, 2016, at 05:03 AM