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/)

DavidPhillips 08-23-2003 10:01 PM

script help -- text replacement
 
I have some data that is captured to a file.

I need some ideas for a way to replace code with a comment.

Here is what I have...

Data stream from ...

cat /dev/ttyUSB0 >> $fname

that looks like this...

20 3
21 6
22 0
23 206
24 72
25 2000
26 90
27 48


20 - 27 are codes, followed by the data...


This sed command addresses the codes correctly..

tail -f $fname |sed 's/^.0[0-9][0-9]//'

and in this case simply removes the leading codes and outputs the data.


3
6
0
206
72
2
9
48




I have a config file that contains the definitions of the codes assigned to a variable.


c20="SEIS -- LOWCUT"
c21="SEIS -- LOWCUT SLOPE"
c22="SEIS -- NOTCH"
c23="SEIS -- ALIAS"
c24="SEIS -- ALIAS SLOPE"
c25="SEIS -- SAMPLE RATE"
c26="SEIS -- RECORD LENGTH"
c27="SEIS -- PREAMP GAIN"

I have a script file that will access the config file when called and returns the definition...

../sbin/code 20
SEIS -- LOWCUT
../sbin/code 21
SEIS -- LOWCUT SLOPE
../sbin/code 22
SEIS -- NOTCH

what I need to figure out is how to call the code script and replace the definition

Something like this ...
tail -f $fname |sed 's/^.024/SEIS -- ALIAS SLOPE/'

3
6
0
206
SEIS -- ALIAS SLOPE 72
2
9
48

Except it needs to act on all of the different codes as they come in, and if possible use the code script to call the definition in the config file, and output all definitions and data for each code.

The codes range from 00 to 99


Thanks for the help.

-- And Gordon just got tagged and spun out at Bristol --

kev82 08-24-2003 04:56 AM

this looks like a perfect job for flex.

DavidPhillips 08-24-2003 11:49 AM

ok,

I'll check out flex.

Thanks, I'll let you know.

david_ross 08-24-2003 02:05 PM

Why not use awk instead of sed? eg:
tail -f $fname | awk {'system("geterr "$1); print "\t\t"$2'}

This assumes your program that converts the numerical error to a literary one is called "geterr" and accepts the error code as an argument.

kev82 08-24-2003 02:58 PM

go with david_ross's solution its much better than mine, as normal :)

DavidPhillips 08-24-2003 04:55 PM

ok, this is getting close to what I was looking for.

checking it out now,

thanks

DavidPhillips 08-31-2003 10:07 PM

Howdy,

I am looking for another suggestion on how to accomplish this..


I have basically got a working sedscript for replacing the id codes with text from the script. It looks like this..


#!/bin/sed -nf
/^.000/s/^.000/\n ***** Observers Log Entry ***** \n/p;t
/^.001/s/^.001/Client:/p;t
/^.002/s/^.002/Company:/p;t
/^.003/s/^.003/Crew:/p;t
/^.004/s/^.004/Area:/p;t
/^.005/s/^.005/Map:/p;t
/^.006/s/^.006/Job:/p;t
/^.007/s/^.007/Reel Number:/p;t
~
~
~
/^.099/s/^.099/End Log Entry/p:t


The output would be like this

~~~~~~~

***** Observers Log Entry *****

Client: Exon
Company: TGC
Crew: 310
Area: Lost Horse
Map: SEI5781345
Job: 20030028
Reel Number: 03103567
~
~
~
End Log Entry



the data comes in from a com port and will repeat the sequence over and over.



What I would like to do some how is to allow a document to be formated as a tempate. The template will be used to format the output..

here is an example..

template document

Code:

   
    Client: @001                Contractor: @002                Crew: @003

    Prospect: @004                                                    Date: @036


This would allow someone to setup any format.

I would need to replace the @??? with the data.

Also the data if it comes in in any random order I am thinking that there would be a command to trigger at the end of each entry like maybe on .099

It would need to output the template, with the @??? replaced with data like this


Code:

   
    Client: Exon                Contractor: TGC                Crew: 310

    Prospect: Lost Horse                                      Date: 05/28/2003



Thanks a lot for the help.

The plan is to replace the DOS junk I have now with a linux machine that will generate logs.

I got the idea for the template from another system we have that runs on unix. It has the ability to do just what I am describing here.


I'm just not really sure how to read the incoming data and the template and output the log.

david_ross 09-01-2003 12:36 PM

If you want to start doing formatting etc I would start turning to perl if it suits you:
Code:

#!/usr/bin/perl

while(<STDIN>){
chop($_);
($name,@data)=split(/:/,$_);
if($_ =~ /^000/){undef %var}
elsif($_ =~ /^009/){&display}
else{$var{$name}=join(':',@data)}
}

sub display{
print <<"EOF";

***** Observers Log Entry *****

Client:$var{'001'}
Company:$var{'002'}
Crew:$var{'003'}
Area:$var{'004'}
Map:$var{'005'}
Job:$var{'006'}
Reel Number:$var{'007'}

End Log Entry

EOF
}

exit;


DavidPhillips 09-01-2003 08:00 PM

Ah yes, I figured that it would require a perl or c script to do this.

Thanks for the code, I'm gonna look into it some more. I think it's time to buy a book on perl.

DavidPhillips 09-02-2003 01:00 AM

Hey,

The script works great. I am able to use a template now.

Maybe not the best way but here is how it works

filename head

Code:

code:#!/usr/bin/perl

while(<STDIN> ){
chop($_);
($name,@data)=split(/:/,$_);
if($_ =~ /^000/){undef %var}
elsif($_ =~ /^009/){&display}
else{$var{$name}=join(':',@data)}
}

