Using GREP/AWK to extract a number and store it as a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using GREP/AWK to extract a number and store it as a variable
# 1  
Old 03-06-2009
Using GREP/AWK to extract a number and store it as a variable

Hello,

I have a question regarding the awk command. Here is the line I need to grep:

1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157

Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any help.

modey3
# 2  
Old 03-06-2009
Try...
Code:
eval $(echo '1 F= -.13250138E+03 E0= blah ' | awk '{print $2 $3}')

Variable is $F
# 3  
Old 03-06-2009
Ygor,

Thanks the command worked! Your solution works because fortunately 'F=' precedes '-.13250138E+03'. Is it possible to define '-.13250138E+03' in terms of a variable even if 'F=' didn't precede it? Thanks

modey3
# 4  
Old 03-06-2009
Never mind, I figured it out...

eval $(echo '1 F= -.13250138E+03 E0= blah ' | awk '{print "X=" $3}')
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract data from a file and store it in a variable

Hi , I have an file like below , cat input.txt 'Pattern2' => 'blahdalskdahdlahldahdlakhdlahdlkajdlkaadasdadadadadadadasda ajlalnalndklandlaksdlkaddd' 'Pattern2' => 'aohaonalkndlanldandlandklasnldnaldnak' ............ ........ ..... Here is what am trying to do , I want to grep for... (3 Replies)
Discussion started by: scott_cog
3 Replies

2. 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

3. 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

4. UNIX Desktop Questions & Answers

How to store the value of grep in a variable

Hi , I was trying to store the value of a grep command to store in a variable so i can use it somewhere else in my script but i am getting error, i am using bash shell in linux: var= `grep -n "$DATE" testfile.log | head -n 1 | sed -n 's/^\(*\).*/\1/p` echo "$var"I get the error: unexpected... (5 Replies)
Discussion started by: learninguser235
5 Replies

5. Shell Programming and Scripting

grep attribute value pair and store it a variable

Hi, I have a file contains attribute value pair like.. ..name=erick rollno=583.0 pass=recon.. From the above line, i need to grep for only "rollno" and store "rollno=583.0" in a variable. Pls suggest (6 Replies)
Discussion started by: skraja1982
6 Replies

6. Shell Programming and Scripting

Capturing a number at the end of line and store it as variable

Hello, Would someone guide me on how to write a shell script the would search for a phone no using at the end text file using sed or awk and store it in a varaible or print it. The text file is in this form text or numbers in first line text or numbers in second line . . . Firsname... (6 Replies)
Discussion started by: amuthiga
6 Replies

7. Shell Programming and Scripting

Using grep to extract line number

I'm trying to use grep to get the line number only. This is the command I'm using: grep -n "Content-Disposition: attachment" mbox The output I get is: 45:Content-Disposition: attachment; filename="test.txt" So now I just want to get the line number (45) from this output. Can someone... (8 Replies)
Discussion started by: mskarica
8 Replies

8. Shell Programming and Scripting

how to store grep command in a Variable

I have a variable A echo $A 5060 I am exporting the value X,Y,Z and it id fetching right thing and When I run C=`${X} -l ${Y} ${Z} "trap '' INT;. ~/.profile >/dev/null 2>/dev/null; netstat -na | grep \$A`" here it is going to same directory and also running netstat -na | grep 5060 ... (4 Replies)
Discussion started by: madhusmita
4 Replies

9. Shell Programming and Scripting

Grep results to store in a shell variable

I have the results of a grep with -n store in a shell variable ie VAR=`grep -n -e 'PATTERN' file` How ever I am missing the line breaks in the variable , How do I store the resualts of grep with many lines in to a variables. I want the varable should be the sawmway as we do the grep grep... (3 Replies)
Discussion started by: jojan
3 Replies

10. UNIX for Advanced & Expert Users

What is the Maximum number of characters that i can store in a variable?

Hi, Can any one tell me, what is the maximum number of characters that can be stored in a variable? Thanks in Advance, Shan (9 Replies)
Discussion started by: pcshan
9 Replies
Login or Register to Ask a Question