Extract numbers from .txt file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract numbers from .txt file
# 1  
Old 08-15-2010
Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!!

The .txt file looks essentially like this but is much bigger.


Code:
	Spearman's rank correlation rho

data:  x[, 2] and x[, n] 
S = 969275.3, p-value = 0.1915
alternative hypothesis: true rho is not equal to 0 
sample estimates:
       rho 
0.09620046 


	Spearman's rank correlation rho

data:  x[, 2] and x[, n] 
S = 641188.7, p-value = 6.52e-10
alternative hypothesis: true rho is not equal to 0 
sample estimates:
      rho 
0.4301456 


	Spearman's rank correlation rho

data:  x[, 2] and x[, n] 
S = 1039343, p-value = 0.402
alternative hypothesis: true rho is not equal to 0 
sample estimates:
       rho 
0.06146867 


	Spearman's rank correlation rho

data:  x[, 2] and x[, n] 
S = 900824, p-value = 0.005946
alternative hypothesis: true rho is not equal to 0 
sample estimates:
      rho 
0.1993957 


	Spearman's rank correlation rho

data:  x[, 2] and x[, n] 
S = 747354.1, p-value = 2.317e-06
alternative hypothesis: true rho is not equal to 0 
sample estimates:
      rho 
0.3357915


Last edited by Scott; 08-15-2010 at 10:13 AM.. Reason: Please use code tags
# 2  
Old 08-15-2010
to extract the p-value
Code:
awk -F= '/p-value/{printf "%s,", $3}END {printf "\n"}' file.txt

to extract rho values

Code:
 awk '/^rho/{getline;printf "%s,",$1} END{printf "\n" }' file.txt

you can redirect these outputs in different files as required
# 3  
Old 08-15-2010
Thank you very much. P-value works like a charm! I changed %s to %f. That's even better. However, rho extraction doesn't seem to work as there are two rho's per paragraph.
# 4  
Old 08-15-2010
that is why when the row starts with "rho" we are printing the next row. seems to be ok for me.
# 5  
Old 08-15-2010
Code:
awk '/      rho/ { getline; printf ("%s,",$1) } ' rho_fil

the problem is because of space before the rho
# 6  
Old 08-15-2010
Alright, that's it! itkamaraj was right. Problem were the spaces before rho. A combination of both suggestions works perfect! Thanks!!!

Code:
Code:
awk -F= '/^      rho/{getline;printf "%f,", $1}END {printf "\n"}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To extract values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 ... (1 Reply)
Discussion started by: jeo_fb
1 Replies

2. Shell Programming and Scripting

Extract information from txt file

Hello! I need help :) I have a file like this: AA BC FG RF TT GH DD FF HH (a few number of rows and three columns) and I want to put the letters of each column in a variable step by step in order to give them as input in another script. So I would like to obtain: for the 1° loop:... (11 Replies)
Discussion started by: edekP
11 Replies

3. Shell Programming and Scripting

Extract numbers from file name-how to ?

Hello friends,I am new to Unix programming. how do I achieve the following in Unix shell script (I am running ksh on AIX) extract the number from name of file? My file format is like "LongFileName-1234.020614-221030.txt" now I want to extract value which is between (-) hyphen and (.) dot... (4 Replies)
Discussion started by: f150
4 Replies

4. Shell Programming and Scripting

Script extract text from txt file with grep

All, I require a script that grabs some text from the gitHub API and will grep (or other function) for a string a characters that starts with (") quotes followed by two letters, may contain a pipe |, and ending with ) . What i have so far is below but it's not returning anything. ... (4 Replies)
Discussion started by: ChocoTaco
4 Replies

5. UNIX for Dummies Questions & Answers

Extract Numbers from a log file

Hi, I am trying to grep/extract the number list from this log file, can I get some help on this. I can grep the word 'href' to see the numbers, but it is resulting with the complete line. Content of my file: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head>... (4 Replies)
Discussion started by: Sajjadmehdi
4 Replies

6. Shell Programming and Scripting

Command to extract all columns except the last few from a txt file

hello, i have publicly available txt file with little less than 300000 rows. i want to extract from column 1 to column 218 and save it in another text file. i use the cut command but the file is saved with multiple rows from the source file onto a single row in the destination. basically it is... (6 Replies)
Discussion started by: madrazzii
6 Replies

7. Shell Programming and Scripting

Finding consecutive numbers in version names on a txt file

Hi all. I have a directory which contains files that can be versioned. All the files are named according to a pattern like this: TEXTSTRING1-001.EXTENSION TEXTSTRING2-001.EXTENSION TEXTSTRING3-001.EXTENSION ... TEXTSTRINGn-001.EXTENSION If a file is versioned, a file called ... (10 Replies)
Discussion started by: fox1212
10 Replies

8. Shell Programming and Scripting

Command to remove numbers from beginning of txt file

Hello. I have the following issue: my txt file has the following format: train/dr4/fklc0/sx175.txt 0 80282 Severe myopia contributed to Ron's inferiority complex. train/dr4/fklc0/sx355.txt 0 42906 Dolphins are intelligent marine mammals. train/dr4/fklc0/sa2.txt With the... (1 Reply)
Discussion started by: li_bi
1 Replies

9. UNIX for Dummies Questions & Answers

Command to delete numbers at beginning of txt file line

Hello. I have the following issue: my txt file has the following format: train/dr4/fklc0/sx175.txt 0 80282 Severe myopia contributed to Ron's inferiority complex. train/dr4/fklc0/sx355.txt 0 42906 Dolphins are intelligent marine mammals. train/dr4/fklc0/sa2.txt awk 'NR%2==0' test1.txt >... (4 Replies)
Discussion started by: li_bi
4 Replies

10. Shell Programming and Scripting

Extract from txt file

I have data as follow in the txt file. I want to skip line starting with '#' sign. #command program abc defmt exp refmt ... ... I want to store abc exp .... in a array. I want to store defmt refmt in a array I need command to read each line in the file. I need... (6 Replies)
Discussion started by: ekb
6 Replies
Login or Register to Ask a Question