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.
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)
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)
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)
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)
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)
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)
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)
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
LEARN ABOUT SUSE
field_new
field_new(3FORM)field_new(3FORM)NAME
field_new - create and destroy form fields
SYNOPSIS
#include <form.h>
FIELD *new_field(int height, int width,
int toprow, int leftcol,
int offscreen, int nbuffers);
FIELD *dup_field(FIELD *field, int toprow, int leftcol);
FIELD *link_field(FIELD *field, int toprow, int leftcol);
int free_field(FIELD *field);
DESCRIPTION
The function new_field allocates a new field and initializes it from the parameters given: height, width, row of upper-left corner, column
of upper-left corner, number off-screen rows, and number of additional working buffers.
The function dup_field duplicates a field at a new location. Most attributes (including current contents, size, validation type, buffer
count, growth threshold, justification, foreground, background, pad character, options, and user pointer) are copied. Field status and the
field page bit are not copied.
The function link_field acts like dup_field, but the new field shares buffers with its parent. Attribute data is separate.
The function free_field de-allocates storage associated with a field.
RETURN VALUE
The function, new_field, dup_field, link_field return NULL on error. They set errno according to their success:
E_OK The routine succeeded.
E_BAD_ARGUMENT
Routine detected an incorrect or out-of-range argument.
E_SYSTEM_ERROR
System error occurred, e.g., malloc failure.
The function free_field returns one of the following:
E_OK The routine succeeded.
E_BAD_ARGUMENT
Routine detected an incorrect or out-of-range argument.
E_CONNECTED
field is connected.
SEE ALSO ncurses(3NCURSES), form(3FORM).
NOTES
The header file <form.h> automatically includes the header file <curses.h>.
PORTABILITY
These routines emulate the System V forms library. They were not supported on Version 7 or BSD versions.
It may be unwise to count on the set of attributes copied by dup_field being portable; the System V forms library documents are not very
explicit about what gets copied and what does not.
AUTHORS
Juergen Pfeifer. Manual pages and adaptation for new curses by Eric S. Raymond.
field_new(3FORM)