You are not logged in.

#1 2008-09-17 20:31:46

jnwebb
Member
Registered: 2008-07-07
Posts: 52

extracting number from text files for variable

Hello again,

I have several different types of text files all with the line

# NPTS=32768  DT=0.005

somewhere in the file.  The numerical values are different for each file and I need to pull them out to assign as a variable for a program I am running.

How can I search a file and pull out each of these two numbers?

Thank you

Offline

#2 2008-09-17 21:16:41

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: extracting number from text files for variable

This should do:

nptsvar=$(sed -ne 's~^# NPTS=\([0-9]*\.*[0-9]*\)  *DT=\([0-9]*\.*[0-9]*\)~\1,\2~p' file1)
dtvar=${nptsvar#*,}
nptsvar=${nptsvar%,*}

EDIT:

How'd it go with the other one btw? I just realized.

And you like a little explanation right:
1. s~ substitute
2. ^# NPTS=\([0-9]*\.*[0-9]*\) ...=x numbers x dots x numbers
3. "  *" some spaces
4. DT=\([0-9]*\.*[0-9]*\) again, x numbers x dots x numbers
5. ~\1,\2~p and print number 1,number 2

Then we use bash's parameter substitution
${nptsvar#*,} removes from nptsvar
- # the shortest possible from the left
- that matches "*," so nptsvar, is removed
${nptsvar%,*} removes from nptsvar
- % the shortest possible from the right
- that matches ",*" so ,dtvar is removed

Last edited by Procyon (2008-09-17 21:28:16)

Offline

#3 2008-09-17 21:26:03

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: extracting number from text files for variable

Procyon's sed script assumes that the NPTS number and the DT number will both always have a period in them. In the original, the NPTS number doesn't have a period, and I don't know whether the DT number may lack a period.

You could try this instead (replacing the 's~....~p' part of Procyon's script):

's~# NPTS=\([0-9.]*\) *DT=\([0-9.]*\)~\1,\2~p'

That may let in stuff you want to exclude, for instance, it would let in:

NPTS=0.0.0.0.0 DT=1.1.1.1.1

and:

NPTS=....... DT=

and so on. But it's a tradeoff: if you know your data doesn't include anything like that, you can go with a simpler sed script. If you need to be fussier about what you're matching, then your sed script will need to be more complex.

EDIT: What I said in first paragraph is wrong. I overlooked the '*' after the '\.' in Procyon's script. What he's got will permit but not require a period. My final point still applies, though, to both of our suggestions. His script and my script would both match the second of my bad cases.

Last edited by Profjim (2008-09-17 21:31:34)

Offline

#4 2008-09-17 21:28:12

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: extracting number from text files for variable

Thank you both...Procyon...it went well thanks for all your help...it was for some earthquake research studies being presented before a congressional board...thanks again.

Offline

#5 2008-09-17 21:31:48

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: extracting number from text files for variable

@Profjim, you're right [0-9.]* is nicer. But [0-9]*\.*[0-9]* does work for e.g. 32768 (zero dots can be matched).

Edit:
And btw, I didn't expect replies so fast, and I did make a small edit to my script with "  *" instead of " *" but it's probably not important.

Last edited by Procyon (2008-09-17 21:33:14)

Offline

Board footer

Powered by FluxBB