Basically I have below scenario e.g.

  1. grep 'test: \K(\d+)' $file => 15
  2. grep 'test1: \K(\d+)' $file => 20

Is there any way to store result of both commands into a variable like with comma as separator,

Test="grep 'test: \K(\d+)' $file;grep 'test1: \K(\d+)' $file"

Answer=eval $Test

Expected output: 15,20?

share|improve this question
1  
Maybe grep -Po 'test1?: \K\d+' < "$file" | paste -sd , -. The order would be based on where test and test1 occur in the file. – Stéphane Chazelas 10 hours ago
1  
(btw, ITYM grep -Po 'test... above (assuming that's GNU grep)) – Stéphane Chazelas 10 hours ago

Yes, you can do that by using Command substitution:

Test="$(grep 'test: \K(\d+)' $file),$(grep 'test1: \K(\d+)' $file)"

The variable=$(..) called Command substitution and it's means nothing more but to run a shell command and store its output to a variable or display back using echo command. For example, display date and time:

echo "Today is $(date)"

and for store it to a variable:

SERVERNAME="$(hostname)"

you can concatenate to output:

echo "$(hostname),$(date)"

the result will be:

yourhostname,Tue Jan 24 09:56:32 EET 2017
share|improve this answer
    
Edited question. Eval will fail if we use above approach? – Rock 10 hours ago
    
@Rock, take a look here stackoverflow.com/questions/11065077/… – Mhd Wissam Al-Roujoulah 10 hours ago
    
Need more suggestions – Rock 10 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.