grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -n -r | uniq | head -1
将返回从大文本文件中提取的列表中的最大值,但如何将输出存储为变量?
答案 0 :(得分:8)
my_var=$(grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -n -r | uniq | head -n1)
另外,为了便于携带,我建议始终使用-n1
作为head
的参数。我遇到了一些使用-1
不起作用的化身。
答案 1 :(得分:1)
对于未经证实的案例,返回引号也会起作用:
variable=`grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -nru | head -1`
答案 2 :(得分:0)
我建议
variable_name=$(grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -nru | head -1)