Reputation: 33
I would like to use xargs to list the contents of some files based on the output of command A
. Xargs replace-str seem to be adding a space to the end and causing the command to fail. Any suggestions? I know this can be worked around using for loop. But curious to know how to do this using xargs.
lsscsi |awk -F\/ '/ATA/ {print $NF}' | xargs -L 1 -I % cat /sys/block/%/queue/scheduler
cat: /sys/block/sda /queue/scheduler: No such file or directory
Upvotes: 1
Views: 2132
Reputation: 439228
The problem is not with xargs -I
, which does not append a space to each argument, which can be verified as follows:
$ echo 'sda' | xargs -I % echo '[%]'
[sda]
Incidentally, specifying -L 1
in addition to -I
is pointless: -I
implies line-by-line processing.
Therefore, it must be the output from the command that provides input to xargs
that contains the trailing space.
You can adapt your awk
command to fix that:
lsscsi |
awk -F/ '/ATA/ {sub(/ $/,"", $NF); print $NF}' |
xargs -I % cat '/sys/block/%/queue/scheduler'
sub(/ $/,"", $NF)
replaces a trailing space in field $NF
with the empty string, thereby effectively removing it. cat
's argument so as to make it work even with filenames with spaces.Upvotes: 2
Reputation: 537
lsscsi |awk -F\/ '/ATA/ {print $NF}'| awk '{print $NF}' | xargs -L 1 -I % cat /sys/block/%/queue/scheduler
The first awk stmt splits by "/" so anything else is considered as field. In this is case "sda "
becomes whole field including a space at the end. But by default, awk removes space . So after the pipe, the second awk prints $NF (which is last word of the line) and leaves out " " space as delimiter. awk { print $1 } will do the same because we have only one word, "sda" which is both first and last.
Upvotes: 0