Advertisement
Guest User

incrementing integers in sed

a guest
Aug 12th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.94 KB | None | 0 0
  1. #!/bin/sed -f
  2. #Increment integers. Handles multiple numbers per line.
  3.  
  4. #bracket numbers with < >
  5. s/[0-9]\+/<&>/g
  6.  
  7. #tail lines with how many times to increment, less one.
  8. #set addresses here to get multiple increments on selected lines
  9. /[0-9]\+/s/.*/& IIII/
  10.  
  11. #start of increment loop
  12. :a
  13.  
  14. # replace all trailing 9s by _
  15. :d
  16. s/9\(_*\)>/_\1>/
  17. td
  18.  
  19. # incr last digit only.  The last line adds a most-significant
  20. # digit of 1 if we have to add a digit.
  21. s/8\(_*\)>/9\1>/g  
  22. s/7\(_*\)>/8\1>/g  
  23. s/6\(_*\)>/7\1>/g  
  24. s/5\(_*\)>/6\1>/g  
  25. s/4\(_*\)>/5\1>/g  
  26. s/3\(_*\)>/4\1>/g  
  27. s/2\(_*\)>/3\1>/g  
  28. s/1\(_*\)>/2\1>/g  
  29. s/0\(_*\)>/1\1>/g  
  30. s/<\(_*\)>/<1\1>/g
  31.  
  32. #/[<0-8]_*>/s/.*/NumLine & numline/g
  33.  
  34. #replace _ with 0
  35. y/_/0/
  36.  
  37. #end of increment loop
  38. #first, an empty loop to 'flush' status
  39. tb;:b
  40. #remove one 'I' from the tail, and loop until none left        
  41. s/ \(I*\)I$/ \1/; ta      
  42.  
  43. #remove < > bracketing
  44. s/<\([0-9]\+\)>/\1/g
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement