Find and replace values using a list from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace values using a list from a file
# 1  
Old 11-26-2013
Find and replace values using a list from a file

Hello Guys.

I have a very big file where I need to change some values according a list on other file.

The following file is were I have the values to be changed.

The field where I need to replace the values is

Code:
    File_Nb             :

File1.txt

Code:
Obs_Report_Result : [
]
# ===== (5) =====
Observer_Report : [
# ===============

  Version         : "5.0"
  Exploitation_Mode     : SLIP SWEEP

  Line_Report     : [
    Filter_Type             : 8N LIN
    Aux_Nb_Trace         : 5
    Seis_Nb_Trace         : 3840
    Total_Nb_Trace         : 3845
    Nb_Of_Dead_Seis_Channels     : 0
    Nb_Of_Live_Seis_Channels     : 3840
    Dead_Seis_Channels         : 
    Live_Seis_Channels         : 6260:4578-5056(1-240)    
                             6270:4578-5056(241-480)    
                             6280:4578-5056(481-720)    
                             6290:4578-5056(721-960)    
                             6300:4578-5056(961-1200)    
                             6310:4578-5056(1201-1440)    
                             6320:4578-5056(1441-1680)    
                             6330:4578-5056(1681-1920)    
                             6340:4578-5056(1921-2160)    
                             6350:4578-5056(2161-2400)    
                             6360:4578-5056(2401-2640)    
                             6370:4578-5056(2641-2880)    
                             6380:4578-5056(2881-3120)    
                             6390:4578-5056(3121-3360)    
                             6400:4578-5056(3361-3600)    
                             6410:4578-5056(3601-3840)
    SFL                 : 6260
    SFN                 : 4578
    Spread_Nb             : 351
    Spread_Type             : ABSOLUTE
    Acq_Error             : 
    ITB                 : FALSE
  ]

  Shot_Report     : [
    Swath_Name             : 2146
    Swath_ID             : 46
    Shot_Nb             : 532
    Line_Name             :  6333.0
    Point_Number         :  4817.0
    Point_Index             : 1
    Acq_Length             : 18000 # (msec)
    Sweep_Length         : 12000 # (ms)
    Pilot_Length         : 12000 # (ms)
    Record_Length         : 6000 # (ms)
    Sample_Rate             : 2000
    Total_Nb_Sample         : 3001
    Type_Of_Source         : VIBRO
    Source_Nb             : 3
    Tb_Window             : 20000
    Date             : Fri Nov 22 06:54:47 2013

    Julian_Day             : 326
    Cog_State             : ACTUAL
    Cog_Easting             : 747980.7
    Cog_Northing         : 2521522.4
    Cog_Elevation         : 500.0
    Cog_Deviation         :    2.4
    Uphole_Time             :  0.00 # (msec)
  ]

  Noise_Report     : [
    Noise_Elim_type         : NE OFF
    Thres_Hold_Var         : N/A
    Hist_Editing_Type         : N/A
    Hist_Range             : N/A # (dB)
    Hist_Taper_Length         : N/A # (power)
    Hist_Thres_Init_Val         : N/A # (dB)
    Hist_Zeroing_Length         : N/A # (msec)
    Low_Trace_Value         : 0 # (dB)
    Low_Trace_Percent         : 0
    Noisy_Trace_Percent         : N/A
    Low_Noisy_Verbose         : 
    Nb_Of_Window         : 0
  ]

  Process_Report     : [
    Type_Of_Process         : CORREL BEFORE
    Acq_Nb             : 1
    Correl_Pilot_Nb         : 3
    Auto_Cor_Peak_Time         : 1000
    Dump_Stacking_Fold         : 1
    Max_Of_Max_Aux_Char         : "   2.444985e+16"
    Max_Of_Max_Seis_Char     : "   3.784387e+14"
    Max_Time_Value_Verbose     : "0/1 : 0 454696.31"
    Tb_GPS_Time             : 1069127704236001
  ]

  Record_Report         : [
    File_Nb             : 1021
    Type_Of_Dump         : DUMP
    Type_Of_Test         : N/A 3
    Tape_Nb             : 408
    Tape_Label             : "N9"
    Record_Type             : NORMAL
    Blocking_Mode         : FALSE
    Device_Bypass         : FALSE
    Tape_Error_Text         : ""
    Tape_Time             : "Fri Nov 22 06:55:09 2013
"
    File_Count             : "955
    File_Per_Tape         : "9999"
  ]

  Comment             : "N/A"
  User_Header             : ""

In the other file2.txt I have the values to be changed and the new value to be used.

Code:
529   959
530   952
531   953
1010  1200
1021  1201

Example.
Code:
    File_Nb             : 1021

shouldbe changed to
Code:
    File_Nb             : 1201

I need to replace a lot of numbers and the original file is very big.

I have attached both files.

Thanks a lot for your help

Last edited by jiam912; 11-26-2013 at 01:35 PM..
# 2  
Old 11-26-2013
Try this:

Code:
awk 'NR==FNR{a[$1]=$2; next} /File_Nb/{$NF=a[$NF]}1' file2 file1

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 11-26-2013
It does not work Smilie, give a syntax error??
# 4  
Old 11-26-2013
I don't see any syntax error in Subbeh's awk program. If you are on SunOS or Solaris try using nawk or /usr/xpg4/bin/awk instead.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 11-26-2013
Quote:
Originally Posted by Yoda
I don't see any syntax error in Subbeh's awk program. If you are on SunOS or Solaris try using nawk or /usr/xpg4/bin/awk instead.
Hi Yoda,, yes there no error i was typing bad... but the code dont works I I want.. Smilie
# 6  
Old 11-26-2013
I ran it on the files that you attached and I got what you want!
Code:
$ grep File_Nb file1.txt
    File_Nb                      : 529
    File_Nb                      : 530
    File_Nb                      : 531
    File_Nb                      : 1010
    File_Nb                      : 1021

$ awk 'NR==FNR{a[$1]=$2; next} /File_Nb/{$NF=a[$NF]}1' file2.txt file1.txt | grep File_Nb
File_Nb : 959
File_Nb : 952
File_Nb : 953
File_Nb : 1200
File_Nb : 1201

This User Gave Thanks to Yoda For This Post:
# 7  
Old 11-26-2013
Thanks Yoda..

How can I keep the same format in the output file?
File_Nb : 529
Regards

Last edited by jiam912; 11-26-2013 at 02:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find common values in python list in ordered format

Hello All, There are various codes available to find the intersection between two sets in python. But my case is the following: I want to find the continual common pattern in different lists compared to list1. (i have underlined the longest common patterns in set a and set b) a = 2, 3, 5,... (1 Reply)
Discussion started by: Zam_1234
1 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

3. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

4. Shell Programming and Scripting

Find and replace many values

Dear Friends, I did the same question before in other thread, but I want to explain a little better my request. I am looking for a way how to find and replace a values in two files using a reference a file where are the key to replace. Basically, I want to keep a copy of the original file... (1 Reply)
Discussion started by: jiam912
1 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace based on a list

Hello, I have two files 'Master' and 'Rename'. Rename has two columns, the first containing old names and the second new names. I want to replace the old names in the 'Master' file with the new names. I ran the following and it does not seem to work. What needs to be done differently? Thanks,... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

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

7. Shell Programming and Scripting

Find and replace duplicate column values in a row

I have file which as 12 columns and values like this 1,2,3,4,5 a,b,c,d,e b,c,a,e,f a,b,e,a,h if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two... (5 Replies)
Discussion started by: nuthalapati
5 Replies
Login or Register to Ask a Question