How to read a number from a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a number from a file?
# 1  
Old 03-26-2015
How to read a number from a file?

hello guys,

I'm struggled to get a number from a very long text file.
Code:
 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.
# 2  
Old 03-26-2015
Hello liuzhencc,

Following may help you in same with provided example.
Code:
awk -F"=" '/^NAtoms/ {sub(/^[[:space:]]+/,X,$2);sub(/[[:alpha:]]+/,X,$2);print $2}' Input_file

Thanks,
R. Singh
# 3  
Old 03-26-2015
Quote:
Originally Posted by RavinderSingh13
Hello liuzhencc,

Following may help you in same with provided example.
Code:
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(/^[[:space:]]+/,X,$2);sub(/[[:alpha:]]+/,X,$2);print $2}'
# 4  
Old 03-26-2015
Quote:
Originally Posted by liuzhencc
hello guys,

I'm struggled to get a number from a very long text file.
Code:
 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.
Don't set field sep -F=, modify like this

Code:
awk '/NAtoms/{print $2}' infile

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 03-26-2015
Hello kenshinhimura,

Following is the explaination for same, hope this helps.
Code:
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
# 6  
Old 03-26-2015
Quote:
Originally Posted by Akshay Hegde
Don't set field sep -F=, modify like this

Code:
awk '/NAtoms/{print $2}' infile

Great! it simply does the trick!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

2. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

3. Solaris

Dtrace-counting the number of read access to a file

Hi, I want to write a script with dtrace to count the number of read and write from and to a file. I use this for reading: dtrace -n 'syscall::read:entry { @ = count(); }' but it doesn'f give me a single answer. The result is always increasing. :wall: Please use next time code tags... (0 Replies)
Discussion started by: golabi
0 Replies

4. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

5. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

6. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

7. Shell Programming and Scripting

How to read n number of lines from a file

Hiii I am very new to shell scripting.This is my data file a.txt: 56 45.78 1000 11.23 76.89 45 34.56 23 3400 100 .......... Now i am must use shell scripting to read n number of lines from the file & from ts n number of lines i need to find greatest number among them & so on for... (44 Replies)
Discussion started by: varsha
44 Replies

8. Shell Programming and Scripting

Read the specified line number from file

Hi Guys, I am new to unix. Actually i want help in writing an single command where i can actually read specific line number in file where the line number will be passed to command as parameter. ex. 1 a 2 b 3 c 4 d And to my command i pass as 2. so i should get output as 2 b ... (15 Replies)
Discussion started by: kam786sim
15 Replies

9. Shell Programming and Scripting

Read a number from file and place it back

Hi All, I want to read one number from the file. Only one number will be there in the file. then i have to increment the number in my script and put it back in the same file. Is it possible? Can anybody help me? Thanks, Vinay (6 Replies)
Discussion started by: vinayakatj56
6 Replies

10. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies
Login or Register to Ask a Question