I have several files Log.100 Log.200,Log.300.... which are in an array
Log.100 file contains the data
gpg RC (54321)
Testion option OK
Local=IN
AIX.SHA1.So
and Log.200 file and other files contains data
gpg RC (54321)
Testion option OK
Linux.SHA1.so
All files misses the line 'Local=IN' except Log.100. So I want to copy the line Local=IN and then paste it in all the remaining files after the line 'Testing option OK'.
Note:- When I read the files by an array, I am not sure which file i.e (Log.100, Log.200,or Log.300 I will be reading first)
All the Log files are in a one single directory.
My plan was to use grep command in the perl script and traverse each file through an array and whenever it finds that line copy and save it in a variable and again traverse all the files and paste the variable value after the line 'Testion Option OK' in all the files.
Kindly provide the code.
your help will be appreciated
Thanks
11 1825 toolic 70
Recognized Expert New Member
With what part of this problem are you having difficulty?
Please be specific.
Do you know how to get a list of file names?
Do you know how to open a file?
Do you know how to test a line of a file for a specific string?
Show your code, and you will get help on how to improve it.
I have this code with me, but I am looking at easy and sophisticated code. Well I want the code to be very understandable and easy. - use strict;
-
use warnings;
-
my @filenames => (dw log.100 log.200 log.300);
-
foreach my $filename (@filenames) {
-
next if /\Afile\.100\Z/xms
-
eval {
-
open my $filehandle, '<', $filename or die Cannot open $filename;
-
open my $temphandle, '>', 'templog' or die Cannot open 'templog';
-
foreach (<$filehandle>){
-
print or die "error copying $filename";
-
print 'Local=IN' or die "Error adding line" if /Testing Option OK/;
-
}
-
close $filehandle or die "error closing $filename";
-
close $temphandle or die "error closing tempfile";
-
unlink $filename or die "error deleteing original $filename";
-
rename 'templog', $filename;
-
};
-
}
Kindly modify the code in a simple way, if possible.
I can understand the above code but I want to write it in a different way so that it looks simple.
toolic 70
Recognized Expert New Member
Your code does not compile. Please fix the syntax errors and post new code inside CODE tags which shows proper indentation.
numberwhun 3,509
Recognized Expert Moderator Specialist
I have added the code tags to your post. In the editor, there is a # symbol. If after you enter your code you highlight it, simply click the hash symbol and it will add the code tags for you.
Regards,
Jeff
Check if this is ok -
-
1. use strict;
-
2. use warnings;
-
3. my @filenames =(log.100, log.200, log.300);
-
4. foreach my $filename (@filenames) {
-
5. next if /\Afile\.100\Z/xms
-
6. eval {
-
7. open my $filehandle, '<', $filename;
-
8. open my $temphandle, '>', 'templog';
-
9. foreach (<$filehandle>){
-
10. print or die "error copying $filename";
-
11. print 'Local=IN' or die "Error adding line" if /Testing Option OK/;
-
12. }
-
13. close $filehandle or die "error closing $filename";
-
14. close $temphandle or die "error closing tempfile";
-
15. unlink $filename or die "error deleteing original $filename";
-
16. rename 'templog', $filename;
-
17. };
-
18. }
RonB 589
Recognized Expert Moderator Contributor
It appears to me that "powerfulperl" wants someone to write a custom and fully tested script rather than learning how.
My suggestion is to look/ask here http://rentacoder.com/cs/default.aspx
Dude I know what I wanted, I have gone through the code but there seems to be problem with the existing code of mine. I just need a true programmer who can guide me in sorting out my problem rather than wasting time by giving other site links.
If you can help then plz assist me.
RonB 589
Recognized Expert Moderator Contributor
Bill, the person that gave you the code, is a good Perl programmer and I suspect he put in the few extremely minor errors to see if you knew Perl.
Lets start with this line, which has 3 errors. Can you find and fix one of them? - my @filenames => (dw log.100 log.200 log.300);
This line is easier, it has 1 problem. Can you find it? - next if /\Afile\.100\Z/xms
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: News.Individual.NET |
last post by:
Hi All,
Is there a way to search in *.tgz file? I mean to search in files that are
included in that *.tgz file. Some ideas?
Thanks in advance!
Zlatozar
|
by: Frank Grellert |
last post by:
Hi,
does anybody have an idea how to read a particular line of a file like awk's
awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)
thanks for your help
Frank Grellert
|
by: Josh |
last post by:
I would like to search a text file on my server and then alert me on
if the current date exist in that file. Any one have any code that
searches text files.
thanks in advance
|
by: Ya Ya |
last post by:
I have a folder with a lot of PDF and CHM files.
I would like to develope an ASP.net application that enables the user to
search inside the content of those files.
How do I search inside those...
|
by: DraguVaso |
last post by:
Hi,
I'm looking for a way to Copy and Paste Files to the clipboard.
I found a lot of articles to copy pieces of text and bitmaps etc, but nog
whole files.
Whay I need is like you have in...
| |
by: sudhakaranr |
last post by:
HI,
Need your help please...
First thing, i want to open a directory and with in the directory i have 1000's of files and we need to read the file and i want a particular line which has...
|
by: mse07 |
last post by:
hi all
i search about statement in text file by this code :
LineFlag = LineFlag + 1
Line Input #1, strLine
If LineFlag = 15 Then
If InStr(1, strLine, " Transfer completed...
|
by: ravindarjobs |
last post by:
hello friends,
i want to seach for a file in the computer. i dont know the exact location where it is. so the requirement is to search entire hard disk and get the path of the specified file.
...
|
by: prassaad |
last post by:
HI!
I 'm working on linux .I hv difficulty for picking particular line from a file...
I like to make some manipulation on LINE such as
3 sda7 333333 66666 888888 444444
from file /PROC/DISKSTATS...
|
by: poolboi |
last post by:
hi all,
i've got a long long program that does deletion in excel file
but this excel files get it's information from a text file
i would like to automate things a little here
wondering if...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |