LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2003, 10:01 PM   #1
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
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 --

Last edited by DavidPhillips; 08-23-2003 at 10:10 PM.
 
Old 08-24-2003, 04:56 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
this looks like a perfect job for flex.
 
Old 08-24-2003, 11:49 AM   #3
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Original Poster
Rep: Reputation: 58
ok,

I'll check out flex.

Thanks, I'll let you know.
 
Old 08-24-2003, 02:05 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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.

Last edited by david_ross; 08-24-2003 at 02:07 PM.
 
Old 08-24-2003, 02:58 PM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
go with david_ross's solution its much better than mine, as normal
 
Old 08-24-2003, 04:55 PM   #6
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Original Poster
Rep: Reputation: 58
ok, this is getting close to what I was looking for.

checking it out now,

thanks
 
Old 08-31-2003, 10:07 PM   #7
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Original Poster
Rep: Reputation: 58
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.

Last edited by DavidPhillips; 08-31-2003 at 10:09 PM.
 
Old 09-01-2003, 12:36 PM   #8
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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;
 
Old 09-01-2003, 08:00 PM   #9
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Original Poster
Rep: Reputation: 58
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.
 
Old 09-02-2003, 01:00 AM   #10
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Original Poster
Rep: Reputation: 58
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.

Last edited by DavidPhillips; 09-02-2003 at 01:05 AM.
 
Old 09-02-2003, 12:44 PM   #11
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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.
 
Old 09-02-2003, 08:36 PM   #12
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Original Poster
Rep: Reputation: 58
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,
 
Old 09-03-2003, 12:34 PM   #13
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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.
 
Old 09-03-2003, 02:50 PM   #14
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Original Poster
Rep: Reputation: 58
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
 
Old 09-03-2003, 03:25 PM   #15
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script text replacement... matthurne Programming 4 06-07-2011 06:46 PM
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
text editing from a script vogelbaugh Linux - General 2 09-22-2005 12:53 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Need text pattern compare script kscott121 Linux - Software 4 05-10-2004 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration