Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-05-2009, 06:09 AM   #1
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, 06:21 AM   #2
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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

Rep: Reputation: 59
Originally Posted by syg00 View Post
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:
#!/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 07:39 AM.
Old 11-05-2009, 08:31 PM   #5
LQ Newbie
Registered: Oct 2009
Posts: 11

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



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:18 AM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 06:57 PM
problem in comparing numeric with string naren_0101bits Programming 1 01-28-2008 09:10 AM
split string izza_azhar Programming 6 01-18-2005 09:24 PM
perl string split problem! farhanali Programming 4 06-22-2003 08:08 AM

All times are GMT -5. The time now is 06:31 PM.

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