How to change certain values in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change certain values in a file
# 1  
Old 09-13-2012
How to change certain values in a file

Hi all,

i need help to replace certain values in a file. I need the script to check and match the ID and exNum1. if match, values in $3 (file2.txt) need to replace the value for 'START' (file1.txt) for each match.

The sample structure is like this:-

File1.txt
Code:
ID   P_6
START    235411
END    18763
//
ID    P_10
START    631012
END    32814
//
ID    P_133
START    389417
END    314124
//

File2.txt
Code:
ex    21204    22151    P_6    
S     21204    22151    P_6     exNum 2
ex    22217    22765    P_6    
S     22217    22765    P_6     exNum 1
ex    37193    37735    P_10    
S     37193    37735    P_10     exNum 5
ex    37862    38019    P_10    
S     37862    38019    P_10     exNum 4
ex    38076    38835    P_10    
S     38076    38835    P_10     exNum 3
ex    38880    39050    P_10    
S     38880    39050    P_10     exNum 2
ex    39093    39644    P_10    
S     39093    39644    P_10     exNum 1
ex    42305    42440    P_133    
S     42305    42440    P_133     exNum 3
ex    42496    42656    P_133    
S     42496    42656    P_133     exNum 2
ex    42657    42674    P_133    
S     42657    42674    P_133     exNum 1

Output (file1.txt) should be updated like this:
Code:
ID    P_6
START   22765 
END    18763
//
ID    P_10
START    39644
END    32814
//
ID    P_133
START    42674
END    314124
//

I parsed structure of file1.txt using PERL before but i don't know how to change the certain value for a field in it. Any help would be appreciated. Thanks
# 2  
Old 09-13-2012
You might want to try this one:
Code:
awk  'NR==FNR{if ($0~/exNum 1/) {rp[++cnt]=$3};next};
      /START/    {$2 = rp[++xtr]}
      1
    ' file2 file1

The first line collects all occurrences of field3 in lines with exNum 1 into an array, the second one replaces every START record's field 2 with the previously collected values in the order they were collected, and the 1 in the third line is an always TRUE pattern to print every line in file 1, eventually with replaced field 2.

Last edited by RudiC; 09-13-2012 at 07:21 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-13-2012
Hi RudiC,

It worked when i used the sample above. But, unfortunately it did not work when i use my real data. I noticed that because my real file have more IDs that are not match with file2. I have tried changing your script but the output that i got is ridiculous.

when the file.txt is like this:

Code:
ID    P_200 START    12412 END    12444 // ID    P_6  START   235411   END    18763  //  ID    P_10  START    631012  END    32814  //  ID    P_60 START    3112 END    3281 // ID    P_9 START    5812 END    6112 // ID    P_133 START    389417  END    314124  //

the correct output supposed to be like this:


Code:
ID    P_200
START    12412
END    12444
//
ID    P_6 
START   22765 
 END    18763 
// 
ID    P_10 
START    39644 
END    32814 
//
 ID    P_60
START    3112
END    3281
//
ID    P_9
START    5812
END    6112
//
ID    P_133
START    42674 
END    314124 
//

The values of START for ID P_200, P_60 and P_9 should remain unchanged as there are no match in file2 for all of them.appreciate your help on this. Thanks

Last edited by redse171; 09-14-2012 at 02:08 PM..
# 4  
Old 09-15-2012
OK, try this (assuming you have a file with line breaks):
Code:
awk     'NR==FNR{if ($0~/exNum 1/) {rp[$4]=$3};next};
         /ID/           {tmp = $2}
         /START/        {if (tmp in rp) $2 = rp[tmp]}
         1
        ' file2 file1

btw, why don't you give the correct sample data in the first place?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-15-2012
Hi RudiC,

Yes, your new code worked great! Thanks..

yeah, i did not give the right sample. Sorry about that. Smilie. Should be more careful next time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change 2 values in 2 different lines

