Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-26-2012
Registered User
 
Join Date: Dec 2011
Posts: 84
Thanks: 29
Thanked 0 Times in 0 Posts
redirect to a file based on a value in a field

Hi all!

Is it possible with awk to redirect all the records of a file that match a string in a field to a specific file.

input.tab:

Code:
aaaaa|bbbbbb|KKKKK
cccccc|dddddd|SSSSS
eeeeee|ffffffff|SSSSS
ggggg|hhhhhh|KKKKK

redirected to 2 separate files depending on the value in $3:

output_1.tab (only lines with "KKKKK" in $3):

Code:
aaaaa|bbbbbb|KKKKK
ggggg|hhhhhh|KKKKK

output_2.tab (only lines with "SSSSS" in $3):

Code:
cccccc|dddddd|SSSSS
eeeeee|ffffffff|SSSSS

If someone could give me a clue, it would be great !
Thx !
Sponsored Links
    #2  
Old 06-26-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,353
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
You can put the right filename in a string, depending on the value of $3. Then you can use:

Code:
print > string

To print to that file...
Sponsored Links
    #3  
Old 06-26-2012
Registered User
 
Join Date: Dec 2011
Posts: 84
Thanks: 29
Thanked 0 Times in 0 Posts
Ok, I was wondering if there was a built-in variable like FILENAME but for the other way


Thanks anyway !
    #4  
Old 06-26-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,353
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
FILENAME is a built-in variable but it would not be useful here, as it contains the name of the current input file, in this case "input.tab" ..
The Following User Says Thank You to Scrutinizer For This Useful Post:
lucasvs (06-26-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to split the data based on field from csv file? somehanu Shell Programming and Scripting 8 03-31-2011 04:50 AM
Update a field in a file based on condition kichu Shell Programming and Scripting 1 12-07-2010 12:57 PM
Spliting file based field pattern raghavendra.cse Shell Programming and Scripting 3 06-30-2010 10:56 AM
Split file based on field s_adu Shell Programming and Scripting 6 05-07-2009 02:08 PM
add lines automatically based on a field on another file melanie_pfefer Shell Programming and Scripting 0 07-24-2008 02:59 AM



All times are GMT -4. The time now is 01:15 AM.