Replacing strings in a file from a def file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing strings in a file from a def file
# 1  
Old 01-18-2012
Replacing strings in a file from a def file

I am sure that there is something out there but before I learn Perl I thought I would ask. Here is what I need: I need a script or program to substitute string values in an xml file from a pair definition file

replace_string -d <file containing string pairs> original_file_in new_file_out

The definition file is a text file with pairs

dog cat
elephant baby

running the script on the original file would generate a new file where every occurrence of dog would be replaced by cat and elephant by baby and so on.

Sorry if this is a stupid question, I am rusty.
# 2  
Old 01-18-2012
Takes three arguments from the command line: pair-file input-file output-file

Code:
#!/usr/bin/env ksh

awk -v pfile=$1 '
    BEGIN {
        while( (getline<pfile) > 0 )
            list[$1] = $2;
        close( pfile );
    }
    {
        for( x in list )
            gsub( x, list[x], $0 );
        print;
    }

' $2 >$3
exit

Should run in most Bourne like shells
This User Gave Thanks to agama For This Post:
# 3  
Old 01-19-2012
Code:
$
$ # the definition file "f32.def"
$
$ cat f32.def
dog        cat
elephant   baby
$
$ # the data file "f32"
$
$ cat f32
this line has the word dog in it
this is the second line
this line has one elephant and another elephant in it
and one more elephant here
one dog and another dog and yet another dog here
the last line of this file...
$
$ # the Perl one-liner
$
$ perl -lne '$#ARGV == 0 ? {/^(\w+)\s+(\w+)$/ and $x{$1}=$2} : print join " ", map {$x{$_} // $_} split' f32.def f32
this line has the word cat in it
this is the second line
this line has one baby and another baby in it
and one more baby here
one cat and another cat and yet another cat here
the last line of this file...
$
$

tyler_durden
# 4  
Old 01-19-2012
Thanks!!

Works perfectly, exactly what I was looking for. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Homework & Coursework Questions

KLIBC .def file

I am working on klibc. I need to add a new command in kernel. klibc contains a SYSCALLS.def file which declares functions. Where can I find definition of functions declared in .def file? (1 Reply)
Discussion started by: Vasundhara08
1 Replies

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

4. Shell Programming and Scripting

Finding/replacing strings in some files based on a file

Hi, We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the... (9 Replies)
Discussion started by: Talkabout
9 Replies

5. Shell Programming and Scripting

Replacing Strings in a File

I have a input file which looks like this: Value1="" Value2="" Value3="" ListOfValues=" $Value1 $Value2 $Value3" I have another program which computes the values ($val1, $val2, $val3). So if $val1 is 'A', $val2 is 'B' and $val3 is 'C', I should edit the input file so it will look like:... (6 Replies)
Discussion started by: laiko
6 Replies

6. Shell Programming and Scripting

Replacing Strings in a File

I have a input file which looks like this: Value1="" Value2="" Value3="" ListOfValues=" $Value1 $Value2 $Value3" I have another program which computes the values ($val1, $val2, $val3). So if $val1 is 'A', $val2 is 'B' and $val3 is 'C', I should edit the input file so it will look like:... (0 Replies)
Discussion started by: laiko
0 Replies

7. Shell Programming and Scripting

Replacing strings in csv file.

Hi, I have a problem.. 1) I have a file that contains the lines as below : VRF-TM_DummyLab/mse02.lab,mse02.lab,ge-2/0/7.222 VRF-EMS_HUAWEI_MSAN_208/mse01.lab,mse01.lab,xe-1/0/0.208 2) I need a method to read this file, line by line from :... (5 Replies)
Discussion started by: msafwan82
5 Replies

8. Shell Programming and Scripting

Replacing strings in a log file and saves as a new csv

Hello Im new here.I need to replace strings and change it into csv format, or at least saves the file as csv if that would work :p. Heres an example of my scenario 1) I have a log file, named abc.log, and its like a txt based file anyway, and the content looks like this ... (2 Replies)
Discussion started by: tententen
2 Replies

9. Shell Programming and Scripting

replacing strings with text from other file

Hi, Im trying to update some properties files with text from another file: file1 user=xyz file2 user= after script file2 user=xyz Im using this reading the $QUARTZURL,ETC... from quartz.properties: echo... (1 Reply)
Discussion started by: mc1392
1 Replies

10. Shell Programming and Scripting

.def file in HP-UX Shell scripting

Hi Pals, I need some information related .def file in HP-Ux shell scripting. What actaully a .def file contains. It is having all definitions of some functions. But what is the relationship between a .def file and shell script. Can anyone give some examples. Thanks in Advance. Best... (1 Reply)
Discussion started by: manu.vmr
1 Replies
Login or Register to Ask a Question