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

将返回从大型文本文件中提取的列表中最大的,但是如何将输出存储为变量?

4

3 回答 3

9

使用命令替换

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不起作用的化身。

于 2012-10-03T08:42:38.227 回答
1

对于未嵌套的情况,后引号也可以:

variable=`grep -A 26 "some text" somefile.txt |   
awk '/other text/ { gsub(/M/, " "); print $4 }' |  
sort -nru | head -1`
于 2012-10-03T08:55:43.090 回答
0

我建议

variable_name=$(grep -A 26 "some text" somefile.txt |
     awk '/other text/ { gsub(/M/, " "); print $4 }' |
     sort -nru | head -1)
于 2012-10-03T08:42:57.107 回答