473,472 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need help in Perl code..

1 New Member
Hi All,

I'm a newbie in perl, have to do some patch work on my SDK so thaught perl can help me in doing this.

My requirement is as under:
---------------------------
root/
|--directory1---a.txt
|
|
|--directory2---b.txt


[ Hope this representation is clear!]

1. I've to insert some lines say "MY_COMMENT_HERE" in a.txt and b.txt files. This line is to be placed in between the code (not at start or end).

2. These lines shud be added to the code once only. If i re-run the script I shud receive an error message.

What I'm able to do:
--------------------
1. I've succedded in opening a file say "a.txt" and append my comment at the last (end of file). but this not a requirement.. actually it should append in between the code.

2. I am able to get the error message once I re-run my script. i.e. file is updated once only and not again and again..

Issues:
-------
1. I'm clueless about how to traverse across different folders in "root" directory and locate "a.txt" and then append the same...

2. How do I append in between the code..? I'm able to append at the end, but thats not required...

My code is as under:
place all code in
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!C:/Perl/bin/perl.exe -w  
  4.  
  5. $My_File="load_env.sh"; 
  6. $Target = " YOUR_COMMENT_HERE "; 
  7.  
  8. if ( !is_target_found( $My_File, $Target ) ) 
  9. {     open($My_File_FP,">>", "$My_File") || die("$MyFilewill not open for update!");     
  10. print $My_File_FP "\n  $Target \n";     
  11. close($My_File_FP); }    
  12.  
  13. sub is_target_found 
  14. { (my $My_File, my $Target) = @_ ;  
  15.    open(my $My_File_FP, "<", "$My_File") || die "$MyFile will not open for input!";
  16.      my $is_found = 0; 
  17.      TARGET_LOOP:
  18.      while ( <$My_File_FP> )
  19.      {        
  20.      if ( /$Target/ )  
  21.          {  
  22.              print " found $Target on line $. \n";
  23.              $is_found = 1 ;
  24.              last TARGET_LOOP;
  25.          } 
  26.     }   
  27.   close $My_File_FP;   
  28.   return $is_found; 
  29. }
  30.  
__________________________________________________ ______


Hope my approach is correct..

thanks for replying ASAP..
Regards,
Jun 21 '10 #1
1 1404
RonB
589 Recognized Expert Moderator Contributor
If you want to insert in the "middle", then you could load the file into an array and use splice to insert the line where needed.
http://perldoc.perl.org/functions/splice.html

For traversing the folders, you'd use File::Find
http://search.cpan.org/~jesse/perl-5...b/File/Find.pm
Jun 21 '10 #2

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

Similar topics

4
by: eight02645999 | last post by:
hi i need help with converting a piece of perl code to python ...... my $start = '\'; my $file = '\'; my $end = '\'; ..... while(<FILE>) #open a file {
1
by: Riyadh Hossain | last post by:
I am developing USPS shipping API for my site. All the work is going on testing phase. My site uses PHP/MySQL. Now I am working on USPS tracking/confirm. I can send request and get responses from...
1
by: eadavid | last post by:
HI, I am a beginner to perl and I need to write perl code for checking a good die in the wafermap. Here is the wafermap look like in file ....XXXX111111111X1111XXXX.......
7
by: group8perl | last post by:
I have this perl code written using Notepad which works fine using Apache but when i try to run the perl script on a Linix browser i get a program/script failure error. Can someone please help me...
3
by: behrooz | last post by:
Hi Dear friends I need a code in perl to receive a list reference and a hash reference copy the contents of the array into the hash such that the even elements of the array are the keys of the...
2
by: sainiamit25 | last post by:
Hi All, I have writte a perl code. The perl code simple do some calculation and based on that, it takes a specific action for a device. But i do not have any logging mechanism for this. By this i...
1
by: sandy2481 | last post by:
Hi Friends, I'm a beginner in perl. Can anyone please provide me with a perl code to read a HTML file and write into excel file. Regards, Sandy
3
by: andrewkl | last post by:
hi, I have the following Perl code that inserts a string to an Oracle DB via a stored procedure: #!/usr/local/bin/perl ## Perl v5.8.6 built for sun4-solaris use strict; BEGIN...
1
by: RaviRajhulk | last post by:
I need to execute the perl code after a delay of say an minute.. and i need to write to do this in the perl code itself....Plz help
1
by: rajpar | last post by:
Environment: Solaris (client + server) db2 version 7.2 latest fixpak (DB2 v7.1.0.111", "s050516" and "U803330") Compiler: gcc Here is my SP code executed on the client: CREATE PROCEDURE...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.