extract rows that have a specific name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract rows that have a specific name
# 1  
Old 05-17-2012
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:

Code:
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 separate file and the no names into another.

output1:
Code:
bob starved and rich
jack rich
joey starved

output2:
Code:
mike

thanks
# 2  
Old 05-17-2012
So save names in file one if it has names more than 1 name and and save to another file if it is more than 1?
# 3  
Old 05-17-2012
Brute force method:

Code:
awk -v nf=name.file ' /^[^ \t]*[ \t]+starved and rich/ || /^[^ \t]*[ \t]+starved/ || /^[^ \t]*[ \t]+rich/  {print >nf; next;} NF == 1 ' input-file >nonames.file


Last edited by agama; 05-17-2012 at 12:35 AM.. Reason: Misred requirment -- fixed
This User Gave Thanks to agama For This Post:
# 4  
Old 05-17-2012
Code:
awk '{ if(NF > 1) print;}' data_file.txt >> file1.txt
awk '{ if(NF == 1) print;}' data_file.txt >> file2.txt

file1.txt contains
bob starved and rich
jack rich
joey starved

file2.txt contains
mike

or if you want to do it in 1 line
Code:
awk '{ if(NF > 1) {print >> "file1.txt";} if(NF == 1) {print >> "file2.txt";}}' data_file.txt


Last edited by codecaine; 05-17-2012 at 12:49 AM..
# 5  
Old 05-17-2012
Code:
grep -E  "^[^ ]* (starved and rich|rich|starved)$" infile > output1
grep -Ev "^[^ ]* (starved and rich|rich|starved)$" infile > output2

This User Gave Thanks to Chubler_XL For This Post:
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 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

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

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 transcript_id "TCONS_00000051"; exon_number "1"; transcript_id... (4 Replies)
Discussion started by: jdhahbi
4 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