Perl: Match part of a line and replace with another line from the same file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Perl: Match part of a line and replace with another line from the same file
Hi,
I need to match and manipulate particular lines of data files. You can see the shortened sample file below. The actual contains data for every day for all 12 months.
The weather data that was not measured for whatever reason is input as 9999 or 999 so that the user knows that data is missing. Unfortunately, these data points cause the simulation software I'm using to crash.
What I would like to do is write a perl script that will open any *.cw2.a file, read it in, find the lines that have 9999 and replace them with the first line above which does not have 9999 in it and is not a day and month demarcation line or white space etc...
I've done a little bit of matching and batch processing with perl and awk but I'm unsure how to proceed with the specific line I/O. Any help is greatly appreciated.
Code:
*CLIMATE
# ascii climate file for binary db _OttawaNRC.iwec ,
# defined in: /cygdrive/c/documents and settings/briana/desktop/ottawanrc_2001.cw2.a
# col 1: Diffuse solar on the horizontal (W/m**2)
# col 2: External dry bulb temperature (Tenths DEG.C)
# col 3: Direct normal solar intensity (W/m**2)
# col 4: Prevailing wind speed (Tenths m/s)
# col 5: Wind direction (clockwise deg from north)
# col 6: Relative humidity (Percent)
OttawaNRC # site name
2001 45.32 -0.67 0 # year, latitude, long diff, rad flag
1 365 # period (julian days)
0 0 0 0 0 100
* day 18 month 01
0 -171 0 0 0 79
0 9999 0 9999 999 100
0 -194 0 17 50 77
0 -186 0 17 80 79
0 -196 0 19 80 83
0 9999 0 9999 999 78
0 9999 0 9999 999 79
0 -193 0 36 80 78
39 -179 130 36 70 79
68 -167 167 36 70 82
'sed -i.bak' edits the files inplace and makes backups of the original files with a '.bak' extension.
Normally sed treats multiple input files as one continuous file but with '-i' they are treated as being separate.
while (<>) {
chomp;
@f = split(/\s+/, $_);
if ($f[2] !~ /999/ && !/day|month/) {
$h = $_;
}
if ($f[2] =~ /999/) {
print $h."\n";
next ;
}
print $_ ."\n";
}
Thanks for your reply. I still can't seem to get this working quite right. I can get the awk and perl code to run and it will print out the file to my command window but its not actually editing the file so when I do grep as shown below. So even after I've run the awk or the perl commands, it still finds all the bad data lines i'm trying to replace with good data.
#!/usr/bin/perl
open CW2, ">>ottawanrc_2001.cw2.a" or die "cannot open file!";
while (<CW2>) {
chomp;
@f = split(/\s+/, $_);
if ($f[2] !~ /999/ && !/day|month/) {
$h = $_;
}
if ($f[2] =~ /999/) {
print $h."\n";
next ;
}
print CW2 $_ ."\n";
close CW2;
}
I had the same problem with Tinkster's code so I don't know if I'm misunderstanding something?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.