Portál AbcLinuxu, 9. května 2025 03:07
$ ls | while read f; do echo "aaa $f bbb"; done aaa subor1 bbb aaa subor2 bbb aaa subor3 bbbA zaujimalo by ma ci sa to iste da dosiahnut s xargs:
$ ls | xargs -n1 echo "aaa bbb" aaa bbb subor1 aaa bbb subor2 <-- toto neni co chcem aaa bbb subor3Proste by som potreboval ci je nejaky "placeholder" ktory by xargs-u povedal kam ma argument vlozit. V podstate by som potreboval nieco ako $1 u awk:
$ ls | awk '{ print "aaa "$1" bbb" } aaa subor1 bbb aaa subor2 bbb aaa subor3 bbbTu pouzivam echo len na ukazku, v reale je miesto echo pouzity nejaky iny prikaz, takze nemozem awk pouzit.
Řešení dotazu:
xargs -n1 -I SOUBOR echo aaa SOUBOR bbb
ls | xargs -n1 -i{} echo "aaa {} bbb"
-I replace-str Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1. --replace[=replace-str] -i[replace-str] This option is a synonym for -Ireplace-str if replace-str is specified. If the replace-str argument is missing, the effect is the same as -I{}. This option is deprecated; use -I instead.
ls | xargs printf 'aaa %b bbb\n'
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.