Referencing file for values and referencing another file to replace values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Referencing file for values and referencing another file to replace values
# 8  
Old 10-11-2013
Try now I modified it
This User Gave Thanks to Akshay Hegde For This Post:
# 9  
Old 10-11-2013
OSUM! Thank you thank you thank you! This is going to save so much of manual labor that would take us a week to do will now be done in a couple seconds. I would love to understand this command for future use and tweaking so will be looking up awk this weekend. If you would be so kind (if at all possible), please could you explain in summary what each part of your command is doing?
# 10  
Old 10-11-2013
Code:
awk -F, '                                                                 # Set Field Separator as comma
FNR==NR{
      A["\""$1"\""]=$2                                                    # Define Array Named A with column one as index and column2 as its value of file1
      next
       }
       {
         for(i in A)                                                      # Start looping on array A
                 if($0~i)                                                 # If line contains index then following
                     R=A[i]
                     if(R && $0~/style/){                                 # If R is not empty and string style is found ?
                                        j++                               # Increment j
                                        if(j==2){                         # If j==2 that is just above </g>
                                                gsub(/#.*;/,"#"R";");     # Replace  style value
                                                R=j=""                    # Reset counter and R
                                               }
                                       }
        }1'  file1 file2

---------- Post updated at 02:54 PM ---------- Previous update was at 01:45 PM ----------

Please use code tag for data and code as its mandatory in Forum
# 11  
Old 10-12-2013
Is that always the last line before </g> or can it be the second or third last one?
Is there always exactly two occurrences or can there be more/less?

---------- Post updated at 20:59 ---------- Previous update was at 20:28 ----------

It may be safer to specify the section in which you want to modify the style (here: the path section):
Code:
awk     'NR==FNR                {Arr[$1]=$2; next}
         ($2 in Arr)            {P=1; IX=$2}
         /<path/                {F=P}
         /style.*fill:#/ && F   {sub (/:#[^;]*;/, ":#" Arr[IX] ";")}
         /<\/g>/                {P=F=0}
         1
        ' FS=, file1 FS=\" OFS=\" file2

# 12  
Old 10-12-2013
Hi

Thanks for the input.

The pattern for now is always the 2nd style. However this may very well change in future. Still wrapping my head around the complexity of the last one - will give yours a shot as well. Please could you explain your code if possible?
# 13  
Old 10-12-2013
Code:
awk     'NR==FNR                {Arr[$1]=$2; next}                      # read file1: 2. fields into array Arr indexed by 1. field
                                                                        # work on file2 from here on field sparator: "
         ($2 in Arr)            {P=1; IX=$2}                            # if field 2 (id) has been recorded in Arr, keep that fact in P var; save $2 for index
         /<path/                {F=P}                                   # if path- section was entered, and id was matched, we (F)ound the right section
         /style.*fill:#/ && F   {sub (/:#[^;]*;/, ":#" Arr[IX] ";")}    # fill- pattern found AND correct section: modify (substitute command)
         /<\/g>/                {P=F=0}                                 # paragraph ended; reset all logical variables
         1                                                              # print all (modified or unmod.) lines in file2
        ' FS=, file1 FS=\" OFS=\" file2                                 # list files to work on and resp. field separators

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

Replace values in a file with values from another file

Hi, I have 2 input files: File 1: echo Name > create_Name.txt echo Group /dir/group, Name >> create_Name.txt File 2: 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... (8 Replies)
Discussion started by: gctex
8 Replies

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question