How to remove extraneous character


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to remove extraneous character
# 1  
Old 03-11-2008
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  
Old 03-11-2008
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  
Old 03-11-2008
MySQL

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  
Old 03-11-2008
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  
Old 03-11-2008
Bug

cool.

Thanks for a detailed explanation.

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

Thanks

Murali
# 6  
Old 03-11-2008
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  
Old 03-18-2008
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

3. Shell Programming and Scripting

Need to remove a character using sed

Hi All, I have output like this below ldprod/03 ldprod/02 ldprod/01 ldprod/00 ldnprod/ ldnprod/030 I want only remove all character including / ldprod ldprod ldprod ldprod ldprod ldnprod (8 Replies)
Discussion started by: ranjancom2000
8 Replies

4. Shell Programming and Scripting

remove the first and last character of a string

How can i remove the first and last character of strings like below: "^^^613*" "admt130" "^^^613*" "123456" "adg8484" "DQitYV09dh1C" Means i wanna remove the quotes(""). Please help (17 Replies)
Discussion started by: proactiveaditya
17 Replies

5. Shell Programming and Scripting

How to remove first 2 character from file name

Hi All Please help me to remove the first 2 character from the file name. files are like this $ ls 12aman file 13atul si 56rana se I want to remove the first 2 char which are numbers. I want the o/p like thus aman file atul si rana se (8 Replies)
Discussion started by: atul9806
8 Replies

6. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

7. UNIX for Dummies Questions & Answers

How to remove \ character

Dear Members, I have a file which is a single line file. It has "\" character and i need to replace this character with a new line character. How can we do this? I tried with sed but it did not work. sed 's//"\n"/g' t1 > t2Thanks Sandeep (3 Replies)
Discussion started by: sandeep_1105
3 Replies

8. Shell Programming and Scripting

Remove a ^M character

Hi, I'd like to ask for some help with the following: I've cut a couple of columns of file1 to create file2 with the following code: cur -f 1,3,8 file1 > file2 Then I need to transfer file 2 from UNIX to Windows and use it further. Unfortunatelly, for some reason the line is displayed... (4 Replies)
Discussion started by: zajtat
4 Replies

9. UNIX for Advanced & Expert Users

To remove new line character

Hi, I am facing one interesting problem : I have a file which contains data like this 459,|1998-11-047|a |b |c \n efg | d|e | \n 459,|1998-11-047|a \n c|b |c \n efg | d|e | \n Basically what I have to do is , I have to remove all \n which is coming ( enclosed ) in between... (7 Replies)
Discussion started by: shihabvk
7 Replies

10. Shell Programming and Scripting

Remove last character of a term

Hi All, I have a few terms with a comma as the last character. Can any experts show me how to remove the last character? Note the the length of the term is not fix. Input: 1, 2, 12, 14, 103, 198, 3006, Output, 1 (6 Replies)
Discussion started by: Raynon
6 Replies
Login or Register to Ask a Question