Re: How can I do a "grep -v" equivalent in Import[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg91849] Re: How can I do a "grep -v" equivalent in Import[]?
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Wed, 10 Sep 2008 05:08:30 -0400 (EDT)
- References: <g9t6mq$j8s$1@smc.vnet.net> <200809081010.GAA27650@smc.vnet.net> <ga5kov$r8g$1@smc.vnet.net>
Jason Ledbetter wrote:
> Valid point:
> This will likely line wrap:
>
> --snip--
> CPU NFS CIFS HTTP Total Net kB/s Disk kB/s Tape kB/s Cache
> Cache CP CP Disk FCP iSCSI FCP kB/s
> in out read write read write age
> hit time ty util in out
> 24% 0 741 0 767 9381 6144 88815 0 0 81011 9
> 98% 0% - 31% 0 26 0 0
> --snip--
>
> I'm trying to avoid importing through a pipe and I'm trying to avoid
> preprocessing the data (for no real reason other than trying to get a handle
> on data processing in Mathematica).
>
> I've done the following:
>
> importTemp = Import[ToFileName[Directory[], "input.txt"], {"Lines"}];
> regexMatch = "^\d{2}.*";
> sysstat = StringCases[importTemp, RegularExpression[regexMatch]];
>
> which gets me ALMOST what I'm looking for.... I end up with the the
> non-matched lines being empty data sets which Part:partw doesn't like.
>
> e.g. output is like this:
>
> {{}, {}, {"24"}, {"29"}, ...
>
>
> Thank you very much for the replies to date.
>
> -jbl
>
I am still not 100% sure I know what you want to do, but assuming you
want just the first two digits of lines that start with 2 digits,
I am still not 100% sure I know what you want to do, but assuming you
want just the first two digits of lines that start with 2 digits,
tmp2 = Select[importTemp,
StringMatchQ[#, DigitCharacter ~~ DigitCharacter ~~ __] &];
Map[StringTake[#, 2] &, tmp2]
Alternatively, you could clean up the output that you have obtained already:
{{}, {}, {"24"}, {"29"}} /. {} :> Sequence @@ {}
With all the new string matching functionality, it is rarely necessary
to resort to C preprocessing, unless speed is critical (i.e. your input
file is very big!).
David Bailey
http://www.dbaileyconsultancy.co.uk
- References:
- Re: How can I do a "grep -v" equivalent in Import[]?
- From: David Bailey <dave@Remove_Thisdbailey.co.uk>
- Re: How can I do a "grep -v" equivalent in Import[]?