extract specific rows


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract specific rows
# 1  
Old 05-31-2011
extract specific rows

Hi
I have a file that looks like the one below.
For the same 'TCONS' in the second column, I would like to extract the row that has the highest (or last) number in the fourth column.
Any kind of help will be appreciated.

input
Code:
transcript_id "TCONS_00000051"; exon_number "1"; 
transcript_id "TCONS_00000051"; exon_number "2"; 
transcript_id "TCONS_00000051"; exon_number "3"; 
transcript_id "TCONS_00000066"; exon_number "1";
transcript_id "TCONS_00000066"; exon_number "2"; 
transcript_id "TCONS_00000090"; exon_number "1"; 
transcript_id "TCONS_00000090"; exon_number "2"; 
transcript_id "TCONS_00000090"; exon_number "3"; 
transcript_id "TCONS_00000090"; exon_number "4";

output
Code:
transcript_id "TCONS_00000051"; exon_number "3"; 
transcript_id "TCONS_00000066"; exon_number "2"; 
transcript_id "TCONS_00000090"; exon_number "4";

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-31-2011 at 07:16 PM.. Reason: code tags, please!
# 2  
Old 05-31-2011
Code:
nawk -F'"' 'a[$2]<$(NF-1){a[$2]=$(NF-1);b[$2]=$0}END{for(i in b) print b[i]}' myFIle

# 3  
Old 05-31-2011
thank you for your help.
Joseph
# 4  
Old 06-01-2011
Quote:
Originally Posted by vgersh99
Code:
nawk -F'"' 'a[$2]<$(NF-1){a[$2]=$(NF-1);b[$2]=$0}END{for(i in b) print b[i]}' myFIle

To be precise
Code:
awk -F" " 'b[$2] < $4 {b[$2]=$4; a[$2]=$0} END {for (i in a) {print a[i]}}' file


Last edited by royalibrahim; 06-01-2011 at 12:46 AM..
# 5  
Old 06-01-2011
Quote:
Originally Posted by royalibrahim
To be precise
Code:
awk -F" " 'b[$2] < $4 {b[$2]=$4; a[$2]=$0} END {for (i in a) {print a[i]}}' file

how's that "precise"?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract duplicate rows with conditions

Gents Can you help please. Input file 5490921425 1 7 1310342 54909214251 5490921425 2 1 1 54909214252 5491120937 1 1 3 54911209371 5491120937 3 1 1 54911209373 5491320785 1 ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

Extract rows with different values at 2 columns

Hallo, I would need to extract only rows which has different value in the second and third column. Thank you very much for any advices Input: A 0 0 B 0 1 C 1 1 D 1 3 Output B 0 1 D 1 3 (4 Replies)
Discussion started by: kamcamonty
4 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. Shell Programming and Scripting

extract rows that have a specific name

Hi I want to extract rows in a large files that have a specific name. The name can be "starved and rich", "rich", "starved" or " ". Heres an example: bob starved and rich jack rich joey starved mike so it can have either 3 names or no name. I want to put the names into a... (4 Replies)
Discussion started by: phil_heath
4 Replies

5. Shell Programming and Scripting

Extract several columns with few rows

Hello, I want to extract several columns and rows from a huge tab delimited file for example: I want to print from from column 3 to 68 till row number 30. I have tried using cut command but it was extracting whole 3rd and 68th column. Please suggest a solution. Ryan (8 Replies)
Discussion started by: ryan9011
8 Replies

6. Shell Programming and Scripting

Extract fields from different rows.

Hi, I have data like below. SID=D6EB96CC0 HID=9C246D6 CSource=xya Cappe=1 Versionc=3670 MAR1=STL MARS2=STL REQ_BUFFER_ENCODING=UTF-8 REQ_BUFFER_ORIG_ENCODING=UTF-8 RESP_BODY_ENCODING=UTF-8 CON_ID=2713 I want to select CSource=xya (18 Replies)
Discussion started by: chetan.c
18 Replies

7. Shell Programming and Scripting

To extract selected rows

Hi, I have two files Input1.txt and Input2.txt and i need to have a output with selected lines starting with the values present in Input2.txt. For example: Input1.txt:... (5 Replies)
Discussion started by: bhas
5 Replies

8. Shell Programming and Scripting

How to extract duplicate rows

Hi! I have a file as below: line1 line2 line2 line3 line3 line3 line4 line4 line4 line4 I would like to extract duplicate lines (not unique, triplicate or quadruplicate lines). Output will be as below: line2 line2 I would appreciate if anyone can help. Thanks. (4 Replies)
Discussion started by: chromatin
4 Replies

9. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

10. Shell Programming and Scripting

How to extract duplicate rows

I have searched the internet for duplicate row extracting. All I have seen is extracting good rows or eliminating duplicate rows. How do I extract duplicate rows from a flat file in unix. I'm using Korn shell on HP Unix. For.eg. FlatFile.txt ======== 123:456:678 123:456:678 123:456:876... (5 Replies)
Discussion started by: bobbygsk
5 Replies
Login or Register to Ask a Question