LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script help -- text replacement (https://www.linuxquestions.org/questions/programming-9/script-help-text-replacement-85032/)

david_ross 09-03-2003 03:49 PM

If that is true then you can use this else statement at the top:
else{
if($name eq "029"){$var{'028'}.=",".join(':',@data)."\n"}
else{$var{$name}.=join(':',@data)}
}

This way both elements are put into the 028 variable.

This will look much better if you use absolute positioning.

DavidPhillips 09-03-2003 11:30 PM

cool,

That's going to work.

When you say absolute positioning does this mean that I can set the position of the output to match the template or would it be more to it than that?

I guess I can use sed to put some code into the template as it is converted to code.

The unix system we use has the same problem, you need to look at the output and adjust the tempate to correct it. However it's still better than the dos one we have now. It is preset into a format that cannot be changed. Also the output is gone before you can see it (no scrolling) so it's almost useless while it's running.

If you get time maybe you can show me an example of positioning.

Do you think there is an advantage using perl over sed?

I have made this from my little sed script, Seems a bit long, but maybe it's faster, better?

Code:


#!/usr/bin/perl -w
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  if 0;
$0 =~ s/^.*?(\w+)[\.\w+]*$/$1/;

use strict;
use Symbol;
use vars qw{ $isEOF $Hold %wFiles @Q $CondReg
            $doAutoPrint $doOpenWrite $doPrint };
$doAutoPrint = 1;
$doOpenWrite = 1;
# prototypes
sub openARGV();
sub getsARGV(;\$);
sub eofARGV();
sub printQ();

# Run: the sed loop reading input and applying the script
#
sub Run(){
    my( $h, $icnt, $s, $n );
    # hack (not unbreakable :-/) to avoid // matching an empty string
    my $z = "\000"; $z =~ /$z/;
    # Initialize.
    openARGV();
    $Hold    = '';
    $CondReg = 0;
    $doPrint = $doAutoPrint;
CYCLE:
    while( getsARGV() ){
        chomp();
        $CondReg = 0;  # cleared on t
BOS:;
# #!/bin/sed -f
# s/@\(0..\)/$var{'\1'}/g
{ $s = s /@(0..)/\$var{'${1}'}/sg;
  $CondReg ||= $s;
}
EOS:    if( $doPrint ){
            print $_, "\n";
        } else {
            $doPrint = $doAutoPrint;
        }
        printQ() if @Q;
    }

    exit( 0 );
}
Run();

# openARGV: open 1st input file
#
sub openARGV(){
    unshift( @ARGV, '-' ) unless @ARGV;
    my $file = shift( @ARGV );
    open( ARG, "<$file" )
    || die( "$0: can't open $file for reading ($!)\n" );
    $isEOF = 0;
}

# getsARGV: Read another input line into argument (default: $_).
#          Move on to next input file, and reset EOF flag $isEOF.
sub getsARGV(;\$){
    my $argref = @_ ? shift() : \$_;
    while( $isEOF || ! defined( $$argref = <ARG> ) ){
        close( ARG );
        return 0 unless @ARGV;
        my $file = shift( @ARGV );
        open( ARG, "<$file" )
        || die( "$0: can't open $file for reading ($!)\n" );
        $isEOF = 0;
    }
    1;
}

# eofARGV: end-of-file test
#
sub eofARGV(){
    return @ARGV == 0 && ( $isEOF = eof( ARG ) );
}

# makeHandle: Generates another file handle for some file (given by its path)
#            to be written due to a w command or an s command's w flag.
sub makeHandle($){
    my( $path ) = @_;
    my $handle;
    if( ! exists( $wFiles{$path} ) || $wFiles{$path} eq '' ){
        $handle = $wFiles{$path} = gensym();
        if( $doOpenWrite ){
            if( ! open( $handle, ">$path" ) ){
                die( "$0: can't open $path for writing: ($!)\n" );
            }
        }
    } else {
        $handle = $wFiles{$path};
    }
    return $handle;
}

# printQ: Print queued output which is either a string or a reference
#        to a pathname.
sub printQ(){
    for my $q ( @Q ){
        if( ref( $q ) ){
            # flush open w files so that reading this file gets it all
            if( exists( $wFiles{$$q} ) && $wFiles{$$q} ne '' ){
                open( $wFiles{$$q}, ">>$$q" );
            }
            # copy file to stdout: slow, but safe
            if( open( RF, "<$$q" ) ){
                while( defined( my $line = <RF> ) ){
                    print $line;
                }
                close( RF );
            }
        } else {
            print $q;
        }
    }
    undef( @Q );
}


It works fine, I'm just wondering if there is an advantage.


Congrats on the Mod status, We're glad you have decided to take it.

david_ross 09-04-2003 01:44 PM

The escape sequences are like this
#\033[TOP;LEFTH

just replace TOP and LEFT with the TOP and LEFT positions. so for your template it may be:
Code:

\033[2;30H *********** OBSERVERS LOG **********

\033[4;5HClient:$var{'001'}\033[4;35HContractor:$var{'002'}\033[4;65HCrew:$var{'003'}

\033[6;7HDate:$var{'036'}\033[6;41HTime:$var{'035'}\033[6;64HSwath:$var{'033'}

\033[8;2HSystem Parameters..

\033[10;2HAux Channels
\033[11;5HSample Rate:$var{'015'} usec\033[11;35HPreamp Gain:$var{'017'} dB
\033[12;11HAlias:$var{'013'} Hz $var{'014'} dB/Oct\033[12;40HLowcut:$var{'011'} Hz $var{'021'} dB/Oct\033[12;65HNotch:$var{'012'} Hz

\033[14;2HData Channels
\033[15;5HSample Rate:$var{'025'} usec\033[15;35HPreamp Gain:$var{'027'} dB
\033[16;11HAlias:$var{'023'} Hz $var{'024'} dB/Oct\033[16;40HLowcut:$var{'020'} Hz $var{'021'} dB/Oct\033[16;65HNotch:$var{'022'} Hz

\033[18;2HSource Parameters..

\033[20;5HStart Frequency:$var{'040'} Hz\033[20;40HEnd Frequency:$var{'041'} Hz
\033[21;9HStart Taper:$var{'044'}\033[21;44HEnd Taper:$var{'045'}
\033[22;8HSweep Length:$var{'042'} ms\033[22;43HStack Size:$var{'038'}

\033[24;5HFile Number: $var{'037'}\033[24;40HRecord Length:$var{'026'}\033[24;60HTime Break:$var{'034'} usec

\033[26;2HActive Spread..
\033[27;5HLine...\033[27;30HTraces:
$var{'028'}


DavidPhillips 09-04-2003 11:24 PM

ok, That's great.

I will look at setting this up.



Any recommendation on a good perl book for beginners.


Thanks again

david_ross 09-05-2003 12:22 PM

I'm afraid I'm not really a book person. The only one I have is "PERL, CGI and JAVASCRIPT Complete" but this is mostly web based stuff (this was the reason I started with perl in the first place)

All the answers I have needed I found on the web.

The positioning should work with your awk script too. As for the speed and performance difference I have no idea. I think you would need to run some tests with the live data.

DavidPhillips 09-05-2003 12:54 PM

cool

I guess I will start with the man pages.

david_ross 09-05-2003 12:56 PM

Any questions just give me a shout. (I usually lurk about the programming forum anyway)

DavidPhillips 09-05-2003 12:57 PM

ok, thanks


All times are GMT -5. The time now is 09:46 AM.