![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| To remove new line character | shihabvk | UNIX for Advanced & Expert Users | 7 | 06-18-2009 07:44 AM |
| bash while read how to remove \n character | papasj | Shell Programming and Scripting | 3 | 05-25-2009 09:24 PM |
| Remove last character of a term | Raynon | Shell Programming and Scripting | 6 | 03-20-2008 06:04 AM |
| Trying to remove single character from a line | Iz3k34l | UNIX for Dummies Questions & Answers | 5 | 07-07-2007 01:29 PM |
| Remove Last Character of Line | danhodges99 | Shell Programming and Scripting | 4 | 05-21-2003 09:30 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Hi,
I am trying to capture the server load and email me automatically. This is how it goes svrload=`uptime|awk '{ print $11 }'` Now this returns a value say "1.39,". How do i strip the "," from the returned value and convert this into a whole number to compare with a threshold? Thanks Murali |
|
||||
|
You're welcome. There are many ways to do this.
Here's the explanation: echo "1.39," | perl -pe 's/.$//' perl -pe This calls perl. The "p" causes Perl to pass through the input to the output, whether or not it is modified along the way. The "e" indicates to Perl that the expression (code) comes next. The expression is a simple regex substitution. The period stands for any character, and the dollar sign means "end of line." So this regular expression matches any character at the end of the line. The second part of the regular expression was left empty, so if the first part matches, it's replaced with nothing. Here's a more verbose regular expression, in Perl syntax, just FYI. $line =~ s/fred$/barney/; Here, I substitute "fred" at the end of the line with "barney." In the shorter example, I wanted to eliminate something, so there was nothing between the final two forward-slashes. Also, I didn't use the "variable =~" syntax, because in a Perl one-liner the line of input is assumed by Perl. It can also be explicitly referred to with the $_. So these two are identical: echo "1.39," | perl -pe 's/.$//' echo "1.39," | perl -pe '$_ =~ s/.$//' Finally, the "=~" syntax sets $_ to the result of running the regular expression substitution on it. In the shorter version that is implicit, and Perl understands it. ShawnMilo |
|
||||
|
I prefer Python, but you can't beat Perl one-liners for doing fairly complicated things in a script or on the command line.
Most of what I had to explain above was regular expression syntax, which is a whole different ball of wax. Don't let regexes scare you away from Perl. I highly recommend Jeffrey Friedl's book "Mastering Regular Expressions." I think the current edition is the third. Read the first 80 or so pages and you'll be a regex champ. ShawnMilo |
|
||||
|
how do i strip this line using perl regex.
$batch = /dataload/R3P/interface/Bowne/reports/RDI00244.rpt
I'd like to strip /dataload/R3P/interface/Bowne/reports/RDI and .rpt from this variable my output should be only 00244 how to do this using perl regex.I'm a newbie to perl and would like to know how to do this. thanks and regards, Ram. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| perl, perl regex, regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|