Sponsored Content
Top Forums Programming Perl: How to read from a file, do regular expression and then replace the found regular expression Post 302579120 by jessy83 on Sunday 4th of December 2011 10:36:53 PM
Old 12-04-2011
Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all,

How am I read a file, find the match regular expression and overwrite to the same files.

Code:
open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat";
open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat";
while (<DESTINATION_FILE>)
{
# print DESTINATION_FILE_2 "jessy\n";
 chomp($_);
# print "$_ is $hash{$_}\n";
   $line="CLIPPED_PIN_COUNT";
  #  print NEW_DESTINATION_FILE "$_\n";
 #if ($_=~/^$line/)
 if (/^CLIPPED_PIN_COUNT/)
 {
  print "original mathched:",$_,"\n";
  $var=$hash{$line};
  print "var is:",$var,"\n";
#Print out from tmptravl file 
  print "Obtain From tmptravl Fle:",$_,"\n";
#Split out
   @field_new=split/:/,$_;
# print @field;
   push @new_array_new, $field_new[$i];
   print "field",$i,":",$field_new[$i],"\n";
   $i ++;
   push @new_array_new, $field_new[$i];
   print "field",$i,":",$field_new[$i],"\n";
 
#Print out from original file
  print "Obtain From Original File:",$var,"\n";
  s/$_/$field_new[0]:$var/g;
  print "After substituition:",$_,"\n";
  print "-----------------------------------------\n";
         print NEW_DESTINATION_FILE "$_\n";
#   print DESTINATION_FILE,"s/$field_new[$1]/$var/";
#Subsititue original attribute to tmptravl attribue
  close (DESTINATION_FILE);
  close (NEW_DESTINATION_FILE);
 }

}

Do I need to open two file handlers? My purpose is to overwrite the attribute i get from tmptravl.dat and write to new_tmptravl.dat? However, when I write to new_tmptravl.dat, only the new substitution is written in my new_tmptravl.dat? May I know what is the root cause for it?
Thanks.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 12-05-2011 at 04:17 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression help in perl

Hi all, I am trying to match a multi line string and return the matching string in one line. Here is the perl code that I wrote: #!/usr/bin/perl my $str='<title>My title</title>'; if ($str =~ /(<title>)(+)(<\/title>)/ ){ print "$2\n"; } It returns : My title I want the... (3 Replies)
Discussion started by: sdubey
3 Replies

2. UNIX for Dummies Questions & Answers

Regular Expression - Replace if x contains y except if...

Hi all, I have a file which contains 1000s of lines of text. I need to delete all lines with the words "Red" EXCEPT if the line also contains the word "GREEN"... For example: ThisIs some random text that should be red deleted ThisIs some random text that should NOT be red deleted green ... (4 Replies)
Discussion started by: not4google
4 Replies

3. Shell Programming and Scripting

regular expression in perl

hi, i want to extract the sessionID from this line. QnA Session Id : here the output should be-- QnA_SessionID=128589 Thanks NT (3 Replies)
Discussion started by: namishtiwari
3 Replies

4. Shell Programming and Scripting

PERL regular expression

Hello all, I need to match the red expressions in the following lines : MACRO_P+P-_scrambledServices_REM_PRC30.xml MACRO_P+P-_scrambledServices_REM_RS636.xml MACRO_P+P-_scrambledServices_REM_RS535.xml and so on... Can anyone give me a PERL regular expression to match those characters ? ... (5 Replies)
Discussion started by: lsaas
5 Replies

5. Shell Programming and Scripting

Perl Regular Expression

Hello, I am trying to use perl LWP module to read and get a specfic URL page. The issue is that the URL ends with the data and time and time is not consistent it changes all the time. if anyone could help me how to write a regular expressin that would work in the LWP::UserAgent get function to... (0 Replies)
Discussion started by: bataf
0 Replies

6. Shell Programming and Scripting

How to continue numbering after a regular expression has been found

Hello, I have a file starting with: fixedStep chrom=chrX start=1 step=1 0.930 0.955 0.972 0.985 0.993 0.995 0.994 0.990 0.984 0.971 0.942 0.944 0.971 fixedStep chrom=chrX start=200 step=1 0.987 (2 Replies)
Discussion started by: jpoldot
2 Replies

7. Shell Programming and Scripting

Perl regular expression help!

Hi I am doing something basic like... if ($stringvariable =~ /have not typed/) I have a little problem because the 'not' in the expression gets highlighted as a kind of a '!'..what am I supposed to do in this situation? Thank you ---------- Post updated at 03:24 PM ----------... (1 Reply)
Discussion started by: vas28r13
1 Replies

8. Shell Programming and Scripting

Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code: grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done The above code things works fine,but sometimes gives erroneous... (3 Replies)
Discussion started by: joachimshaun
3 Replies

9. Shell Programming and Scripting

Perl regular expression

Hi , I have the below array my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43"); When i am trying to grep the contents of array and if mathced substituting with the digitis or some date format from the element like below my @action = grep { $_ =~... (7 Replies)
Discussion started by: ragilla
7 Replies

10. Shell Programming and Scripting

perl : regular expression to replace the strings

There are 2 strings as below. $str1 = "41148,,,,,,,,,,,,,,,,,,,,,,,,"; $date = "TUE 08-28-2012"; The output should be as below $str1 = "TUE 08-28-2012,,,,,,,,,,,,,,,,,,,,,,,,"; Could anyone please help with the perl regular expression or any other alternative? (3 Replies)
Discussion started by: giridhar276
3 Replies
ARP2ETHERS(8)						      System Manager's Manual						     ARP2ETHERS(8)

NAME
arp2ethers - convert arpwatch address database to ethers file format SYNOPSIS
arp2ethers [ arp.dat file ] DESCRIPTION
arp2ethers converts the file /var/lib/arpwatch/arp.dat (or the file specified on the command line) into ethers(5) format on stdout. Usu- ally arp.dat is an ethernet/ip database file generated by arpwatch(8). The arpwatch daemon in Debian will create different arp.dat depend- ing on its configuration. All of them will be available at /var/lib/arpwatch/. FILES
/var/lib/arpwatch - default directory for arp.dat arp.dat - ethernet/ip address database SEE ALSO
arpwatch(8), ethers(5), rarp(8), arp(8), BUGS
Please send bug reports to arpwatch@ee.lbl.gov. AUTHORS
Original version by Craig Leres of the Lawrence Berkeley National Laboratory Network Research Group, University of California, Berkeley, CA. Modified for the Debian Project by Peter Kelemen, with additions from Erik Warmelink. The current version is available via anonymous ftp: ftp://ftp.ee.lbl.gov/arpwatch.tar.gz This manual page was contributed by Hugo Graumann. ARP2ETHERS(8)
All times are GMT -4. The time now is 01:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy