Find a string and extract a value from a file

I have a file where a line has the following form:

n0=7.00 !Central density [10**19 m-3]

and I want to extract the value 7.00. I used to do this with the order below, which finds the string "n0" and take the rest of the line parting from the separator "=", but the comment "Central density..." makes this solution not applicable to this case,

>n0=` grep "n0" myfile.dat | cut -d "=" -f2,3 `

Anyone knows how can I improve it in order to take only the number?. Thanks in advance

this will do i guess

echo "n0=7.00 !Central density [10**19 m-3]"|awk -F"[=!]" '/^n0/{print $2}'

Thanks for the reply, but actually n0 can take any value, it is not 7.00 necessarily. I've solved it concatenating a second cut command which defines another delimiter " ":

> n0=` grep "n0" $bsim | cut -d "=" -f2 | cut -d " " -f1`

Thanks again!

www:~# echo "n0 \u003d 7,00! Central densidade [10 ** 19 m-3]" | awk '{print $3}' |cut -f1 -d\
7,00
www:~#

Espero ter ajudado.

---------- Post updated at 12:45 PM ---------- Previous update was at 12:42 PM ----------

www:~# echo "n0=7.00 \!Central density [10**19 m-3]" | awk '{print $1}' |cut -f1 -d\! |cut -f2 -d\=
7.00
www:~#

Hello,

Per our forum rules, all users must write in English, use semi-formal or formal English language and style, and correct spelling errors.

The reason for this is that most software and operating systems are written in English and these are software related technical forums.

In addition, nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, you need to spell correctly!

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your language, check your spelling and correct your spelling errors. You might receive a forum infraction if you don't pay attention to this.

Also, do not write in cyberpunk or abbreviated chat style under any circumstances and do not use profanity. This is not a chat room, it is a formal knowledge base to serve you and everyone, of all ages and cultural backgrounds.

Thanks!

The UNIX and Linux Forums

or...

echo 'n0=7.00 !Central density [10**19 m-3]' | awk -F '=|!' '{print $2}'

Or -

$
$ cat f1
first line
n0=7.00 !Central density [10**19 m-3]
third line
$
$ perl -nle '/^n0=(.*?) / && print $1' f1
7.00
$
$

tyler_durden

Just a derivative:

echo 'n0=7.00 !Central density [10**19 m-3]'|awk -F"[=\ ]" '/^n0/{print $2}'