The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep string and then find another string doza22 UNIX for Dummies Questions & Answers 7 02-10-2009 04:47 PM
Find the position of a string and replace with another string bab123 Shell Programming and Scripting 6 01-21-2009 04:14 AM
Perl: Better way to match string within a string Juha Shell Programming and Scripting 3 01-11-2009 10:09 PM
Read a string with leading spaces and find the length of the string dayamatrix UNIX for Dummies Questions & Answers 2 11-13-2008 10:08 AM
Find String in Perl mahalakshmi Shell Programming and Scripting 11 04-26-2008 06:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-09-2009
ejdv ejdv is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 46
[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 (permalink)  
Old 06-09-2009
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
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 (permalink)  
Old 06-09-2009
ejdv ejdv is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 46
Quote:
Originally Posted by Yogesh Sawant View Post
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 (permalink)  
Old 06-09-2009
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
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 (permalink)  
Old 06-10-2009
ejdv ejdv is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 46
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.
  #6 (permalink)  
Old 06-10-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by ejdv View Post
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0