Need help with perl/bash script to parse PicBasic file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Need help with perl/bash script to parse PicBasic file
I have recently been assigned to a project that uses a pic programmed with PicBasic. The code is nearly 11,000 lines long and is poorly, poorly written and structured. I am trying to disect the file to get a better understanding of the code.
I perfordmed the following op to get all line labels and subroutine labels and their corresponding line number and put them in their own file:
See the man pageon sort, but what you want is:
wizard@2[~]$ vi test.dat
wizard@2[~]$ sort -f -i -t: +1 test.dat
10868:ALTER_SW:
10887:CLear_home:
422:CNT_LOAD:
452:DISPLAY_ERROR_CODE:
492:DISPLAY_M:
10884:DOWN_SW:
10805:getkeyp:
303:GET_REMAINING_COATS:
349:GET_REMAINING_WASHES:
471:GOOD_STATUS:
10818:gotkey: ' Change row and column to key number 0 - 15
10862:HOME_SW:
292:LOAD_INFOPAC:
10833:LOOK_KEY:
10871:MENU_SW:
10865:NEXT_SW:
499:NXT_LP1:
266:PU_LOOP:
245:START:
10875:START_SW:
10878:STOP_SW:
10881:UP_SW:
287:WAIT_STEP_HOME:
wizard@2[~]$
If you want to replace the original file, add "-o test.dat" to the command.
I want to look at each sub label in sub_names_alpha.txt and record to a new file at what line number the subroutine is called.
For example if when I find the sub name VIBRATE in sub_names_alpha.txt I look it up in subcalls_alpha.txt and see it is called from lines 1020, 3405, 3712. Ithen want to write out a line in the following format to a new file
Code:
8500 VIBRATE: 1020 3405 3712
Any hints are appreciated!!!
I can post the entire files if anyone would like. They are pretty big though.
Last edited by cmfarley19; 10-06-2004 at 12:34 PM.
New addition...
I have yet a new file that contains all of the goto calls (goto_calls.txt)
Code:
10597 IF TEMP1.1 = 0 Then GoTo S1_LOW
10599 GoTo DS0
10603 IF TEMP1.0 = 0 Then GoTo S0_LOW
10605 GoTo CK_FOR_ADDR0
10612 GoTo SPEED_LOOP
10769 GoTo EESAVE
10838 IF PORTB.4 = 0 Then GoTo HOME_SW
10839 IF PORTB.5 = 0 Then GoTo NEXT_SW
10842 IF PORTB.6 = 0 Then GoTo ALTER_SW
10845 IF PORTB.7 = 0 Then GoTo MENU_SW
10850 IF PORTB.4 = 0 Then GoTo START_SW
I want to do the same referencing as above, but first I want to remove any text between the line number and the word GoTo.
I want to look at each sub label in sub_names_alpha.txt and record to a new file at what line number the subroutine is called.
For example if when I find the sub name VIBRATE in sub_names_alpha.txt I look it up in subcalls_alpha.txt and see it is called from lines 1020, 3405, 3712. Ithen want to write out a line in the following format to a new file
I have been playing with this most of the morning and I have hit a snag.
In refernce to post 5:
I have modified your script (thank you) a bit to accomodate some file discrepancies I had.
It now looks like this:
Code:
[cfarley@wombat tmp]$ cat subs.sh
#!/bin/bash
# clear out file
>subs.out
cat sub_names_master.txt | sed 's/\[.*\]'// | cut -d' ' -f2- | cut -d: -f1 | sort -f | uniq > subs.tmp
cat subs.tmp | \
while true
do
read SUB
if [ $? -ne 0 ]; then break; fi
# It seems to be choking on this line
# LINES=`grep " $SUB" subcalls_alpha.txt | cut -d' ' -f1 '`
LINES=`cat subcalls_alpha.txt | grep " $SUB" | cut -d' ' -f1 '`
echo $SUB: $LINES>>subs.out
done
rm -fv cat subs.tmp
echo "Results in subs.out"
I am getting this error for each iteration of the loop:
Code:
./subs.sh: command substitution: line 1: unexpected EOF while looking for matching `''
./subs.sh: command substitution: line 2: syntax error: unexpected end of file
I get the same error regardless of what version of the "grep" line above is used.
I need help figuring out the regex for this. Should be simple but I'm not getting it. I need a sed expression the will filter out lines where the first non-whitespace character is a comment ( ' ) .
Ex:
Code:
326 SET_VALUES: ' IF NOT LOAD DEFUALT
327 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
328 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
329 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
330 IF tone = 6 Then GoTo DRAWER_M 'UP BUTTON
331 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
332 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
333 IF tone = 13 Then GoTo DRAWER_M 'DWN BUTTON
334 'IF tone = 13 THEN GOTO drawer_m 'DWN BUTTON
So filter out lines 327, 328, 329, 331, 332, 334 and output lines 326, 330, 333
Code:
cat -n file.bas | sed <????>
I just realised as I was typing this that it will need to ignore the line numbers. So restated the sed statement should filter out lines where the first non-whitespace character after the line number is a comment ( ' ) .
Originally posted by cmfarley19 Back with another question...
I need help figuring out the regex for this. Should be simple but I'm not getting it. I need a sed expression the will filter out lines where the first non-whitespace character is a comment ( ' ) .
Ex:
Code:
326 SET_VALUES: ' IF NOT LOAD DEFUALT
327 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
328 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
329 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
330 IF tone = 6 Then GoTo DRAWER_M 'UP BUTTON
331 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
332 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
333 IF tone = 13 Then GoTo DRAWER_M 'DWN BUTTON
334 'IF tone = 13 THEN GOTO drawer_m 'DWN BUTTON
So filter out lines 327, 328, 329, 331, 332, 334 and output lines 326, 330, 333
Code:
cat -n file.bas | sed <????>
I just realised as I was typing this that it will need to ignore the line numbers. So restated the sed statement should filter out lines where the first non-whitespace character after the line number is a comment ( ' ) .
I don't understand why the '-n' in the cat ... now you've got 2 sets of numbers. Also, sed doesn't filter out lines, grep does...
Code:
wizard@2[~]$ cat file.bas
327 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
328 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
329 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
330 IF tone = 6 Then GoTo DRAWER_M 'UP BUTTON
331 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
332 'IF tone = 2 THEN GOTO DRAWER_M 'NEXT BUTTON
333 IF tone = 13 Then GoTo DRAWER_M 'DWN BUTTON
334 'IF tone = 13 THEN GOTO drawer_m 'DWN BUTTON
wizard@2[~]$ cat file.sh
cat -n file.bas | grep -v "^[0-9 ]*'"
wizard@2[~]$ sh file.sh
4 330 IF tone = 6 Then GoTo DRAWER_M 'UP BUTTON
7 333 IF tone = 13 Then GoTo DRAWER_M 'DWN BUTTON
wizard@2[~]$
What looks like several spaces in the grep regx is a space and a tab.
jmings,
The basic file itself does not have line numbers. I am piping several commands to get my desired output.
I first cat the file with the -n parameter to output the file with line numbers. That way I know what line number to go to in my editior.
Code:
cat -n hrc_60.bas
I next want to only see the lines that contain the strings "draw" or "load" so I pipe it to:
Code:
grep -iP "(draw|load)"
That produces 429 lines. A bunch of those lines are commented out so I want to eleiminate commented out lines.
I do not, however, want to eliminate lines that contain trailing comments. (see post #10)
That's where my sed command comes in.
Lastly I pipe the whole mess to:
Code:
cat -n
to get a count of how many instances of "draw" or "load" occur.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.