Hello all, I have a file looks like a xml file: .... <SrcIntDef>WAUXDFXXX<\SrcIntDef> <SrcIntRep>WUBGIUNXXX<\SrcIntRep> ... For these 2 lines I will change the values. So for result it should look like: ... <SrcIntDef>WUBGIUNXXX<\SrcIntDef> <SrcIntRep>WAUXDFXXX<\SrcIntRep>... (4 Replies)
Discussion started by: API
4 Replies

2. UNIX for Beginners Questions & Answers

How to change values in xml file?

I have xml file like below, i want change the values at default-value place of each argument name using shell script. like where argument name= protocol and default-value=tcp, where argument name =port and default-value= 7223, where argument name = username and default-value=test, example ... (12 Replies)
Discussion started by: s1s2s3s4
12 Replies

3. Shell Programming and Scripting

Change values in .conf file with a script

This is my first time posting here...so be gentle. Suppose I have a test.conf file that contains a value such as a IP. I would like to be able to use the Dialog Utility in Linux to allow me to enter the new IP in a Dialog form...and the results get saved into the test.conf file in the place... (4 Replies)
Discussion started by: calahanp
4 Replies

4. Shell Programming and Scripting

Change nice values of a queue

Hello Guys I need your suggestions for the below We have few unix jobs running in different queues Is there any way I can manage which jobs goes to which queue and to change their nice value as well Thanks a lot for your valuable suggestions (1 Reply)
Discussion started by: Pratik4891
1 Replies

5. Shell Programming and Scripting

Finding change in values

I have an array X = ( -100 -90 -80 -70 -60 -50 -40 30 40 50 60 70 80 90 100 ) I want to find the place where values change from negative to positive. (8 Replies)
Discussion started by: kristinu
8 Replies

6. Shell Programming and Scripting

How to change values in datafile from source file

Hi all, I have this source file source.txt : hdrEthernetSwitch1IPAddress 112.13.9.34 hdrEthernetSwitch2IPAddress 112.13.8.245 noOfRevASLAvailable 8000 noOfRevASLInUse 122 maxDOSPayload 7777 pcmdEvdoRUM Enabled pcmdEvdoFlow Enabled And I have this datafile to be modified : ... (5 Replies)
Discussion started by: luna_soleil
5 Replies

7. Shell Programming and Scripting

Perl script to change values in datafiles

Hi all, Let say I have 2 files, 1 is source file and another is destination file. Source file contains the following : Kitten Dogs Donkey Chicken Turkey And destination file contains : Kitten, 0 Dogs, 0 Donkey, 0 Chicken, 0 Turkey, 0 Kitten, 0 Dogs, 0 Donkey, 0 (16 Replies)
Discussion started by: luna_soleil
16 Replies

8. Shell Programming and Scripting

How do I change the values in a file?

TRASH_PATH:~/deleted/ MAX_VERSIONS:5 FILE_MAX_SIZE:1024 FOLDER_MAX_SIZE:8096 TRASH_MAX_SIZE:1024 LOG_MAX_SIZE:100 how do i change the value of TRASH_MAX_SIZE to 2040 using the script? the filename is config.ini please advice Use code tags, ty. (5 Replies)
Discussion started by: classic
5 Replies

9. Shell Programming and Scripting

How to change values in certain column only in every line (any script)

Let say in a file I have lines of data like this : 13;2073;461496;15075341;3;001f7d3a;2042063674; 13;2074;461446;15080241;6;001ed33a;2042020154; 13;2075;461401;15085270;6;001f593b;2042054459; 13;2076;461381;15087160;6;001f7483;2042061443; 13;2077;461419;15083419;6;001eca1a;2042017818; I... (3 Replies)
Discussion started by: luna_soleil
3 Replies

10. Shell Programming and Scripting

Change the Values in a file

I have a data file. I want to write a shell script that reads a data file and reads position 19 thru 24. if the data in those fields is 002006, than it should change it to 002007. example: hello world hello 002006 hello world hello world hello world hello 002005 hello world hello world... (6 Replies)
Discussion started by: rudoraj
6 Replies
Login or Register to Ask a Question