sub display{
print <<"EOF";

filename tail

Code:


EOF
}

exit;

now I run sed on the template

Code:

#!/bin/sed -f
s/@\(0..\)/$var{'\1'}/g

and do
Code:

cat head template tail > obscript

example

Code:

cat template

                *********** OBSERVERS LOG **********

Client:@001            Contractor:@002        Crew:@003

Date:@036              Time:@035              Swath:@033

System Parameters..

 Aux Channels
Sample Rate:@015 usec  Preamp Gain:@017 dB
Alias:@013 Hz @014 dB/Oct      Lowcut:@011 Hz @021 dB/Oct      Notch:@012 Hz

 Data Channels
Sample Rate:@025 usec  Preamp Gain:@027 dB
Alias:@023 Hz @024 dB/Oct      Lowcut:@020 Hz @021 dB/Oct      Notch:@022 Hz

Source Parameters..

Start Frequency:@040 Hz        End Frequency:@041 Hz
Start Taper:@044        End Taper:@045
Sweep Length:@042 ms    Stack Size:@038

File Number: @037      Record Length:@026        Time Break:@034 usec

 Active Spread..
Line:@028        Traces:@029

and get this

Code:


cat ~/testdta |./obscript

                *********** OBSERVERS LOG **********

Client:Exon            Contractor:TGC          Crew:310

Date:09/01/2003        Time:12:14:02          Swath:11

System Parameters..

 Aux Channels
Sample Rate:2000 usec  Preamp Gain:48 dB
Alias:206 Hz 72 dB/Oct  Lowcut:0 Hz 72 dB/Oct  Notch:0 Hz

 Data Channels
Sample Rate:2000 usec  Preamp Gain:48 dB
Alias: Hz  dB/Oct      Lowcut:0 Hz 72 dB/Oct  Notch:0 Hz

Source Parameters..

Start Frequency:12 Hz  End Frequency:96 Hz
Start Taper:.300        End Taper:.300
Sweep Length:6000 ms    Stack Size:12

File Number: 21      Record Length:3000        Time Break:600 usec

 Active Spread..
Line:123 501 - 651        Traces:135 - 285


Awesome


Now I have another problem...


If you look at @028 as an example

there will be data on code 028 several times between 000 and 099


I think I need to put the data in an array somehow and then pull it out



which should result in something like this


Code:


Active Spread..
Line:121 501 - 634        Traces001 - 134
Line:123 501 - 651        Traces:135 - 285


or probably could make this since the data only would be in an array,

Line  Stations          Traces
@028                        @029



Line  Stations            Traces
121  501 - 634          001 - 134
123  501 - 651          135 - 285


Thanks,

This is getting cool.

david_ross 09-02-2003 12:44 PM

The simple way to do it would be just to put the line breaks in when you create the variable - eg replace:
else{$var{$name}=join(':',@data)}

With:
else{
if($var{$name}){$var{$name}.="\n"}
$var{$name}.=join(':',@data);
}
This will obviously not work properly if you have the 2 side by side. If that is what you want you might want to look into escape sequences and absolute positioning.

DavidPhillips 09-02-2003 08:36 PM

cool, this outputs the data,


Traces:6 - 134
135 - 285
Line:121 501 - 628
123 501 - 651



I guess I will be working on getting it like this..


Line Traces
121 501 - 628 6 - 134
123 501 - 651 135 - 285


or this would be ok as well if I can do it

Traces 6-134
Line 121 501 - 628

Traces 135-285
Line 123 501 - 651


something like that.

I guess the best thing would be if I can do this

Traces Line Stations
$var{'029'} $var{'028'}
$var{'029'} $var{'028'}
$var{'029'} $var{'028'}



Looking into escape sequences now



thanks,

david_ross 09-03-2003 12:34 PM

Will there always be the same number of 029s and 028s? and will they always come in the form:
029: ...
028: ...
029: ...
028: ...
029: ...
028: ...


It doesn't matter if there are others in between. IF this is the case then it would be possible just to like these two variables together in the "else" section.

I can write something for you if the statements I made earlier are true.

DavidPhillips 09-03-2003 02:50 PM

It is a random number of repeated codes but they always come in this order


raw data

Code:


ÿ000

008 DISABLED

001                     

002                     

003       

004                     

005                         

006           

007           

018 3

074 TEST

075    209

019    1 -    67

010  3

011  6

012  0

013 206

014  48

015 2000

016  10

017  48

028 183  :    503 -    569

029  68 -  134

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 185  :    503 -    569

029  135 -  201

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 187  :    503 -    563

029  202 -  262

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 189  :    503 -    563

029  263 -  323

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 191  :    503 -    563

029  324 -  384

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 193  :    503 -    563

029  385 -  445

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 195  :    503 -    563

029  446 -  506

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 197  :    503 -    563

029  507 -  567

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 199  :    503 -    563

029  568 -  628

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 201  :    503 -    563

029  629 -  689

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 203  :    503 -    563

029  690 -  750

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 205  :    503 -    563

029  751 -  811

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 207  :    503 -    531

029  812 -  840

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 209  :    503 -    531

029  841 -  869

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

028 211  :    503 -    531

029  870 -  898

020  3

021  6

022  0

023 206

024  48

025 2000

026  10

027  48

076 3D

070  195

071  211

072    501


david_ross 09-03-2003 03:25 PM

Ok. Looking at that it looks like there will always be a 028 for every 029 - yes?


All times are GMT -5. The time now is 05:11 PM.