![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| replace lines in a file | TehOne | Shell Programming and Scripting | 4 | 03-17-2009 01:50 AM |
| replace multiple lines in multiple files | unihp1 | Shell Programming and Scripting | 1 | 09-21-2008 09:47 AM |
| replace multiple lines in file | nox | Shell Programming and Scripting | 2 | 08-18-2008 09:44 AM |
| Replace a perticular character of all lines of a file | abovais | Shell Programming and Scripting | 1 | 12-11-2007 05:35 AM |
| How can I replace multiple lines from different files | ranga27 | Shell Programming and Scripting | 2 | 02-07-2007 08:57 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Replace whole lines in the file from different files
Hi all, I have two parameter blocks in a configuration file, file1.conf. First parameter block starts with PWAT-PARM1 and ends with END PWAT-PARM1. Second parameter block starts with PWAT-PARM2 and ends with END PWAT-PARM2 (please see below in file1.conf). file1.conf is fixed width columns and numeric values are written in fortran format. I don't know how to update numeric values in file1.conf from two different files, file2.txt and file3.txt for each iteration. Thus, numeric values in the first parameter block (PWAT-PARM1) should be updated from file2.txt and, numeric values in the second parameter block (PWAT-PARM2) should be updated from file3.txt <file1.conf> Code:
PWAT-PARM1 *** < PLS> PETMAX PETMIN INFEXP INFILD DEEPFR BASETP AGWETP *** x - x (deg F) (deg F) 101 606 40. 35. 2. 2. 0.1 0.02 0. END PWAT-PARM1 PWAT-PARM2 *** <PLS > CEPSC UZSN NSUR INTFW IRC LZETP *** x - x (in) (in) (1/day) 101 606 0.1 1.128 0.2 0.75 0.5 0.1 END PWAT-PARM2 <file2.txt> Code:
101 606 20. 15. 1. 1. 0.1 0.12 0. 101 606 1.2. 25. 3. 0.5. 0.5 0.22 0. <file3.txt> Code:
101 606 0.2 1.376 0.1 0.25 0.1 1.1 101 606 1. 12.76 3.1 1.0 1.1 2.1 After first iteration, for example, file1.conf should look like this. Code:
PWAT-PARM1 *** < PLS> PETMAX PETMIN INFEXP INFILD DEEPFR BASETP AGWETP *** x - x (deg F) (deg F) 101 606 20. 15. 1. 1. 0.1 0.12 0. END PWAT-PARM1 PWAT-PARM2 *** <PLS > CEPSC UZSN NSUR INTFW IRC LZETP *** x - x (in) (in) (1/day) 101 606 0.2 1.376 0.1 0.25 0.1 1.1 END PWAT-PARM2 Seems to me, sed or awk would be a solution, but I don't know how to solve it. I searched this forum using key word "replace lines" and "replace pattern match", but no luck. Any help would be greatly appreciated. Thanks, Jae Last edited by Jae; 04-14-2009 at 06:24 PM.. |
|
||||
|
How to loop in getline from files
Hi Franklin, Thanks to you, I am almost got the solution, but I don't know how to loop in getline from two files. Only first line from file2.txt and file3.txt is updated in file.conf. For next iteration, file.conf should be updated with second line from these two files. Jae, Code:
awk 'BEGIN{getline v1 < "file2.txt";getline v2 < "file3.txt"}
/PWAT-PARM3/{print;getline;print;getline;print;getline
if (match($0,match(v1,$0))) {
print v1;
getline
} else {
# do some stuff here
print
}
next
}
/PWAT-PARM4/{print;getline;print;getline;print;getline
if (match($0,match(v2,$0))) {
print v2;
getline
} else {
# do some stuff here
print
}
next
}
{print}
' file1
Last edited by Jae; 04-14-2009 at 05:46 PM.. |
![]() |
| Bookmarks |
| Tags |
| awk match, replace, sed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|