awk script - redirecting out put based on mapping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script - redirecting out put based on mapping
# 1  
Old 07-02-2013
IBM awk script - redirecting out put based on mapping

Need awk solution. Please advise.

inputfile.txt
Code:
1,NY, 1111
2,MI, 222
3,NY,333
4,OH,444
5,OH,555

mapping.txt
Code:
NY      NYNY
IL        ILLINOIS
OH       OHIO


Need to write a code which will compare 2nd column of inputfile.txt with mapping file and redirect output based on the mapping.txt file.
eg: OH will be mapped to OHIO and will be redirect to OHIO.txt. Mapping not found will be redirected to notfound.txt file

output would look like
-------------------------------
NYNY.txt
Code:
1,NY, 1111
3,NY,333

-------------------------------
OHIO.txt
Code:
4,OH,444
5,OH,555

-------------------------------
notfound.txt
Code:
2,MI, 222


Last edited by Don Cragun; 07-02-2013 at 03:43 AM.. Reason: Added CODE tags.
# 2  
Old 07-02-2013
couldnt test but something like this should work..

Code:
awk 'NR==FNR{A[$1]=$2;next}
{FS=",";if(A[$2]){print $0>A[$2]".txt"}else{print $0>"notfound.txt"}}' mapping.txt inputfile.txt

# 3  
Old 07-02-2013
Quote:
Originally Posted by vidyadhar85
couldnt test but something like this should work..

Code:
awk 'NR==FNR{A[$1]=$2;next}
{FS=",";if(A[$2]){print $0>A[$2]".txt"}else{print $0>"notfound.txt"}}' mapping.txt inputfile.txt

This should come close, but the 1st line in the 2nd file will be split into fields using the wrong value for FS (since field splitting will be done before FS is reset). Assuming that there could be around 50 output files, you will also need to close files to keep from running out of file descriptors. (And, if a file is opened multiple times, the redirection operator needs to be >> instead of >. But, this also means that the state output files need to be manually removed before the script is rerun unless you want multiple runs to accumulate data in the output files instead of replacing the data each time the script is run. The script below could be modified to remove or truncate the output files while it is reading mapping.txt.) Anyway, this alternative may help you find something that works for your input:
Code:
awk '
FNR == NR {
        sf[$1] = $2
        next
}
{       if(lf != sf[$2]) {
                if(lf != "") close(of)
                lf = sf[$2] ? sf[$2] : "notfound"
                of = lf ".txt"
        }
        print >> of
}' mapping.txt FS=',' inputfile.txt

If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /usr/bin/awk or /bin/awk.
These 2 Users Gave Thanks to Don Cragun 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

How to put the command to remove duplicate lines in my awk script?

I create a CGI in bash/html. My awk script looks like : echo "<table>" for fn in /var/www/cgi-bin/LPAR_MAP/*; do echo "<td>" echo "<PRE>" awk -F',|;' -v test="$test" ' NR==1 { split(FILENAME ,a,""); } $0 ~ test { if(!header++){ ... (12 Replies)
Discussion started by: Tim2424
12 Replies

2. Shell Programming and Scripting

How to put a difference calculation in my awk script ?

Hello, For my CGI, I have this script : #!/bin/bash echo "Content-type: text/html" echo "" echo ' <html> <head> <meta http-equiv="Content-Type" content="test/html"; charset=UTF-8"> <title> CLF MONITORING </title> <h1> FRAME... (4 Replies)
Discussion started by: Tim2424
4 Replies

3. Shell Programming and Scripting

Excel vlookup function like value mapping with awk

I have two files File1 175552 st_497858.1 rs86052.1 rs92185.1 st_001022416.1 174841 175552_174841 179912 st_001122967.2 rs90435.1 rs89122.1 st_001022583.1 175545 179912_175545 179912 st_001122967.2 rs90435.1 rs89122.1 st_001022584.1 175545 179912_175545 179967 st_001256606.1 rs93516.2... (1 Reply)
Discussion started by: sammy777888
1 Replies

4. Shell Programming and Scripting

awk script to find time difference between HTTP PUT and HTTP DELETE requests in access.log

Hi, I'm trying to write a script to determine the time gap between HTTP PUT and HTTP DELETE requests in the HTTP Servers access log. Normally client will do HTTP PUT to push content e.g. file_1.txt and 21 seconds later it will do HTTP DELETE, but sometimes the time varies causing some issues... (3 Replies)
Discussion started by: Juha
3 Replies

5. Shell Programming and Scripting

awk script to call another script based on second column entry

Hi I have a text file (Input.txt) with two column entries separated by tab as given below: aaa str1 bbb str2 cccccc str3 dddd str4 eee str3 ssss str2 sdf str3 hhh str1 fff str2 ccc str3 ..... ..... ..... (1 Reply)
Discussion started by: my_Perl
1 Replies

6. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

7. Shell Programming and Scripting

Put delimeter in data based on value

Hi Friends, I have a file as below source.txt 12345JackYKing32N 1235 JulyYoig 31N i am using cut command for cutting the fields cut -c 1-5 source.txt 12345 1235 like above i have to use each time to cut all the fieds manually. I have a file(pre.txt) which tells... (3 Replies)
Discussion started by: i150371485
3 Replies

8. Shell Programming and Scripting

Creating unique mapping from multiple mapping

Hello, I do not know if this is the right title to use. I have a large dictionary database which has the following structure: where a b c d e are in English and p q r s t are in a target language., the two separated by the delimiter =. What I am looking for is a perl script which will take... (5 Replies)
Discussion started by: gimley
5 Replies

9. Shell Programming and Scripting

using redirecting from awk

Well this is what im doing, im writing a script that you pass 3 variables into. Filename, delimiter or "FS in AWK", and a string of columbs you want to keep 1,2,4,5... Just modifing a data file and rewriting with a different extension. My problem atm is using awk to seperate the "columb String"... (2 Replies)
Discussion started by: ugh
2 Replies

10. Shell Programming and Scripting

awk script to split a file based on the condition

I have the file with the records like 4234234 US phone 3244234 US cup 2342342 CA phone 8947234 US phone 2389472 CA cup 2348972 US maps 3894234 CA phone I want the records with (US,phone) as record to be in one file, (Us, cup) in another file and (CA,cup) to be in another I mean all... (12 Replies)
Discussion started by: superprogrammer
12 Replies
Login or Register to Ask a Question