copying selected records from two columns to another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copying selected records from two columns to another file
# 1  
Old 11-16-2010
CPU & Memory copying selected records from two columns to another file

Hey guys
I have got a tab-separated file and I want to copy only selected records from two columns at a time satisfying specified condition, and create a new file.

My tab separated file is like this

ID score ID score ID Score ID score ID score
1_11 0.80 2_23 0.74 2.36 0.78 2_34 0.75
A_34 0.34 A_45 0.49 E_73 0 W_9 0.34
.....
A_49 0 A_36 0.34 E_45 0 W_898 0.13


Each column is sorted so top ranking scores are maximum scores. I want to copy only the scores>0.1 with their corresponding rownames from this file to another file and create two columns as mentioned below.

Resulting file can be
ID score
1_11 0.80
A_34 0.34
2_23 0.74
A_45 0.49

Any suggestions ?
# 2  
Old 11-16-2010
Code:
 
awk 'BEGIN {print "ID   SCORE"} {if($2 > 0.1){print $1, $2}}' $file_name


Last edited by anurag.singh; 11-16-2010 at 11:02 AM..
# 3  
Old 11-16-2010
Hi, thanks.
In my input file I have got 10 columns ( 5 pairs of IDs and scores). Is it possible to add some loop that can fetch the records from not only first two but also from other 8 columns to another file ?
# 4  
Old 11-16-2010
Code:
nawk 'FNR>1{for(i=1;i<=NF;i+=2) if ($(i+1)>0.1) print $i,$(i+1)}' myFile

# 5  
Old 11-16-2010
Code:
awk 'NR==1{print $1,$2}FNR==1{i+=2;next}$i>0.1{print $(i-1),$i}' file file file file file

Code:
ID score
1_11 0.80
A_34 0.34
2_23 0.74
A_45 0.49
A_36 0.34
2.36 0.78
2_34 0.75
W_9 0.34
W_898 0.13

# 6  
Old 11-16-2010
Many Thanks.
Unfortunate I get an error message - nawk not found. I have mac osx. I donīt have nawk/gawk available.

Is it possible to use awk instead of nawk ?
I also want to use tab separator in output file.

---------- Post updated at 10:22 AM ---------- Previous update was at 10:19 AM ----------

Quote:
Originally Posted by Scrutinizer
Code:
awk 'NR==1{print $1,$2}FNR==1{i+=2;next}$i>0.1{print $(i-1),$i}' file file file file file

Code:
ID score
1_11 0.80
A_34 0.34
2_23 0.74
A_45 0.49
A_36 0.34
2.36 0.78
2_34 0.75
W_9 0.34
W_898 0.13

Hi Scrutinizer,
I have only one input file with multiple pairs of columns. What change would be required in the command you suggested ?
# 7  
Old 11-16-2010
Hi, you just need to specify the same input file 5 times on the command line...

Last edited by Scrutinizer; 11-16-2010 at 11:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading and copying a selected rows

Dear All, I have a data file input.res like below. (Only six column shown here for example.) Sequence of first column starting from 1 to 148. Input file 1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0 1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0 1 Q0... (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

2. Shell Programming and Scripting

Shell script to filter records in a zip file that contains matching columns from another file

Not sure if this is the correct forum for this question. I have two files. file1.zip, file2 Input: file1.zip col1, col2 , col3 a , b , 0:0:0:0:0:c436:9346:d40b x, y, 0:0:0:0:0:880:39f9:c9a7 m, n , 0:0:0:0:0:80c7:9161:fe00 file2.txt col1 c4:36:93:46:d4:0b... (1 Reply)
Discussion started by: anil.v
1 Replies

3. Shell Programming and Scripting

Script to find blank records in a file except for few columns

I have a file with the following format: X|High|2|GIC|DM||XHM|||6 Months X|Moderate|2|GIC|DM||XHM|||6 Months X|High|2|GCM|DM||XSF|||6 Months X|Med|2|GCM|DM||XSF|||6 Here there are ten columns but I need to print rows having blank records in any of the rows (except for 6th,8th and 9th... (10 Replies)
Discussion started by: chatwithsaurav
10 Replies

4. Shell Programming and Scripting

transpose selected columns

Can I transform input like the below ? Note: Insert zeros if there is no value to transform. Input key name score key1 abc 10 key2 abc 20 key1 xxx 100 key2 xxx 20 key1 zzz 0 key2 zzz 29 key3 zzz 129 key1 yyy 39output abc ... (1 Reply)
Discussion started by: quincyjones
1 Replies

5. Shell Programming and Scripting

awk script for getting the selected records from a file.

Hello, I have attached one file named file.txt . I have to create a file using the awk script with the records in which 38th position is P and not V . ex it should have 00501 HOLTSVILLE NYP00501 and it should not include 00501 I R S SERVICE CENTER ... (3 Replies)
Discussion started by: sonam273
3 Replies

6. UNIX for Dummies Questions & Answers

Copying and Pasting columns from one text file to another

I have a tab delimited text file that I want to cut columns 3,4,5 from. Then I want to paste these columns into a space delimited text file between columns 2 and 3. I still want to keep the space delimited format in the final text file. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

Getting rid of selected columns

Hi All, I've got a file like this: a 1 0 0 0 1 0 0 1 1 3 3 1 4 4 4 b 1 0 0 0 1 4 4 1 3 1 1 4 4 2 2 c 1 0 0 0 2 0 0 3 3 1 3 1 1 2 4 d 1 0 0 0 2 0 0 1 1 0 0 4 4 2 4 The file has ~4200 entries. I need to exclude those columns that are zeros for all those rows that have 2 in column 6. For... (0 Replies)
Discussion started by: zajtat
0 Replies

8. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. Shell Programming and Scripting

Script to place selected columns from a group of files side by side in a new file

Hi Everyone, I need a shell/perl script to bring selected columns from all the files located in a directory and place them in a new file side by side. File1: a b c d 2 3 4 5 f g h i .......... File2: I II III IV w x y z .............. and so on many files are there...... (8 Replies)
Discussion started by: ks_reddy
8 Replies

10. Shell Programming and Scripting

Copying selected content from file

I want to capture contents of a file between 2 strings into another file for eg all lines in between the keywords "start log" and "end log" should be copied into another file (1 Reply)
Discussion started by: misenkiser
1 Replies
Login or Register to Ask a Question