Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- 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.
- 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.
- 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.
- 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.
- 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.
- sed_args="-e $first_expression -e $second_expression -e $third_expression -e $fourth_expression -e $fifth_expression"
- if [[ "$1" == "-h" || "$1" == "" ]]; then # If the first parameter is empty or -h (and as such $2 cannot exist), print help.
- echo "Usage: $0 <input> (output), where output is optional (if empty, it's a test run)" # Description of command & basic help.
- elif [[ "$1" != "" && "$2" == "" ]]; then #If the first parameter (input file) isn't empty, but the second one (output file) is, perform a dry-run.
- 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.
- elif [[ "$1" != "" && "$1" == "$2" ]]; then
- echo "$(sed $sed_args "$1" )" # Same as the line above. Prints the dry-run, so that the user knows what's happening.
- sed -i $sed_args "$1" # Here sed actually runs the expressions on the file, and redirects the output into a new file.
- echo ""; # Spacing for next message
- echo "File updated.";
- else #In any other case, use the first parameter and second parameter as file names.
- echo "$(sed $sed_args "$1" )" # Same as the line above. Prints the dry-run, so that the user knows what's happening.
- sed $sed_args "$1" > "$2" # Here sed actually runs the expressions on the file, and redirects the output into a new file.
- echo ""; # Make sure the next line isn't lost to the user as general output.
- echo "Saved to $2"; # Let the user know that it has been saved.
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement