Skip to content

Instantly share code, notes, and snippets.

@mathew-fleisch
Last active January 13, 2020 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathew-fleisch/66d627fb9192aa09d9d7d1bf6f6cb716 to your computer and use it in GitHub Desktop.
Save mathew-fleisch/66d627fb9192aa09d9d7d1bf6f6cb716 to your computer and use it in GitHub Desktop.
Convert Text File To TSV
# ENV-DEPLOYMENT_ID-APP_NAME-INSTANCE_NUM.DOMAIN
# Prod-20160502-app-02.company.local
# Qa-20181001-app-02.company.local
# Qa-20181001-app-04.company.local
# Qa-20181002-myapp-01.company.local
#
# # ENV<TAB>APP_NAME<TAB>DEPLOYMENT_ID<TAB>COUNT_OF_INSTANCES
# Prod app 20160502 1
# Qa app 20181001 2
# Qa myapp 20181002 1
#
#
# Save data to text file
echo -e "Prod-20160502-app-02.company.local
Qa-20181001-app-02.company.local
Qa-20181001-app-04.company.local
Qa-20181002-myapp-01.company.local" > data.txt
# Remove existing state, and create a new state file
rm -rf state.txt
touch state.txt
# Loop line by line, through output of last line (done <<< "$(cat data.txt)")
while IFS= read -r line; do
# Extract out the data we don't care about with sed regular expression
thisline=$(echo $line | sed -e 's/\.company.*//g')
# Split thisline by '-' and only show the first element
ENV=$(echo $thisline | awk -F- '{print $1}')
# Split thisline by '-' and only show the second element
DEPLOYMENT_ID=$(echo $thisline | awk -F- '{print $2}')
# Split thisline by '-' and only show the third element
APP_NAME=$(echo $thisline | awk -F- '{print $3}')
# Split thisline by '-' and show the first, second and third element (add back the dash)
KEY=$(echo $thisline | awk -F- '{print $1 "-" $2 "-" $3}')
# Count the number of occurences of the KEY variable
count=$(cat data.txt | grep "$KEY" | wc -l)
# Only display one "app_name" per line
found=$(cat state.txt | grep $KEY)
if [ -z "$found" ]; then
# Desired Output
echo -e "${ENV}\t${DEPLOYMENT_ID}\t${APP_NAME}\t${count}"
fi
# Add appname to state after it has been displayed once
echo $KEY >> state.txt
done <<< "$(cat data.txt)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment