How to read a number from a file?

hello guys,

I'm struggled to get a number from a very long text file.

 NAtoms=   33 NActive=   30 NUniq=   23 SFac= 1.00D+00 NAtFMM=   60 NAOKFM=F Big=F
 Integral buffers will be    131072 words long.
 Raffenetti 2 integral format.

The number 33 is what I wanted, always follows NAtoms= . I use awk -F= '{print $2}', but get "33 NActive" instead of 33.

Thank you very much for your kind help.

Hello liuzhencc,

Following may help you in same with provided example.

awk -F"=" '/^NAtoms/ {sub(/^[[:space:]]+/,X,$2);sub(/[[:alpha:]]+/,X,$2);print $2}' Input_file

Thanks,
R. Singh

Hi Singh,
can you exdplain your answer word by word?
awk -F"=" '/^NAtoms/ {sub(/[1]+/,X,$2);sub(/[[:alpha:]]+/,X,$2);print $2}'


  1. [:space:] ↩︎

Don't set field sep -F= , modify like this

awk '/NAtoms/{print $2}' infile
1 Like

Hello kenshinhimura,

Following is the explaination for same, hope this helps.

awk 
-F"="                                  #### Take = as delimiter
'/^NAtoms/                         #### Search those lines which are starting from word NAtoms
{sub(/^[[:space:]]+/,X,$2);  #### substitute leading space of $2 with NULL 
sub(/[[:alpha:]]+/,X,$2)       #### substitute all alphabates in $2 with NULL so only digits will come as result 
;print $2}'                          #### print $2 now. 
test4523                            #### Input_file

Thanks,
R. Singh

Great! it simply does the trick!