![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
[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. |
|
||||
|
Quote:
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. |
|
|||||
|
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 |
|
||||
|
Quote:
I had a look at the output, but 122 lines of Perl ? Better stick with what I had. Thanks for responding. E.J. |
|
||||
|
Quote:
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|