diff options
author | InigoGutierrez <inigogf.95@gmail.com> | 2019-11-19 20:53:24 +0100 |
---|---|---|
committer | InigoGutierrez <inigogf.95@gmail.com> | 2019-11-19 20:53:24 +0100 |
commit | b399d85263a6939b926365bdaff126f1622aa389 (patch) | |
tree | 30ce014c79a3beef6adf09b2743b46465c5f9b64 /sed | |
parent | 8f247ce3b9cde5021e7d7d3ace8b0a486c6d1390 (diff) | |
download | scripts-b399d85263a6939b926365bdaff126f1622aa389.tar.gz scripts-b399d85263a6939b926365bdaff126f1622aa389.zip |
Created script to swap date order in text files.
Diffstat (limited to 'sed')
-rwxr-xr-x | sed/dateReverser.sh | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/sed/dateReverser.sh b/sed/dateReverser.sh new file mode 100755 index 0000000..5936d4b --- /dev/null +++ b/sed/dateReverser.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# Usage: dateReverser.sh [-s SEPARATOR] [-i[BACKUP_SUFFIX]] [FILE]... +# +# Reverses the order of dates found in files (dates have the format n-n-n) +# and prints the result to standard output. +# +# With -i makes changes directly in the files instead and creates a backup if +# BACKUP_SUFFIX is specified. +# Reads from stdin if no arguments specified. + +helpMessage="usage: dateReverser.sh [-s SEPARATOR] [-i[BACKUP_SUFFIX]] [FILE]... + + -s SEPARATOR + Uses SEPARATOR instead of \"-\" as date field separator + + -i[BACKUP_SUFFIX] + Actually edit files instead of print to standard output + Make backup files if BACKUP_SUFFIX is specified" +sep="-" +inFile="" + +while echo "$1" | grep -q '^-'; do + case "$1" in + "-s" ) + sep="$2" + if echo "$sep" | grep -q '/'; then + sep="$(echo "$2" | sed 's|/|\\/|g')" + fi + shift 2 ;; + "-i"* ) + inFile="$1" + shift ;; + * ) + echo "$helpMessage" + exit 1;; + esac +done + +for file in "$@"; do + if [ -n "$file" ] && [ ! -f "$file" ]; then + echo "Error: file $file not found" + exit 1 + fi +done + +sedscr='s/\([0-9][0-9]*\)'"$sep"'\([0-9][0-9]*\)'"$sep"'\([0-9][0-9]*\)/\3'"$sep"'\2'"$sep"'\1/g' + +sed $inFile $sedscr "$@" |