2012-10-03 13 views
5
grep -A 26 "some text" somefile.txt | 
    awk '/other text/ { gsub(/M/, " "); print $4 }' | 
    sort -n -r | uniq | head -1 

restituirà il più grande in un elenco estratto da un file di testo di grandi dimensioni, ma come posso archiviare l'output come variabile?output di archivio bash come variabile

risposta

7

Uso command substitution:

my_var=$(grep -A 26 "some text" somefile.txt | 
    awk '/other text/ { gsub(/M/, " "); print $4 }' | 
    sort -n -r | uniq | head -n1) 

Inoltre, per la portabilità, vorrei suggerire di usare sempre -n1 per l'argomento di head. Mi sono imbattuto in un paio di incarnazioni di esso in cui l'utilizzo di -1 non funziona.

0

Io suggerirei

variable_name=$(grep -A 26 "some text" somefile.txt | 
    awk '/other text/ { gsub(/M/, " "); print $4 }' | 
    sort -nru | head -1) 
1

Per i casi unnested indietro quotazioni lavorare troppo:

variable=`grep -A 26 "some text" somefile.txt | 
awk '/other text/ { gsub(/M/, " "); print $4 }' | 
sort -nru | head -1`