Replace two values in a file with input from two different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace two values in a file with input from two different files
# 8  
Old 05-16-2014
Perhaps something like this will work:
Code:
awk '
FNR == 1 {
        # This is the 1st line in a file; increment file count.
        f++
}
{       # Collect data from input files.
        d[f, cnt[f] = FNR] = $0
}
END {   # Verify that number of lines in the 1st two files are identical...
        if(cnt[1] != cnt[2]) {
                print "1st two files do not have the same number of lines."
                exit 1
        }
        # Verify that we were given 3 input files...
        if(f != 3) {
                printf("Found %d files, expected 3.\n", f)
                exit 2
        }
        # For each set of values found in the 1st two files...
        for(i = 1; i <= cnt[1]; i++) {
                # Set output filename.
                of = "output" i
                # For each line in the 3rd file...
                for(j = 1; j <= cnt[3]; j++) {
                        # Initialize output line to data from the 3rd file.
                        o = d[3, j]
                        # Replace occurrences of hello1 and hello2 with data
                        # from the 1st and 2nd files, respectively.
                        gsub(/hello1/, d[1, i], o)
                        gsub(/hello2/, d[2, i], o)
                        # Write the modified line to the output file.
                        print o > of
                }
                # Close the current output file.
                close(of)
        }
}' hello[12].txt input

As always, if you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 05-16-2014
Quote:
Originally Posted by Don Cragun
Perhaps something like this will work:
Code:
awk '
FNR == 1 {
        # This is the 1st line in a file; increment file count.
        f++
}
{       # Collect data from input files.
        d[f, cnt[f] = FNR] = $0
}
END {   # Verify that number of lines in the 1st two files are identical...
        if(cnt[1] != cnt[2]) {
                print "1st two files do not have the same number of lines."
                exit 1
        }
        # Verify that we were given 3 input files...
        if(f != 3) {
                printf("Found %d files, expected 3.\n", f)
                exit 2
        }
        # For each set of values found in the 1st two files...
        for(i = 1; i <= cnt[1]; i++) {
                # Set output filename.
                of = "output" i
                # For each line in the 3rd file...
                for(j = 1; j <= cnt[3]; j++) {
                        # Initialize output line to data from the 3rd file.
                        o = d[3, j]
                        # Replace occurrences of hello1 and hello2 with data
                        # from the 1st and 2nd files, respectively.
                        gsub(/hello1/, d[1, i], o)
                        gsub(/hello2/, d[2, i], o)
                        # Write the modified line to the output file.
                        print o > of
                }
                # Close the current output file.
                close(of)
        }
}' hello[12].txt input

As always, if you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
Don,

Beautiful!!! Absolutely beautiful!

Thanks a lot for your time and this wonderful piece.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace Stub Values In One Group Of Files With Actual Values From Another Group Of Files

I have two directories of files (new-config-files and old-config-files): new-config-files/this-db/config.inc.php new-config-files/that-db/config.inc.php new-config-files/old-db/config.inc.php new-config-files/new-db/config.inc.php new-config-files/random-database/config.inc.php etc. ... (4 Replies)
Discussion started by: spacegoose
4 Replies

2. Shell Programming and Scripting

Match value in two files and replace values in selected columns

The purpose is to check if values for column 3 and 4 in file1 match with column 1 in file2. If any value match do: 1) Replace values in file2 for column 2 and 3 using the information of file1 columns 5 and 6 2) Replace string ($1,1,5) and string ($1,6,5) in file2 with values of columns 7... (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Replace values between 2 files

I want to replace the third and fourth lines of a 2nd file by the first two lines of a file. Input: file_1 file_1.line_1 file_1.line_2 file_2 file_2.line_1 <file_2.line_2_blank> file_2.line_3 file2.line_4 <file_2.line_5_blank> Output: file_2.line1 <file_2.line_2_blank>... (1 Reply)
Discussion started by: arpagon
1 Replies

4. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

5. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

6. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

7. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

8. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

9. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

10. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies
Login or Register to Ask a Question