So che non sto rispondendo xargsdirettamente alla domanda, ma vale la pena menzionare findl' -execopzione.
Dato il seguente file system:
[root@localhost bokeh]# tree --charset assci bands
bands
|-- Dream\ Theater
|-- King's\ X
|-- Megadeth
`-- Rush
0 directories, 4 files
Il comando find può essere fatto per gestire lo spazio in Dream Theater e King's X. Quindi, per trovare i batteristi di ogni band usando grep:
[root@localhost]# find bands/ -type f -exec grep Drums {} +
bands/Dream Theater:Drums:Mike Mangini
bands/Rush:Drums: Neil Peart
bands/King's X:Drums:Jerry Gaskill
bands/Megadeth:Drums:Dirk Verbeuren
Nella -execscelta {}sta per il nome del file incluso il percorso. Nota che non devi scappare o metterlo tra virgolette.
La differenza tra -execi terminatori ( +e \;) è che +raggruppa quanti più nomi di file è possibile su una riga di comando. Considerando \;che eseguirà il comando per ogni nome di file.
Quindi, find bands/ -type f -exec grep Drums {} +comporterà:
grep Drums "bands/Dream Theater" "bands/Rush" "bands/King's X" "bands/Megadeth"
e find bands/ -type f -exec grep Drums {} \;comporterà:
grep Drums "bands/Dream Theater"
grep Drums "bands/Rush"
grep Drums "bands/King's X"
grep Drums "bands/Megadeth"
In questo caso grepha l'effetto collaterale di stampare il nome del file o meno.
[root@localhost bokeh]# find bands/ -type f -exec grep Drums {} \;
Drums:Mike Mangini
Drums: Neil Peart
Drums:Jerry Gaskill
Drums:Dirk Verbeuren
[root@localhost bokeh]# find bands/ -type f -exec grep Drums {} +
bands/Dream Theater:Drums:Mike Mangini
bands/Rush:Drums: Neil Peart
bands/King's X:Drums:Jerry Gaskill
bands/Megadeth:Drums:Dirk Verbeuren
Naturalmente, greple opzioni -he -Hcontrolleranno se il nome del file viene stampato o meno indipendentemente da come grepviene chiamato.
xargs
xargs può anche controllare come sono i file man sulla riga di comando.
xargsper impostazione predefinita raggruppa tutti gli argomenti su una riga. Per fare la stessa cosa che -exec \;usa xargs -l. Si noti che l' -topzione dice xargsdi stampare il comando prima di eseguirlo.
[root@localhost bokeh]# find ./bands -type f | xargs -d '\n' -l -t grep Drums
grep Drums ./bands/Dream Theater
Drums:Mike Mangini
grep Drums ./bands/Rush
Drums: Neil Peart
grep Drums ./bands/King's X
Drums:Jerry Gaskill
grep Drums ./bands/Megadeth
Drums:Dirk Verbeuren
Vedi che l' -lopzione dice a xargs di eseguire grep per ogni nome di file.
Contro il valore predefinito (ovvero nessuna -lopzione):
[root@localhost bokeh]# find ./bands -type f | xargs -d '\n' -t grep Drums
grep Drums ./bands/Dream Theater ./bands/Rush ./bands/King's X ./bands/Megadeth
./bands/Dream Theater:Drums:Mike Mangini
./bands/Rush:Drums: Neil Peart
./bands/King's X:Drums:Jerry Gaskill
./bands/Megadeth:Drums:Dirk Verbeuren
xargsha un migliore controllo su quanti file possono essere presenti nella riga di comando. Dai -lall'opzione il numero massimo di file per comando.
[root@localhost bokeh]# find ./bands -type f | xargs -d '\n' -l2 -t grep Drums
grep Drums ./bands/Dream Theater ./bands/Rush
./bands/Dream Theater:Drums:Mike Mangini
./bands/Rush:Drums: Neil Peart
grep Drums ./bands/King's X ./bands/Megadeth
./bands/King's X:Drums:Jerry Gaskill
./bands/Megadeth:Drums:Dirk Verbeuren
[root@localhost bokeh]#
Vedi che è grepstato eseguito con due nomi di file a causa di -l2.
ls |grep mp3 |sed -n "7p"te puoi semplicemente usareecho "Lemon Tree.mp3".