[Perl] Find one string, change another string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Perl] Find one string, change another string.
# 1  
Old 06-09-2009
[Perl] Find one string, change another string.

Hi,

In principle I am searching for a Perl equivalent for this sed command:

Code:
sed "/TIM_AM_ARGS=/ s/60/1440/" $EDIT_FILE > $TEMP_FILE
cp $TEMP_FILE $EDIT_FILE

I was wondering if it needs to be like this, or that there other, shorter, alternatives:

Code:
open (TIMENVFILE, "<$timenvfile") or die "Cannot open $timenvfile\n";
my @lines = <TIMENVFILE>;
close (TIMENVFILE);

open (NEWTIMENVFILE, ">$timenvfile")  or die "Cannot open $timenvfile\n";
for (@lines) {
  if ( $_ =~ m/TIM_AM_ARGS/ ) { $_ =~ s/60/1440/}
  print NEWTIMENVFILE;
  }
close (NEWTIMENVFILE);

This is part of the file:

Code:
export TIM_ALMSRV_ARGS
TIM_AM_ARGS="-S -U 1440 nbsol151alarms"

I am mostly interested in this part of the code:

Code:
if ( $_ =~ m/TIM_AM_ARGS/ ) { $_ =~ s/60/1440/}

It is a one-liner, but perhaps there is a better way of doing it.

E.J.
# 2  
Old 06-09-2009
you could write a perl one-liner for this. but if you are still interested in a perl script, here's one:
Code:
#!/usr/bin/perl
# sed_like.pl
use strict
use warnings;

my $filename = shift;
my $newfile  = shift;

my @to_write;
open(FREAD, '<', $filename)  or  die "Failed to read file $filename : $!";
while (<FREAD>) {
    if (m/TIM_AM_ARGS=/) {
        s/60/1440/;    # may be you need word boundaries here? like this: s/\b60\b/1440/
    }
    push(@to_write, $_);
}
close(FREAD);

open(FWRITE, '>', $newfile)  or  die "Failed to write file $newfile : $!";
print FWRITE @to_write;
close(FWRITE);

run this perl script as:
Code:
perl sed_like.pl file.txt newfile.txt

edit: and here's the one-liner:
Code:
perl -pi -e 's/60/1440/  if (/TIM_AM_ARGS=/)' file.txt

# 3  
Old 06-09-2009
Quote:
Originally Posted by Yogesh Sawant
you could write a perl one-liner for this. but if you are still interested in a perl script, here's one:
Thanks.
So the if statement is okay this way, I assume.

Just out of interest, what is the difference between a perl one-liner and a perl script ?
Basically a one-liner is a script consisting of one line (which can be run from the command line with perl -e).
Or ?

E.J.
# 4  
Old 06-09-2009
Hi.

Most perl installations include s2p, a sed-to-perl translator. Here is an example:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate sed-to-perl translator, s2p.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed s2p
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
lines=$( s2p -f $FILE | wc -l )

echo " The s2p translator produced $lines lines of perl."

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
GNU sed version 4.1.5
s2p - ( /usr/bin/s2p Jan 1 09:56 )

 Data file data1:
/TIM_AM_ARGS=/ s/60/1440/

 Results:
 The s2p translator produced 122 lines of perl.

See man s2p for details ... cheers, drl
# 5  
Old 06-10-2009
Quote:
Originally Posted by ejdv
Thanks.
So the if statement is okay this way, I assume.

Just out of interest, what is the difference between a perl one-liner and a perl script ?
Basically a one-liner is a script consisting of one line (which can be run from the command line with perl -e).
Or ?

E.J.
The biggest difference is that a script is generally saved as a file and a one-liner isn't.

A one-liner can actually be more than one line. They are generally used for simple tasks, where a script can do much more complex stuff than you would typically want to try using a one-liner.
# 6  
Old 06-10-2009
Quote:
Most perl installations include s2p, a sed-to-perl translator.
Thanks for pointing out the sed-to-perl translator.
I had a look at the output, but 122 lines of Perl ?
Better stick with what I had.

Thanks for responding.

E.J.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

3. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

4. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

5. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

6. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

7. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

8. Shell Programming and Scripting

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies

9. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

10. Shell Programming and Scripting

Find String in Perl

Hi all, I am trying to search a SQL string and trying to find the table name in the query. For e.g. Select a,b,c,d,e,f from ITS.table where ITS.x=name In the above string i need to identify the word ITS and replace with owner. And also the name is not always ITS, it can be any... (11 Replies)
Discussion started by: mahalakshmi
11 Replies
Login or Register to Ask a Question