6
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 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

Comments

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`

Comments

0

I'd suggest

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.