From: John M. <ex...@h0...> - 2003-04-10 18:01:47
|
On Tue, Apr 08, 2003 at 03:10:44PM -0500, BLACKSTONE, J. DAVID wrote: > > > -----Original Message----- > > From: Christian Goetze [mailto:cg...@hq...] > > Sent: Tuesday, April 08, 2003 2:22 PM > > To: exp...@li... > > Subject: Re: [Expectperl-discuss] parsing non-ascii char's > > > > > > > Thx for your suggestion and I can see some merit here. > > > I am, however, starting to think I am off topic for this list. > > > > > > My real problem is that I do not have multiple lines of > > > data to parse. It's all one long line with lots of > > > control chars. > > > > > > When I print the data to a file > > > cat <filename> > > > shows output that looks just like my telnet session. > > > cat -v <filename> > > > shows all the ugly special chars > > > wc -l <filename> > > > shows I have one line of data and > > > cat <filename> | wc -l > > > still tells me I have one line of data. > > > > What you need here is a terminal emulator. If you're really > > unlucky, the other side will optimize redraws and you will > > not easily get the correct picture. As a first step, you > > could simply try ignoring the special chars, but you may end > > up having to emulate the screen and do "screen scraping". > > I have had to do some of that and it was not fun. Two things that might > help you: > > If you capture your session to a file, write a program to "play it back" > as a "movie" by clearing the screen and sending each character to the screen > in sequence with a small delay in between (like 0.1 seconds). > > The terminal garbage may likely include codes that position the cursor. I > think in my case those codes were something like escape, control-[ , row, > semicolon, height, control-G. Or something awful like that; I can't > remember what. But I could parse out the row and column and use it. I > wrote a program to take a cut and paste terminal screen capture and print it > out with - and | characters, with row and column numbers at the top and > bottom. I made printouts of these to see where my data displayed, and > checked the session capture log to make sure the row and column appeared > exactly in the control garbage. Watching the movie also became important > because sometimes things did not print out on the screen in the order you > would expect. > > It would theoretically be possible to write a program that maintained an > 80x24 "screen" array of characters, read the control codes, and updated the > appropriate places in the array. If you do, put it on CPAN!!! But I didn't > find it necessary. > > jdb Thanks to everyone for your help. The biggest things to help me were: use \c for reg exp's involving control chars; learning that someone else had faced text that included strings similar to "escape, control-[ , row, semicolon, height, control-G" etc (yuk!); reading my text with /usr/local/bin/less which is kind enough to display chars like "ESC[12;31HPort:"; Presently, I am successfully parsing this string with: #start by stripping the CTRL-[ chars $line =~ s/\c[//g; #strip the stupid NULL MAC $line =~ s/00-00-00-00-00-00//; #find the 1st MAC if ( $line =~ /(..\-..\-..\-..\-..\-..)/ ) { $first_mac = $1; } #throw away everything before the 1st MAC $line =~ s/^.*$first_mac/$first_mac/; #replace weird chars before each MAC $line =~ s/\[\d+\;1H/\n/g; #replace weird chars before each Port $line =~ s/\[\d+\;\d+H/\t/g; #find the last line and split it off if ( $line =~ m#\s+\[\d+K(Press\s.*)# ) { $last_line = $1; #(I need this $last_line data too) $line =~ s/\s+\[\d+KPress\s.*//; } @array = split ( /\n/, $line ); push ( @all_arrays, @array ); Seems like a lot a work to get what I want, but it works and I can get on with solving a problem. Thx again for all the help. -- John Mahoney ex...@h0... |