SHARE
TWEET

fanuc_extr

l0rdjcd Feb 27th, 2020 (edited) 205 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ##  This program extracts all programs individually from
  4. #   the *.ALL or *.CNC into individual files.
  5. #
  6. #   Applicable to FANUC version
  7. #       0i, 16i, 18i, 31i
  8. #
  9. #   Coded by Sebastian Staitsch
  10. #   s.staitsch@gmail.com
  11. #   Version 1.1
  12. #   last modified: 2020/03/19 00:30:35
  13. #   https://github.com/sstaitsch/fanuc
  14. #   https://pastebin.com/4wFFYnw3
  15. #
  16. #   === VIDEO ===
  17. #   https://youtu.be/PS7ZXYBhjWQ
  18. #
  19. #   NOTE: Files must be in the same folder as the script file
  20. #   USE: sh fanuc.sh
  21.  
  22. ########### BEGIN FUNCTIONS ###########
  23.  
  24. #GET VERSION
  25.     ver_get(){
  26.             if [ "$( egrep -o '<|>' $file)" ] ; then ver=31
  27.                 elif [ "$( egrep -o '<|>' $file )" = "" ] ; then ver=18
  28.             else ver=0
  29.             fi
  30.     }
  31.  
  32. #DELETE CR/LF
  33.     del_crlf(){
  34.         cat $file | tr -d '\r' > .tmp ; rm $file ; mv .tmp $file
  35.     }
  36.  
  37. #WRITE FILE
  38.     write_file(){
  39.         if [ -e $ver/$O_NAME ] ; then
  40.             (( cnt ++ ))
  41.             O_NAME2="$O_NAME-$cnt"
  42.             echo "%" > $ver/$O_NAME2
  43.             cat $file | head -$O_LNe >> $ver/$O_NAME2
  44.             echo "%" >> $ver/$O_NAME2
  45.             echo $( head -2 $ver/$O_NAME2 | tail -n1 ) >> $ver/list.txt
  46.         else
  47.             echo "%" > $ver/$O_NAME
  48.             cat $file | head -$O_LNe >> $ver/$O_NAME
  49.             echo "%" >> $ver/$O_NAME
  50.             head -2 $ver/$O_NAME | tail -1
  51.             echo $( head -2 $ver/$O_NAME | tail -n1 ) >> $ver/list.txt
  52.         fi
  53.     }
  54.  
  55. #DELETE FIRST LINE
  56.     del_firstln(){
  57.         tail -n+2 $file > tmp
  58.         rm $file
  59.         mv tmp $file
  60.     }
  61.  
  62. #DELETE LINES AFTER WRITE
  63.     del_ln(){
  64.             O_LNe=$( expr $O_LNe + 1 )
  65.             cat $file | tail -n+$O_LNe > tmp
  66.             rm $file
  67.             mv tmp $file
  68.     }
  69.  
  70. #FANUC 18
  71.     loop18(){
  72.         cnt=0
  73.         cp $file $file.bak
  74.         mkdir $ver 2>/dev/null
  75.         del_firstln
  76.         until [ "$( cat $file | wc -l )" = "0" ] ; do
  77.             O_NAME=$( egrep -o '^O{1}[0-9]{4}' $file | head -1 )
  78.             O_LNb=$( egrep -no '^O{1}[0-9]{4}' $file | egrep -o '^[0-9]{1,4}' | head -1 )
  79.             O_LNe=$( egrep -no '^O{1}[0-9]{4}|^#3000{1}|^%{1}' $file | head -2 | tail -n+2 | egrep -o '^[0-9]{1,4}')
  80.             O_LNe=$( expr $O_LNe - 1 )
  81.             write_file
  82.             del_ln
  83.         done
  84.         mv $file.bak $file
  85.     }
  86.  
  87. #FANUC 31
  88.     loop31(){
  89.         cnt=0
  90.         cp $file $file.bak
  91.         mkdir $ver 2>/dev/null
  92.         del_firstln
  93.         until [ "$( cat $file | wc -l )" = "0" ] ; do
  94.             O_NAME=$( egrep -o '^<{1}\S*>{1}' $file | tr -d '<>' | head -1 )
  95.             O_LNb=$( egrep -no '^<' $file | egrep -o '^[0-9]{1,3}' | head -1 )
  96.             O_LNe=$( egrep -no '^<|^%{1}' $file | head -2 | tail -n-2 | egrep -o '^[0-9]{1,4}')
  97.             O_LNe=$( expr $O_LNe - 1 )
  98.             write_file
  99.             del_ln
  100.         done
  101.         mv $file.bak $file
  102.     }
  103.  
  104. ########### END FUNCTIONS ###########
  105.  
  106. #MAIN PROGRAM
  107.     if [[ ! $( ls *.ALL 2>/dev/null ) && ! $( ls *.CNC 2>/dev/null ) ]] ;
  108.     then echo "No *.CNC or *.ALL Files found" ; sleep 2 ; exit ; fi
  109.     for file in *ALL ; do
  110.         del_crlf
  111.         ver_get
  112.         if [ "$ver" = "31" ] ; then cnt=0 ; loop31
  113.                 echo "Extracted $(ls $ver | wc -l ) Files. Found $( md5sum 18/* | sort | uniq -D -w 32 | wc -l ) duplicates"
  114.                 md5sum 31/* | sort | uniq -D -w 32 > $ver/md5_duplicates.txt
  115.                 md5sum 31/* | sort > $ver/md5_all.txt
  116.  
  117.             elif [ "$ver" = "18" ] ; then loop18
  118.                 echo "Extracted $(ls $ver | wc -l ) Files. Found $( md5sum 18/* | sort | uniq -D -w 32 | wc -l ) duplicates"
  119.                 md5sum 18/* | sort | uniq -D -w 32 > $ver/md5_duplicates.txt
  120.                 md5sum 18/* | sort > $ver/md5_all.txt
  121.  
  122.             elif [ "$ver" = "0" ] ; then echo "unknown File" ; exit
  123.         else
  124.         echo "Error detecting File" ; exit
  125.         fi
  126.     done
  127.     d=$(date +"%Y-%m-%d_%H:%M")
  128.     if [ -e 31/ ] ; then mv 31/ $d/
  129.         elif [ -e 18/ ] ; then mv 18/ $d/
  130.     fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top