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
IO::AtomicFile(3)					User Contributed Perl Documentation					 IO::AtomicFile(3)

NAME
IO::AtomicFile - write a file which is updated atomically SYNOPSIS
use IO::AtomicFile; ### Write a temp file, and have it install itself when closed: my $FH = IO::AtomicFile->open("bar.dat", "w"); print $FH "Hello! "; $FH->close || die "couldn't install atomic file: $!"; ### Write a temp file, but delete it before it gets installed: my $FH = IO::AtomicFile->open("bar.dat", "w"); print $FH "Hello! "; $FH->delete; ### Write a temp file, but neither install it nor delete it: my $FH = IO::AtomicFile->open("bar.dat", "w"); print $FH "Hello! "; $FH->detach; DESCRIPTION
This module is intended for people who need to update files reliably in the face of unexpected program termination. For example, you generally don't want to be halfway in the middle of writing /etc/passwd and have your program terminate! Even the act of writing a single scalar to a filehandle is not atomic. But this module gives you true atomic updates, via rename(). When you open a file /foo/bar.dat via this module, you are actually opening a temporary file /foo/bar.dat..TMP, and writing your output there. The act of closing this file (either explicitly via close(), or implicitly via the destruction of the object) will cause rename() to be called... therefore, from the point of view of the outside world, the file's contents are updated in a single time quantum. To ensure that problems do not go undetected, the "close" method done by the destructor will raise a fatal exception if the rename() fails. The explicit close() just returns undef. You can also decide at any point to trash the file you've been building. AUTHOR
Primary Maintainer David F. Skoll (dfs@roaringpenguin.com). Original Author Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com). REVISION
$Revision: 1.2 $ perl v5.18.2 2005-02-10 IO::AtomicFile(3)
All times are GMT -4. The time now is 07:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy