Mapping and replacing numbers in two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mapping and replacing numbers in two files
# 1  
Old 04-20-2011
Mapping and replacing numbers in two files

hi i have two files
one of the form
Code:
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

Code:
1
2
1
3
4
5
6
3
3
6
6

now i want to replace each number in the second file according to the mappings in the first file

e.g for above files the file 2 should become like
Code:
2
45
2
56
98
6598
98
56
56
98
98

i know sed command but how can i apply it to two files so that it will happen in single line command ?

Thanks in advance
# 2  
Old 04-20-2011
using (n)awk:

Code:
#  nawk 'NR==FNR{x[$1]=$2;next}{print x[$1]}'  map infile
2
45
2
56
98
6598
98
56
56
98
98

HTH
These 2 Users Gave Thanks to Tytalus 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

Replacing 12 columns of one file by second file based on mapping in third file

i have a real data prod file with 80+ fields containing 1k -2k records. i have to extract say 12 columns out of this which are sensitive fields along with one primary key say SEQ_ID (like DOB,account no, name, SEQ_ID, govtid etc) in a lookup file. i have to replace these sensitive fields in... (11 Replies)
Discussion started by: megh12
11 Replies

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

3. Shell Programming and Scripting

Replacing a character between two numbers

Hi, I need to replace a character between two numbers (specifically a - to a _). The problem is that they can be *any* numbers. So, I need a one liner to turn a file like this: 1-2 3-4 55-66 4323-12312893 into the following 1_2 3_4 55_66 4323_12312893 Any help would be appreciated! (5 Replies)
Discussion started by: mikey11415
5 Replies

4. Shell Programming and Scripting

Mapping two files

I have a large comma delimited file from which i have to extract a specific column, compare it against a static file and replace the column value with the matching column from the static file. The following are the files: Main file: mainfile.csv test1234,,real,,,,open,,,,EU,,,,23/03/2012... (12 Replies)
Discussion started by: goddevil
12 Replies

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

6. Shell Programming and Scripting

Preserving Empty Lines while Replacing Numbers

Greetings, I am using tcsh to write a script that will replace the numbers in a file with a single number, the caveat is that this file has blank lines which are necessary for another step down the line so I need to preserve the blank lines. I have tried sed and awk but both will collapse the... (1 Reply)
Discussion started by: bonesy
1 Replies

7. Shell Programming and Scripting

Replacing numbers in bash scripts

Hi, I have lines in some files that look exactly as below, and the line numbers they occur in are always the same. (Lines 136-139) W 0.00000000 0.00000000 2.00000000 W 0.50000000 0.50000000 2.50000000 W 0.00000000 0.00000000 3.00000000 W 0.50000000 0.50000000 3.50000000 I'd like to... (0 Replies)
Discussion started by: bluesmodular
0 Replies

8. Shell Programming and Scripting

Need help in finding and replacing port numbers.

Hi All, I am trying to write a shell script which firstly will search some files and then increase the port numbers mentioned in them by a certain no. let me clear it with an example- suppose there r few files a,b,c,d.... file a's content- <serverEntries xmi:id="ServerEntry_1"... (3 Replies)
Discussion started by: ankushsingh10
3 Replies

9. Shell Programming and Scripting

replacing numbers greater than 0 with 1

I have some ASCII files containing numerous numbers. What I'd like to do is replace all numbers greater than 0 with 1. Examples of the numbers include: - 000011 and 000042 Thanks (4 Replies)
Discussion started by: vrms
4 Replies

10. UNIX for Dummies Questions & Answers

Sed - Replacing numbers w/asteriks in random location in a file

I have a file which contains lots of text (comment field). I would like to parse through the comment field which can be up to 255 characters long and look for anything that seems to resemble, say, a credit card number or customer account number, etc. and replace the numbers with asteriks (*). ... (9 Replies)
Discussion started by: giannicello
9 Replies
Login or Register to Ask a Question