1833004 Members
2917 Online
110048 Solutions
New Discussion

Script help

 
SOLVED
Go to solution
Gordon_3
Regular Advisor

Script help

Hi all, I find a script issue and hope u guys can help, suppose I have a output

07/22/2004 11:07:59,LUN 150 [150; RAID 5; hkhpdv11.oocl.com],0.0,0.0,0.0,0.0,0.0
,0.0,0.0
07/22/2004 11:12:59,LUN 150 [150; RAID 5; hkhpdv11.oocl.com],0.0,0.0,0.0,0.0,0.0
,0.0,0.0

which each , represent a field, how can I global delete those .0* field, i.e change all field value to integer? thx.

Gordon
Gordon
14 REPLIES 14
Denver Osborn
Honored Contributor
Solution

Re: Script help

Have you tried sed?

sed -e 's/.0//g' myfile.txt > mynewfile.txt


-denver
RAC_1
Honored Contributor

Re: Script help

Try this.

cat file|awk -F "]" '{print $1}'

Anil
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: Script help

We can do it with int() function on awk program,

Check the files for .0 with grep as,

while read line; do
grep -v "\.0" $line
if [[ $? -eq 0 ]]; then
echo $line | awk -F "," '{
print $1 $2
for (i=3;i print int($i)
}'
fi
done <
Easy to suggest when don't know about the problem!
Rodney Hills
Honored Contributor

Re: Script help

Possiblly a perl one liner-

perl -p -i -e 's/(\d+)\.\d+/\1/g' yourfile.txt

This will apply the changes directly to "yourfile.txt". If you rather generate a new file with the results-

perl -p -e 's/(\d+)\.\d+/\1/g' yourfile.txt >new_yourfile.txt

HTH

-- Rod Hills
There be dragons...
Gordon_3
Regular Advisor

Re: Script help

Hi all,

thx all for yr help, I find the perl solution do what exactly I want!! thx all again !

Gordon
Gordon
Gordon_3
Regular Advisor

Re: Script help

Hi all,

thx all for yr help, I find the perl solution do what exactly I want!! However, there is still problem that I donno, my input has some line that has "E" extentsion for number. e.g.

07/22/2004 11:52:59 0.0 0.0 4.834483779821818E-6 0.00330034092702
50276 0.0 0.5 0.0 150

Using the perl script, it give out E-6 as output but not '4', any idea? thx.

Gordon
Gordon
Hein van den Heuvel
Honored Contributor

Re: Script help


If you want the script to magically match up more number formats then you'll have to teeach it that through a more complex regular expression. In your case it looks like you are only interested in negative Exponentials (E-). You can express that with:

perl -p -e 's/(\d+)\.\d+(E-\d+)?/\1/g' infile > outfile.



What I added to the prio solution was:

(E-\d+)?

That means:
- a group: ()
- of E followed by - followed by one or more digits: E-\d+
- and that group must me present 0 or 1 times to match: ?


Hein.
Gordon_3
Regular Advisor

Re: Script help

Hi Hein,

Great, this fix the issue, yes I am a newbie for Perl, do donno the syntax of Perl, can u share with me some good source to learn Perl, esp for above line it's a very useful perl one-liner that I must use frequently, but since I donno the syntax, so donno how to modify the "rule"....

thx again.

Gordon
Gordon
Hein van den Heuvel
Honored Contributor

Re: Script help


I'd recommend the 'camel' books. O'reilly.

Check out: http://learn.perl.org/

and: www.perldoc.com


cheers,
Hein. [0 points for this]
Hein van den Heuvel
Honored Contributor

Re: Script help

Ah, I just remembered. Earlier today a similar question was asked in:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=646837

Check out the 'regular expression' suggestion. Because more then Perl, that's what you need to learn for tasks you describe.

Cheers,
Hein.

extracts:

Fred Ruffet Jul 28, 2004 12:02:11 GMT
-------------------------------------------
Let me tell you I consider the camel book (O'Reilly Perl Reference) as the better book on the subject. Notice that for learning there is also the good Lama Book (O'Reilly Learning Perl). The last editions are are up2date.


procura Jul 28, 2004 13:01:01 GMT
-------------------------------------------
Even knowing that I am a *real* Perl fanatic, the best book for regular expressions is Jeffrey Friedl's "Mastering Regular Expressions" from O'Reilly

An absolute must for anyone using vi, perl, awk, grep, sh, sed, emacs, ...
Gordon_3
Regular Advisor

Re: Script help

Hi Hein,

Sorry , but I cannot find the "camel books", can u pls enlighen, many thx thx.

Gordon
Gordon
H.Merijn Brand (procura
Honored Contributor

Re: Script help

Camel books are the books from O'Reilly that have a Camel on the front and deal with Perl

Start here: http://www.oreilly.com/catalog/prdindex.html
browse don to the books starting with "Perl" and find

http://www.oreilly.com/catalog/pperl3 (the bible. the only true reference, except for the on-line docs of course)
http://www.oreilly.com/catalog/lperl3 (the beginner book. Read once and pass on)
http://www.oreilly.com/catalog/perlpr4 (an absolute need. I still use it daily)
http://www.oreilly.com/catalog/perlckbk2 (the Ram book, a good addition)

Also visit my site to read:

--8<---
For those that are not (yet) comfortable with Perl, [0]Wendy van Dijk has put up a nice article on [1]use.perl.org titled "Where and how to start learning Perl".
-->8---
[0] http://perlmonks.org/index.pl?node_id=283154
[1] http://use.perl.org/
[2] http://perlmonks.org/index.pl?node_id=284175

My HP ITRC site pages can be found at (please use LA as primary choice):

USA Los Angeles http://mirrors.develooper.com/hpux/
SGP Singapore https://www.beepz.com/personal/merijn/
USA Chicago http://ww.hpux.ws/merijn/
NL Hoofddorp http://www.cmve.net/~merijn/

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Script help

And Jeffrey's book is also from O'Reilly, so when ordering, tick this one too :)

http://www.oreilly.com/catalog/regex2 (Mastering Regular Expressions)

Don't buy the first edition. It's very outdated now 2nd ed is out!

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Gordon_3
Regular Advisor

Re: Script help

Hi Procura, thousand thx!

Gordon
Gordon