The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-11-2008
vsmurali vsmurali is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 5
Question How to remove extraneous character

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
  #2 (permalink)  
Old 03-11-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
To strip off the last character:

echo "1.39," | perl -pe 's/.$//'

Output: 1.39



If you only want to strip off a comma and ignore anything else, replace the period in that regex with a comma.

ShawnMilo
  #3 (permalink)  
Old 03-11-2008
vsmurali vsmurali is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 5
Thumbs up

Awesome, it works. Thanks much.

Can you pls provide a little explanation on this?

Is this the only way to do it (or) is there any other way too?

Would it be possible to round the number too?

Thanks
Murali
  #4 (permalink)  
Old 03-11-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
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
  #5 (permalink)  
Old 03-11-2008
vsmurali vsmurali is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 5
Smile

cool.

Thanks for a detailed explanation.

I always wanted to learn perl language but never got around it.

Thanks

Murali
  #6 (permalink)  
Old 03-11-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
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
  #7 (permalink)  
Old 03-18-2008
ramky79 ramky79 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 56
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
Closed Thread

Bookmarks

Tags
perl, perl regex, regex, regular expressions

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 03:54 PM.


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