Replace values on file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace values on file
# 1  
Old 08-31-2017
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
Code:
21 1209
22 1210

file3.txt

Code:
SCI TB Timestamp Local : 8/30/17 7:52:52.980 AM
File # : 21
Is Raw : false
Shot # : 1752
Swath Name : 2567
Tape # : 3189
Tape Label : C4
________________________________________________________________________

SCI TB Timestamp Local : 8/30/17 7:51:52.980 AM
File # : 21
Is Raw : false
Shot # : 1752
Swath Name : 2568
Tape # : 3188
Tape Label : C4

The desired output should be (file4.txt)
Code:
SCI TB Timestamp Local : 8/30/17 7:52:52.980 AM
File # : 21
Is Raw : false
Shot # : 1752
Swath Name : 2567
Tape # : 3189
Tape Label : C4
________________________________________________________________________

SCI TB Timestamp Local : 8/30/17 7:51:52.980 AM
File # : 1209
Is Raw : false
Shot # : 1752
Swath Name : 2568
Tape # : 3188
Tape Label : C4

I use this code to fix this
Code:
awk 'NR==FNR{a[$1]=$2; next} /File # :/{gsub(/ /,"",$NF); $NF=" "a[$NF]}1' file2.txt FS=":" OFS=":" file3.txt > file4.txt

The problem I have is.. In file3.txt i have many File # : 21 which correspond to different record information.. Then what I like is to do the replacement ONLY where it is Tape # : 3188..

Code:
SCI TB Timestamp Local : 8/30/17 7:51:52.980 AM
File # : 1209
Is Raw : false
Shot # : 1752
Swath Name : 2568
Tape # : 3188
Tape Label : C4

Attached examples with more information and desired output file...

the original file is hug and contends a lot of data to replace, then i add small files with examples..

Appreciate your help Smilie
# 2  
Old 08-31-2017
You've got me - again and again and again. PLEASE make sure to remove DOS line teminators from text files before posting / attaching them here!
And, your results file is not consistent - not all matching records were modified. Try being more careful with data you post.
For your problem, try
Code:
awk '
BEGIN           {SRCH = "File # : "
                }
NR == FNR       {a[$1] = $2
                 next
                }
/Tape # : 3188/ {match ($0, SRCH "[0-9]*" )
                 T = substr ($0, RSTART+9, RLENGTH-9)
                 sub (SRCH  T, SRCH a[T])
                }
1
' /tmp/file2.txt RS=  ORS="\n\n" /tmp/file3.txt

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-31-2017
Hi RudiC,

It works perfectly.. Thanks a lot...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a file and replace values in a script

Hi , I have a property file placed in folder /usr/opt/temp/aorc.prop which has values given below . I need to read this file content and replace the node with actual values in a shell script . Each time the script shall replace the node value from the poperty file and execute a cfsend command and... (10 Replies)
Discussion started by: samrat dutta
10 Replies

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

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

4. Shell Programming and Scripting

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

Hi, I was having the following issue cat input hello1, my name is unix.com. I am awesome. Hope you know this, hello2! cat hello1.txt Hi Friends Hi Folks Hi Well-Wishers cat hello2.txt Honey Sweety Darling Required Output (8 Replies)
Discussion started by: jacobs.smith
8 Replies

5. Shell Programming and Scripting

Replace a field where values are null in a file.

Hi, I've a pipe delimited file and wanted to replace the 3rd field to 099990 where the values are null. How can I achieve it using awk or sed. 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S... (12 Replies)
Discussion started by: rudoraj
12 Replies

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

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

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

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

10. HP-UX

Replace all 'low values" in a file

Hi, I've to replace all low values (hex '00') in a flat file (containing more than 1,000,000 records) with space (hex '20'). I try to use: awk '{gsub("\x00","\x20")}' file_name and I obtain the following response: awk: Line AP000010536900577986 cannot have more than 199 fields.... (2 Replies)
Discussion started by: sw.an
2 Replies
Login or Register to Ask a Question