Replace values in a file with values from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace values in a file with values from another file
# 1  
Old 10-13-2013
Replace values in a file with values from another file

Hi,

I have 2 input files:

File 1:

Code:
echo Name > create_Name.txt 
echo Group /dir/group, Name >> create_Name.txt

File 2:

Code:
Name AAA BBB CCC
group A B C
dir A1 B1 C1

................................

Need to replace the contents of File 1 with column 2, 3 & 4 values of File 2 where ever column 1 of File 2 matches with the words in File 1. The number of output files would be number of columns in File 2 minus 1. In this case it would be 3.

ie., Contents of Output File1 (cre_AAA.sh) would be:

Code:
echo AAA > create_AAA.txt
echo Group /A1/A, AAA >> create_AAA.txt

Contents of Output File2 (cre_BBB.sh) would be:

Code:
echo BBB > create_BBB.txt
echo Group /B1/B, BBB >> create_BBB.txt

Contents of Output File3 (cre_CCC.sh) would be:

Code:
echo CCC > create_CCC.txt
echo Group /C1/C, CCC >> create_CCC.txt

and so on ...

File 1 is static but File 2 would be dynamic. There could be many columns (AAA, BBB,...FFF) and parameters (Name, dir, group, ... location) in it.

Can someone please help me with this?

Thanx

-Gc

Last edited by Scrutinizer; 10-13-2013 at 07:38 PM.. Reason: code tags
# 2  
Old 10-13-2013
Try something like this:
Code:
awk '
  NR==1{
    n=NF
  } 
  NR==FNR{
    for(j=2; j<=n; j++) A[$1,j-1]=$j
    W[$1]
    next
  } 
  {
    r=$0
    for(j=1; j<n; j++) {
      $0=r 
      for(i in W) gsub(i, A[i,j])
      print > ("create_" A["Name",j] ".txt")
    }
  }
' file2 file1

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-13-2013
Thanks, Scrutinizer. But I am getting this error when running it:

Code:
awk: syntax error near line 6
awk: illegal statement near line 6
awk: syntax error near line 14
awk: illegal statement near line 14
awk: syntax error near line 15
awk: illegal statement near line 15


Last edited by Scrutinizer; 10-14-2013 at 10:45 AM.. Reason: code tags
# 4  
Old 10-14-2013
Hi, you would need to use the POSIX version of awk on Solaris: /usr/xpg4/bin/awk
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-14-2013
Works beautifully well !... Smilie Thanks, Scrutinizer.

How can I get all the new files created (create_*.txt) in a list?
# 6  
Old 10-14-2013
You are welcome.. You could try changing the second part to this:
Code:
    r=$0
    for(j=1; j<n; j++) {
      $0=r 
      f="create_" A["Name",j] ".txt"
      if(FNR==1) print f
      for(i in W) gsub(i, A[i,j])
      print > f
    }


Last edited by Scrutinizer; 10-14-2013 at 10:50 AM.. Reason: changed !N[f]++ to FNR==1
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 10-14-2013
Thanks, almost there ... it lists the files on screen. I need them saved to a file, so that they can be executed as a script.

Something like:

Code:
ls create_*.txt > create_master.txt


Last edited by Scrutinizer; 10-14-2013 at 10:46 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. Shell Programming and Scripting

Replace values on file

Gents, Please i need your help. Using the file2.txt i will like to replace values in file3.txt. Example in file 2 column 1 is the value to find in file3.txt and replace with value in colunm2 (file2.txt). Example file2.txt 21 1209 22 1210file3.txt SCI TB Timestamp Local : 8/30/17... (2 Replies)
Discussion started by: jiam912
2 Replies

3. UNIX for Dummies Questions & Answers

awk to replace values in one file using a second reference file

Hi, I'd be grateful for your help with the following: I have a file with a single column (file1). Let's say the values are: a b c 5 d I have a second, reference file (ref_file), which is colon-delimited, and is effectively a key. Let's say the values in it are: a:1 b:2 c:3 d:4... (4 Replies)
Discussion started by: aberg
4 Replies

4. Shell Programming and Scripting

Replace values using other file

Gents, Please can you help me. I need to update file1 using file2 values file1 S 44519.00 49349.00 1V1 0.0 0 0.0 0.0 0.0 0.0289091513 S 44513.00 48581.00 1V1 0.0 0 0.0 0.0 0.0 0.0289094319 S 44511.00 48605.00 1V1 0.0 0 0.0... (1 Reply)
Discussion started by: jiam912
1 Replies

5. Shell Programming and Scripting

Replace column values from other file

I have one file as it has the following format File1 S No Site IP Address 1 Australia 192.168.0.1/26 2 Australia 192.168.0.2/26 3 Australia 192.168.0.3/26 I need awk/sed command to replace the column2 value ( under Site) with some other... (8 Replies)
Discussion started by: samaritan
8 Replies

6. Shell Programming and Scripting

Referencing file for values and referencing another file to replace values

Hi I have a file which has values in each line: MP304,d40000 MP310,ff0000 etc I have another file which as the first value in it and is unique in the file(not repeated). I need to replace a string with the second value above. The second file contents is as follows:(snippet) <g ... (12 Replies)
Discussion started by: majikins
12 Replies

7. Shell Programming and Scripting

use some part of variable value to replace ../../ values in a file

hi, i have variable value as follows val="/dir1/dir2/dir3/dir4/dir5/dir6/file1" it is pointing to some file location. and i have another file as ../../dir4/file3 ../../dir4/dir5/file4 ../../dir7/file5 i want the output as /dir1/dir2/dir3/dir4/file3 (7 Replies)
Discussion started by: snreddy_gopu
7 Replies

8. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

9. UNIX for Dummies Questions & Answers

replace a column with values from another file

Dear all, I have a file1.pdb in pdb format and a dat file2 containing values, corresponding to the atoms in the pdb file. these values (file2.dat) need to be in the column instead of the 0.00 (file1) values for each atom in file1.pdb .(the red values must be replaced by the blue ones,in order)... (11 Replies)
Discussion started by: chen.xiao.po
11 Replies

10. UNIX for Dummies Questions & Answers

Replace values in a specified column of a file

Hello, I have a file with four columns and I would like to replace values in the second column only. An arbitrary example is: 100 A 105 B 200 B 205 C 300 C 305 D 400 D 405 E 500 E 505 F I need to replace the second column as shown below: ... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies
Login or Register to Ask a Question