Search file for string and store last result to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search file for string and store last result to variable
# 1  
Old 01-28-2011
Search file for string and store last result to variable

Hi,

I'm trying to search a text file for a string: "energy(sigma->0)=". To do so, I executed the grep command, which returned many matches, for example:

Code:
  energy without entropy =     -112.16486170  energy(sigma->0) =     -112.16520778
  energy without entropy =     -112.16488936  energy(sigma->0) =     -112.16522827
  energy  without entropy=     -112.164889  energy(sigma->0) =     -112.165228

What I want to do is store the grep results to a variable (var1), then get the very last number in the very last string result (the last number in var1), and store it to a second variable (var2) for later use in bash.

Alternatively, if there is a better command than grep that can automatically return only the last search hit in a given file, that would be even better.

Any assistance would be greatly appreciated!

gwr

Last edited by Franklin52; 01-28-2011 at 03:32 AM.. Reason: Please use code tags
# 2  
Old 01-28-2011
Code:
var1=$(grep "energy(sigma->0)=" text.file)
var2=${var1##*= }

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-28-2011
Perfect, works a treat! Cheers Chubler_XL.
# 4  
Old 01-28-2011
Code:
var=$(grep "energy(sigma->0)=" file | tail -1)

# 5  
Old 01-30-2011
Code:
awk '/energy\(sigma->0\) =/ {a=$NF}END{print a}' text.file

-112.165228

# 6  
Old 01-30-2011
Code:
while read line
do
  case $line in
    *"sigma->0"*) var=${line##* }
  esac
done < infile
echo $var

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

2. Shell Programming and Scripting

Bash to store result in variable for other lines in script to use

I can not figure out how to capture the $filename variable store by the bash. #!/bin/bash # oldest folder stored as variable for analysis, version log created, and quality indicators matched to run dir=/home/cmccabe/Desktop/NGS/test find "$dir" -maxdepth 1 -mindepth 1 -type d -printf... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Store result variable

Friends have the following problem: cat $PATH_DAT/mr.txt | nawk 'BEGIN { CantPnt=0; NumReg=0; FS="|" } { NumReg++ CantPnt=CantPnt+int($2) } END{ printf... (5 Replies)
Discussion started by: tricampeon81
5 Replies

4. Shell Programming and Scripting

Extract a string from a file and store it in variable

Hi, I've a file ImageSizeDetails.txt with the following contents: Image Name: swncd 01.10.00.04 Created: Wed Jan 9 14:05:48 2013 Image Type: ARM Linux Multi-File Image (gzip compressed) Data Size: 7351011 Bytes = 7178.72 kB = 7.01 MB Load Address: 00008000 Entry Point: ... (6 Replies)
Discussion started by: Parameswaran
6 Replies

5. Shell Programming and Scripting

How to store result of grep into a variable?

In a directory i have a file *output* whose contents are changing for every simulation. zgrep "trace file is" *output* | zgrep g o/p: trace file is Int_01.1352176388z4f56ec33.0.trace.gz I want to extract "Int_01.1352176388z4f56ec33.0.trace.gz" from the above result into a variable. i... (2 Replies)
Discussion started by: twistedpair
2 Replies

6. Programming

how to store string in variable

sorry i'm newbies c programer how to store string to variable with value flexible. example int hh=1; ---> value flexible 1,2,3,4,5; int xx=1; ---> value flexible 1,2,3,4,5; char test="value=%d and value=%d",hh,xx; --> not working char test2="value2=%d and value2=%d",hh,xx; --> not... (1 Reply)
Discussion started by: slackman
1 Replies

7. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

8. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

9. Shell Programming and Scripting

Prase a file and store and result to an array

Dear all, I have a file having the following formats: ThreadFail=Web1=1234 ThreadFail=Web2=2345 ThreadFail=Web3=12 ConnectionFail=DB1=11 ConnectionFail=DB2=22 The number of lines will be different from every time . How can I parse the file and store the result to an a array inside... (6 Replies)
Discussion started by: youareapkman
6 Replies

10. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies
Login or Register to Ask a Question