D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] getchar for bash?

 

On Sun, 2008-03-02 at 22:44 +0000, Simon Williams wrote:
> Hello.
> Does anyone know of a way that I can read *one* char at a time from 
> standard input in bash?

perl.

;-)

> I keep writing short (3 line) C programs to do this, but it's a bit 
> annoying having to compile it.

Umm, perl.

Honest, it's easiest to simply load some perl using bash.

> I should probably be using regex for this, but I can't work it out and I 
> swear it's much easier just to do it with getchar and loops.

I can't see how - regular expressions are always easier in perl than in
shell (or C) but then again, there is no regular expression that will
match all valid email addresses whilst correctly skipping all false
positives. You would need a customised set of expressions that provide
what you need for this file and be ready to adapt it again for other
similar files.

>  I guess the 
> main point is that I want to operate on a stream of chars rather than 
> line by line.

open (FOO, "$file") or die ("Cannot open input file. $!\n");
...
$var =~ /..../;

> Basically, I have a huge block of text with arbitrary newlines (which 
> should be ignored completely

$var =~ s/\n//;

> and email addresses scattered all over the place. I want to extract 
> these addresses and print them to standard out. I think the regex for 
> this should be '[a-zA-Z0-9]*@[a-zA-Z0-9]*', but I can't figure out how 
> to get sed or grep (what should I be using for this?)

Perl.

if ($var =~ /[a-zA-Z0-9]*@[a-zA-Z0-9]*/)
{

}

But note that this reg.exp. will still miss some valid email addresses
and parse some non-email strings that you might want to skip. There are
almost certainly Perl modules that can improve on your reg.exp. as well
as modules that can do the entire job in one go. It's not as if this is
a unique or unusual task, although wherever possible the original data
should be output in a more sane format.

If perl really wasn't available (say in Emdebian), I'd write some
bespoke C code to do the entire job in one go.

-- 


Neil Williams
=============
http://www.data-freedom.org/
http://www.nosoftwarepatents.com/
http://www.linux.codehelp.co.uk/


Attachment: signature.asc
Description: This is a digitally signed message part

-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html