473,473 Members | 4,185 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Search for a particular line from a file and paste it in all the other files

14 New Member
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
Aug 21 '10 #1
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.
Aug 22 '10 #2
powerfulperl
14 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3. my @filenames => (dw 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 or die Cannot open $filename; 
  8.         open my $temphandle, '>', 'templog' or die Cannot open '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. }
Aug 22 '10 #3
powerfulperl
14 New Member
Kindly modify the code in a simple way, if possible.
Aug 22 '10 #4
powerfulperl
14 New Member
I can understand the above code but I want to write it in a different way so that it looks simple.
Aug 22 '10 #5
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.
Aug 22 '10 #6
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
Aug 22 '10 #7
powerfulperl
14 New Member
Check if this is ok

Expand|Select|Wrap|Line Numbers
  1.  
  2.    1. use strict; 
  3.    2. use warnings; 
  4.    3. my @filenames =(log.100, log.200, log.300); 
  5.    4. foreach my $filename (@filenames) { 
  6.    5.     next if /\Afile\.100\Z/xms 
  7.    6.     eval { 
  8.    7.         open my $filehandle, '<', $filename; 
  9.    8.         open my $temphandle, '>', 'templog'; 
  10.    9.         foreach (<$filehandle>){ 
  11.   10.             print or die "error copying $filename"; 
  12.   11.             print 'Local=IN' or die "Error adding line" if /Testing Option OK/; 
  13.   12.         } 
  14.   13.         close $filehandle or die "error closing $filename"; 
  15.   14.         close $temphandle or die "error closing tempfile"; 
  16.   15.         unlink $filename or die "error deleteing original $filename"; 
  17.   16.         rename 'templog', $filename; 
  18.   17.     }; 
  19.   18. }
Aug 22 '10 #8
RonB
589 Recognized Expert Moderator Contributor
This question has been cross posted in multiple forums and the one where this code was given can be found at http://perlguru.com/gforum.cgi?post=...=unread#unread
Aug 22 '10 #9
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
Aug 22 '10 #10
powerfulperl
14 New Member
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.
Aug 22 '10 #11
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?
Expand|Select|Wrap|Line Numbers
  1. my @filenames => (dw log.100 log.200 log.300); 

This line is easier, it has 1 problem. Can you find it?
Expand|Select|Wrap|Line Numbers
  1. next if /\Afile\.100\Z/xms
Aug 22 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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
5
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
3
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
3
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...
5
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...
4
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...
3
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...
13
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. ...
3
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...
5
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
1
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...
0
tracyyun
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...
1
isladogs
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.