Skip to content

Instantly share code, notes, and snippets.

@danielewood
Last active November 13, 2018 19:16
Show Gist options
  • Save danielewood/a1dc95ddd00ed29b75676526e51cea70 to your computer and use it in GitHub Desktop.
Save danielewood/a1dc95ddd00ed29b75676526e51cea70 to your computer and use it in GitHub Desktop.
#!/bin/bash
inputfile="$1"
while read -r line; do
#set migrate to a non-zero/one value
migrate=2
#regex patterns to use later
migpattern='^MIG\s?-.*'
# Match lines beginning with MIG(case sensitive), optional whitespace, then a hyphen, then anything else to the end of the line
notmigpattern='^NOT\s?-.*'
# Use / as a field separator and break into an array called array
IFS='/' read -r -a array <<< "$line"
# Iterate through each element in the array
for i in "${!array[@]}"
do
if [[ "${array[i]}" =~ $migpattern ]]; then
migrate=1
elif [[ "${array[i]}" =~ $notmigpattern ]]; then
migrate=0
#if we dont want to have "NOT" override all subsequent "MIG"s, comment out break
break
fi
done
if [ $migrate -eq 0 ]; then
echo $line >> "/tmp/notmigrate$1"
printf "nomigrate\t$line\r\n"
elif [ $migrate -eq 1 ]; then
echo $line >> "/tmp/migrate$1"
printf "migrate\t\t$line\r\n"
else
# Lines not matching either regex get dumped unto an untagged file
echo $line >> "/tmp/untagged$1"
printf "untagged\t$line\r\n"
fi
done < "$inputfile"
echo "output files:"
echo "/tmp/migrate$1"
echo "/tmp/notmigrate$1"
echo "/tmp/untagged$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment