grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -n -r | uniq | head -1
will return the largest in a list pulled from a large text file, but how do I store the output as a variable?
将返回从大文本文件中提取的列表中的最大值,但如何将输出存储为变量?
3 个解决方案
#1
8
Use command substitution:
使用命令替换:
my_var=$(grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -n -r | uniq | head -n1)
Also, for portability, I would suggest always using -n1
for the argument of head
. I've come across a couple of incarnations of it where using -1
doesn't work.
另外,为了便携性,我建议总是使用-n1作为head的参数。我遇到过几个版本,其中使用-1不起作用。
#2
1
For unnested cases back quotes will work too:
对于未经证实的情况,返回引号也会起作用:
variable=`grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -nru | head -1`
#3
0
I'd suggest
variable_name=$(grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -nru | head -1)
#1
8
Use command substitution:
使用命令替换:
my_var=$(grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -n -r | uniq | head -n1)
Also, for portability, I would suggest always using -n1
for the argument of head
. I've come across a couple of incarnations of it where using -1
doesn't work.
另外,为了便携性,我建议总是使用-n1作为head的参数。我遇到过几个版本,其中使用-1不起作用。
#2
1
For unnested cases back quotes will work too:
对于未经证实的情况,返回引号也会起作用:
variable=`grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -nru | head -1`
#3
0
I'd suggest
variable_name=$(grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -nru | head -1)