Mapping two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mapping two files
# 8  
Old 11-15-2012
Quote:
Originally Posted by goddevil
I am getting the following error when i execute the command

Code:
awk: syntax error near line 1
awk: bailing out near line 1


Solaris? Try nawk instead of awk
# 9  
Old 11-15-2012
Quote:
Originally Posted by Chubler_XL
Solaris? Try nawk instead of awk
Yep. Solaris bash

I used nawk and am not getting any errors now but the string is not being replaced.

Please see my updated mainfile.csv.

The field 26 has blankspaces and thus will not exactly match. Thats why i am using
Code:
[[ ]] and comparing with *

---------- Post updated at 01:37 AM ---------- Previous update was at 01:34 AM ----------

Quote:
Originally Posted by Chubler_XL
Try this:

Code:
awk -F, 'FNR==NR{C[$2]=$1;next}{$26=C[$26]}1' OFS=, reffile mainfile.csv

Can you kindly explain what you're doing here?

Code:
 'FNR==NR{C[$2]=$1;next}{$26=C[$26]}1'

# 10  
Old 11-15-2012
If you don't mind the blank spaces being removed when field 26 is updated, this should work for you

Code:
nawk -F, '
FNR==NR{C[$2]=$1;next}
{sub(/ *$/,"",$26);$26=C[$26]}1' OFS=, reffile mainfile.csv

As for the explination:

FNR=Current file row number
NR=Global row number
when FNR==NR first file is being processed (ie reffile)

C[$2]=$1;next
Build a lookup array with refcode (2nd field) as index and replace value (1st field) as data

sub(/ *$/,"",$26)
replace any trailing spaces from field 26 (assumption is they are spaces and not tabs)

$26=C[$26]
lookup array with field 26 value and put data back into $26
# 11  
Old 11-16-2012
Quote:
Originally Posted by Chubler_XL
If you don't mind the blank spaces being removed when field 26 is updated, this should work for you

Code:
nawk -F, '
FNR==NR{C[$2]=$1;next}
{sub(/ *$/,"",$26);$26=C[$26]}1' OFS=, reffile mainfile.csv

As for the explination:

FNR=Current file row number
NR=Global row number
when FNR==NR first file is being processed (ie reffile)

C[$2]=$1;next
Build a lookup array with refcode (2nd field) as index and replace value (1st field) as data

sub(/ *$/,"",$26)
replace any trailing spaces from field 26 (assumption is they are spaces and not tabs)

$26=C[$26]
lookup array with field 26 value and put data back into $26
The code is working now. Thanks a mil for the explanation. It makes a lot of sense now

---------- Post updated at 05:12 AM ---------- Previous update was at 01:52 AM ----------

I am trying to adapt the code so that the replacement is done only if the pattern in $26 matches the pattern C[$2] in the below code.

Where do i insert an IF loop like if ( $26 == C[$2] )

Code:
nawk -F, '
FNR==NR{C[$2]=$1;next}
{sub(/ *$/,"",$26);$26=C[$26]}1' OFS=, reffile mainfile.csv


Last edited by goddevil; 11-15-2012 at 10:22 PM..
# 12  
Old 11-16-2012
Again you probably want to trim $26 before doing a compare so:

Code:
nawk -F, ' 
    FNR==NR{C[$2]=$1;next} 
    {sub(/ *$/,"",$26);if($26==C[$2])$26=C[$26]}1' OFS=, reffile mainfile.csv

This User Gave Thanks to Chubler_XL For This Post:
# 13  
Old 11-16-2012
Quote:
Originally Posted by Chubler_XL
Again you probably want to trim $26 before doing a compare so:

Code:
nawk -F, ' 
    FNR==NR{C[$2]=$1;next} 
    {sub(/ *$/,"",$26);if($26==C[$2])$26=C[$26]}1' OFS=, reffile mainfile.csv

No joy there.


When i insert the if condition there, the code doesnt work. Nothing is replaced meaning nothing is matched. C[$2] will have a different meaning since NR is now 2 right?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Many to many -- mapping

INPUT 13333--TEXT1 14444--TEXT2 13333--TEXT3 12233--TEXT5 14444--TEXT5 12233--TEXT1 12222--TEXT5 13333--TEXT09 what I'm looking for is something using awk arrays with below given output. 14444--TEXT2,TEXT5 13333--TEXT1,TEXT3,TEXT09 12233--TEXT5,TEXT1 12222--TEXT5 (6 Replies)
Discussion started by: busyboy
6 Replies

2. UNIX for Advanced & Expert Users

Mapping Oracle SGA memory into physical files in Solaris.

Is there a way by which we could map Oracle SGA memory into physical files in solaris. We could find these physical files in /dev/shm/ folder in linux environment but they are missing in solaris. (1 Reply)
Discussion started by: arjun_chauhan
1 Replies

3. UNIX for Dummies Questions & Answers

Loop Through Files in Order and mapping

Hi there, How can I loop through files with order. I tried using ls .html | sort -v However the filename is with spaces:1233.61.47.0 - 121.61.123.112 nexpose.html Here is the original code that is working well, but I need to sort through the filename.echo "<center>" for file in *.html... (2 Replies)
Discussion started by: alvinoo
2 Replies

4. Shell Programming and Scripting

Mapping Servers

Hi All, I need an urgent assistance please . My case below: I have a list of 500 IP addresses. All These ip addresses are mapped/connected to different machine kinds : NT, Linux, Switch, Router ,FW, and so on. My Requirement is to filter from all this ip address only the Linux machines.... (2 Replies)
Discussion started by: James Stone
2 Replies

5. UNIX for Advanced & Expert Users

Help With Mapping machine

Hi All, I need an urgent assistance please . My case below: I have a list of 500 IP addresses. All These ip addresses are mapped/connected to different machine kinds : NT, Linux, Switch, Router ,FW, and so on. My Requirement is to filter from all this ip address only the Linux... (0 Replies)
Discussion started by: James Stone
0 Replies

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

7. Shell Programming and Scripting

Need Help Mapping Arrays

I have the following arrays with different lengths that I want to map them with the same key. # Week numbers, 8 columns @headers = ("2011-34", "2011-35", "2011-36", "2011-37", "2011-38", "2011-39", "2011-40", "2011-41"); %data = ("2011-34", BCE, "2011-35", YZA, "2011-36",... (5 Replies)
Discussion started by: tqlam
5 Replies

8. UNIX for Dummies Questions & Answers

Re-Mapping Printers.

Hi we have a situation where some printers are on a server that sometimes has to be rebooted. If this happens the Unix boxes we have that are referencing the printers in the vfstab file fail to work even when the print server is brought back up. Does anyone know if it would be possible to put... (0 Replies)
Discussion started by: Hadleyshope
0 Replies

9. Shell Programming and Scripting

Mapping and replacing numbers in two files

hi i have two files one of the form 1 2 2 45 3 56 4 98 5 6598 6 98 7 10 8 0 9 15 10 56 This file's significance is that it maps the number in first column to that of the number in second column The other file is of the form 1 2 (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

10. Shell Programming and Scripting

Join 3 files using key column in a mapping file

I'm new of UNIX shell scripting. I'm recently generating a excel report in UNIX(file with delimiter is fine). How should I make a script to do it? 1 file to join comes from output of one UNIX command, the second from another UNIX command, and third from a database query. The key columes of all... (7 Replies)
Discussion started by: bigsmile
7 Replies
Login or Register to Ask a Question