Find and replace based on other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace based on other file
# 1  
Old 02-20-2009
Find and replace based on other file

Hi All,

I need an enlightenment.
I have a file that some field need to be replaced.
sample : fileA.txt

abc,4#cik#221,text
abc,4#kus#343,text
...
...

what I need to replace is the "cik" and "kus" field to their fix value.
I have another file : fileB.txt that has value of "cik and "kus",
such as
cik,001
kus,002
...
...

so the result will be like this :

abc,4#001#221,text
abc,4#002#343,text
...
...

I can do manually it with awk
awk '/cik/{gsub(/cik/, "001)};{print}'

but, it's trouble some because of the large data.
do anyone have an idea so I can do it in 1 script ?
Thank you.
# 2  
Old 02-20-2009
Try this:

Code:
awk 'NR==FNR{a[$1]=$2;next}a[$2]{$2=a[$2]}1' FS="," fileB.txt FS="#" OFS="#" fileA.txt

Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a word based upon the file content

I have a file which contains timestamp and date in the second column. If the line contains one of the word then it need to be replace like below. Any help is appreciated. File: a smallint b timestamp c date d varchar O/P: a smallint ... (1 Reply)
Discussion started by: JoshvaPeter
1 Replies

2. Shell Programming and Scripting

Replace variable value in first file based on records in second

Hello , I have below files a) File A <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root xmlns="http://aaa/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="2.0"> <project name="source"> <mapping name="m_Source"> <parameter... (3 Replies)
Discussion started by: Pratik4891
3 Replies

3. Shell Programming and Scripting

Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space: ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ; ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ADZ YUL TS_ $300.00... (3 Replies)
Discussion started by: transat
3 Replies

4. Shell Programming and Scripting

Find and replace string based on entries on another file

I have a file1 with different with multiple fields and records File2 has 2 fields. I want to find and replace strings in file1 based on file2 values (I Want an exact match i.e. for example: when searching for DT:3, Substr of DT:34 should not be matched) File2: DT:3 foo_err DT:34 bar_frr... (8 Replies)
Discussion started by: aydj
8 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace based on a list

Hello, I have two files 'Master' and 'Rename'. Rename has two columns, the first containing old names and the second new names. I want to replace the old names in the 'Master' file with the new names. I ran the following and it does not seem to work. What needs to be done differently? Thanks,... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

6. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (1 Reply)
Discussion started by: perl_beginner
1 Replies

7. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (2 Replies)
Discussion started by: perl_beginner
2 Replies

8. UNIX for Dummies Questions & Answers

Find and Replace based on values in an file

I have a file in which I want to do multiple find and replace of strings. For a single replace I can implement: sed -i 's/old/new/' <input_file> I have a second file that contains the old and the new values like the arbitrary example below: old new xyz pqr ab 756 rst pqr... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

9. Shell Programming and Scripting

Replace character based on reference file problem asking

I got two files right now, input file (target file), reference file 1 (query file) reference file 1 (long list of data) KOLOPWMOPOPO ADASDASD ADSASDASDAD . . target file (one long liner content) ADASDASDTYUKOKOLOPWMOPOPOOPLUAADSASDASDADPOPOUYADADASDASD desired output file content ... (1 Reply)
Discussion started by: patrick87
1 Replies

10. Shell Programming and Scripting

replace words in file based on another file

Hello, Can someone kindy help me solve this problem..I am using SunOS shell script I got a file A with following content: This is my correct document. I wrote 111 This is my incorrect word , 222 This is my wrong statement 333 This is my correct document 444 This is my correct document 555... (9 Replies)
Discussion started by: kinmak
9 Replies
Login or Register to Ask a Question