Read Field from file1 and find and replace in file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read Field from file1 and find and replace in file2
# 1  
Old 04-06-2011
Read Field from file1 and find and replace in file2

Hi All,

I have file1 line below:
Code:
$myName$|xxx

Now I need to read the file1 and find for $myName$ in file2 and replace with xxx

file1:
Code:
$myName$|xxx

file2:
Code:
My name is $myName$

expected output in file2 after executing the script is below:
Code:
my name is xxx

Thanks,

Last edited by Franklin52; 04-06-2011 at 04:15 AM.. Reason: Please use code tags
# 2  
Old 04-06-2011
The $ char you have chosen for your marker makes it harder, as is is a meta char and means something to gsub.

Anyway here is an solution in awk:
Code:
awk -F\| 'NR==FNR{A=$1; gsub("\\$", "\\$", A); rep[A]=$2; next;}
{ for(r in rep) gsub(r, rep[r],$0);
  print $0 }' file1 file2

Here is solution with %% or # markers instead:
Code:
awk -F\| 'NR==FNR{rep[$1]=$2; next;}
{ for(r in rep) gsub(r, rep[r],$0);
  print $0 }' file1 file2

# 3  
Old 04-06-2011
Hi gdevadas,

Try with:

Code:
awk 'NR==FNR{gsub(/\|/," ");a[$1]=$1;b[$1]=$2;next}{c[$4]=$4;d[$4]=$1" "$2" "$3}
END{for(i in c) if(c[i]==a[i]) print d[i],b[i]; else print d[i],c[i]}' file1 file2


Hope it helps.

Regards.
# 4  
Old 04-06-2011
@cgkmal, didn't work too well with my test files (didn't expand my markers, changed order of lines and truncated first line):

file1
Code:
$myName$|xxx
$myAge$|21
$myShell$|/bin/bash

file2
Code:
My name is $myName$, and I'm $myAge$ years old.
I love $myShell$

output
Code:
I love $myShell$ 
My name is $myName$,

# 5  
Old 04-06-2011
Hi Chubler,

Quote:
Originally Posted by Chuble_XL
@cgkmal, didn't work too well with my test files (didn't expand my markers, changed order of lines and truncated first line):
This change order issue is something what I was seeing how to solve in the printing when I do for(i in X), but I can't so far Smilie.

The other thing, I see now that I was testing with fixed format files format and not more general files as you show. Certainly it won't work
only with my simple test filesSmilie. I'll try to adapt it in some other way if I can in order to work with other filesSmilie

Your first code works fine with my test files, but the 2nd code prints the same file2, as below:

Code:
$ cat file1
$myName$|xxx
$yourName1$|xxy
$hisName$|zzz

$ cat file2
Your name is $yourName$
My name is $myName$
His name is $hisName$

$ awk -F\| 'NR==FNR{rep[$1]=$2; next;}
{ for(r in rep) gsub(r, rep[r],$0);
  print $0 }' file1 file2
Your name is $yourName$
My name is $myName$
His name is $hisName$

Regards

Last edited by cgkmal; 04-06-2011 at 04:24 AM..
# 6  
Old 04-06-2011
Quote:
Originally Posted by Chubler_XL
The $ char you have chosen for your marker makes it harder, as is is a meta char and means something to gsub...
Thanks a lot for your valuable point. As suggested by you we will replace the $ into @ symbol.

Now my requirement is little bit changed and I explained it below in detail:

1. ConfigFile

File1|@Name@|XXXX
File2|@Phone@|1234
File3|@Age@|25


Now the I need to read this config file and then I need to get the file in first column and need to find the value in second column in the file and then I need to replace it.

Please help me to write the snippet for this also please suggest some link to get the complete understanding about awk. I am beginner in UNIX
# 7  
Old 04-06-2011
Not real clear what you are after here. Some sssumptions I've made:

1. The order the files are output should remain the same as defined in config file
2. Multiple replaces may appear for each file
3. Replace tag (eg @Phone@) may have different values for different files

Here is the solution in awk:

Code:
awk -F\| '{if(!($1 in F))P[++f]=$1;F[$1];R[$1,$2]=$3}
END{
   for(i=1;i<=f;i++)  {
     while((getline < P[i]) > 0) {
         for(idx in R) {
            split(idx,val,SUBSEP);
            if(val[1]==P[i]) gsub(val[2],R[idx]);
         }
         print $0
     }
     close(P[i])
   }
}' ConfigFile


Last edited by Chubler_XL; 04-06-2011 at 07:35 PM..
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

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to update field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Replacing first field of file2 with the second filed of file1 for matching cases

Dear All, Need your help..:D I am not regular on shell scripts..:( I have 2 files.. Content of file1 cellRef 4};"4038_2_MTNL_KALAMBOLI" cellRef 1020};"4112_3_RAINBOW_BLDG" cellRef 134};"4049_2_TATA_HOSPITAL" cellRef 1003};"4242_3_HITESH_CONSTRUCTION" cellRef... (6 Replies)
Discussion started by: ailnilanjan
6 Replies

5. Shell Programming and Scripting

Retreive the records from file2 by using the first field in file1

Hi Freinds, i have a file1 as below file1 1|ndmf|fdd|d3484|34874 2|jdehf|wru7|478|w489 3|dfkj|wej|484|49894 file2 contains lakhs of records and not in sorted order i want to retrive only the records from file2 by searcing the first field of file 1 i used grep ^1 file2... (4 Replies)
Discussion started by: i150371485
4 Replies

6. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

search from file1 and replace into file2

I have 2 files: file1.txt: 1|15|XXXXXX||9630716||0096000||30/04/2012|E|O|X||||20120525135617-30.04.2012|PAT66OLM|STA||||00001|STA_0096000_YYYPPPXTMEX00_20120525135617_02_P.pdf|... (2 Replies)
Discussion started by: pparthiv
2 Replies

8. Shell Programming and Scripting

using field 2 in file2 to complete field 3 in file1

Hello, I was hoping someone could help me with this work related problem... basically what I want to do is the following: file2: 1 o 2 t 4 f 5 v 7 n 8 e 10 a file1: 1 : (8 Replies)
Discussion started by: smarones
8 Replies

9. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

10. Shell Programming and Scripting

Read each word from File1 and search each file in file2

file1: has all words to be searched. 100007 200999 299997 File2: has all file names to be searched. C:\search1.txt C:\search2.txt C:\search3.txt C:\search4.txt Outfile: should have all found lines. Logic: Read each word in file1 and search each file in the list of File2; if the... (8 Replies)
Discussion started by: clem2610
8 Replies
Login or Register to Ask a Question