LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2009, 05:09 AM   #1
kariagekun
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Rep: Reputation: 0
Split string and numeric with perl


Hi experts,

I have a file contains this line:

Mem: 906292k total, 873656k used, 32636k free, 164852k buffers

------------
I have made a perl script to split that lines

@top_file = <JX>;

foreach my $regex (@top_file){
if ($regex =~ /Mem: (.*)$/){
@data = split(/,\s/, $regex);

the result is : 906292k total873656k used32636k free164852k buffers

My question, how to delete all string k total, k used, k free, k buffers, so I just have a number e.g. 906292 873656 32636 164852.

Many thanks for your help.
 
Old 11-05-2009, 05:21 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if Perl is not a must, here's awk for you
Code:
$ awk '{for(i=2;i<=NF;i+=2)printf "%s ",$i+0}' file
906292 873656 32636 164852
 
Old 11-05-2009, 06:17 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Code:
grep -oP "\d*" ???
Yes, I know it's grep, but that "P" is to use perl style regex ... (hint maybe)
 
Old 11-05-2009, 06:37 AM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by syg00 View Post
Code:
grep -oP "\d*" ???
Yes, I know it's grep, but that "P" is to use perl style regex ... (hint maybe)
Just a note: the -P flag isn't available on many versions or builds of grep. (Also, the Perl-style regexes of grep and many other tools and libraries aren't really Perl regular expressions. That doesn't mean that they're bad or useless, but it can trip you up if you expect them to be identical to Perl's regexes.)

@kariageken: Rather than slurp the whole file in at once, I would read it in line by line. In terms of removing everything but the digits, I would do that using map as I did the split. Here's an example:
Code:
#!/usr/bin/env perl
use strict;
use warnings;

open my $fh, '<', 'file' or die "Can't open 'file' for reading: $!";

while (my $line = <$fh>) {
    if ($line =~ /^Mem:/) {
        my @data = map { s/[^0-9]//g; $_ } split(/,\s+/, $line);
        print "@data\n";
    }
}

Last edited by Telemachos; 11-05-2009 at 06:39 AM.
 
Old 11-05-2009, 07:31 PM   #5
kariagekun
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Hai All, thank you for your reply and appreciate for your solution

Regards
Kariagekun
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a numeric string with a sequence of numbers ziggy25 Linux - Newbie 13 12-03-2009 10:18 AM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 05:57 PM
problem in comparing numeric with string naren_0101bits Programming 1 01-28-2008 08:10 AM
split string izza_azhar Programming 6 01-18-2005 08:24 PM
perl string split problem! farhanali Programming 4 06-22-2003 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration