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 -- |
this looks like a perfect job for flex.
|
ok,
I'll check out flex. Thanks, I'll let you know. |
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. |
go with david_ross's solution its much better than mine, as normal :)
|
ok, this is getting close to what I was looking for.
checking it out now, thanks |
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:
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:
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. |
If you want to start doing formatting etc I would start turning to perl if it suits you:
Code:
#!/usr/bin/perl |
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. |
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 Code:
Code:
#!/bin/sed -f Code:
cat head template tail > obscript example Code:
cat template Code:
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:
Thanks, This is getting cool. |
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. |
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, |
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. |
It is a random number of repeated codes but they always come in this order
raw data Code:
|
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. |