Advertisement
lswest

Sed TaskWarrior Formatter

Jan 12th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.49 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. first_expression="s/\([a-zA-Z0-9]\)\(\s\{2,15\}\)/\1\;\2/g" # Find all characters followed by 2-15 spaces, and insert a semi-colon before the spaces.
  4.  
  5. second_expression="s/\([0-9]\{3\}\)\(\s[a-zA-Z0-9]\)/\1\;\2/g" # Find all 3 consecutive digits that are followed by a space and a letter or number, and insert the semi-colon.  Was originally 2 consecutive numbers, but that caused issues with the first ID column.
  6.  
  7. third_expression="s/\([a-zA-Z]\)\(\s[0-9]\{1,2\}\/\)/\1\;\2/g" # Find all letters followed by a 1 or 2 digit number that is followed by a / (i.e. the date), and insert a semi-colon.
  8.  
  9. fourth_expression="s/\(^[0-9]*\stasks\)/\;\;\;\1/g" # Find a line that starts with any number and followed by a space and the word "tasks", and insert 3 semi-colons before it.
  10.  
  11. fifth_expression="s/\(^[A-Z]*\)\(\s*[a-zA-Z]\)/\1\;\2/g" # Check for any number of capital letters at the start of a line, followed by a space and more text, and insert a semi-colon.
  12.  
  13. sed_args="-e $first_expression -e $second_expression -e $third_expression -e $fourth_expression -e $fifth_expression"
  14.  
  15. if [[ "$1" == "-h" || "$1" == "" ]]; then # If the first parameter is empty or -h (and as such $2 cannot exist), print help.
  16.  
  17.     echo "Usage: $0 <input> (output), where output is optional (if empty, it's a test run)" # Description of command & basic help.
  18.  
  19. elif [[ "$1" != "" && "$2" == "" ]]; then #If the first parameter (input file) isn't empty, but the second one (output file) is, perform a dry-run.
  20.  
  21.     echo "$(sed $sed_args "$1" )" # The $() tells bash to execute the command, and then echo the output.  Sed then pulls in each expression from the variable.
  22.  
  23. elif [[ "$1" != "" && "$1" == "$2" ]]; then
  24.  
  25.  
  26.     echo "$(sed $sed_args "$1" )" # Same as the line above.  Prints the dry-run, so that the user knows what's happening.
  27.  
  28.     sed -i $sed_args "$1" # Here sed actually runs the expressions on the file, and redirects the output into a new file.
  29.  
  30.     echo ""; # Spacing for next message
  31.  
  32.     echo "File updated.";
  33.  
  34.  
  35. else #In any other case, use the first parameter and second parameter as file names.
  36.  
  37.     echo "$(sed $sed_args "$1" )" # Same as the line above.  Prints the dry-run, so that the user knows what's happening.
  38.  
  39.     sed $sed_args "$1" > "$2" # Here sed actually runs the expressions on the file, and redirects the output into a new file.
  40.  
  41.     echo ""; # Make sure the next line isn't lost to the user as general output.
  42.  
  43.     echo "Saved to $2"; # Let the user know that it has been saved.
  44.  
  45. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement