Search code examples
bashshellherokuappendstring-concatenation

How to extract and parse commands of shellscript to file?


I have the following program in Shell Script:

#!/bin/sh

max=9
for i in `seq 1 $max`
do
    sudo -u ${USERNAME} heroku config --app mws-usp-app0${i}
done

max=50
for i in `seq 10 $max`
do
    sudo -u ${USERNAME} heroku config --app mws-usp-app${i}
done

The result of each line of the for command is:

=== mws-usp-app01 Config Vars
CLEARDB_DATABASE_URL:         mysql://<user>:<password>@<hostname>/<defaultschema>?reconnect=true
DATABASE_URL:                 postgres://...:...@ec2-23-21-73-32.compute-1.amazonaws.com:5432/dalmmfh5cs6nqa
HEROKU_POSTGRESQL_PURPLE_URL: postgres://...:...@ec2-50-19-219-148.compute-1.amazonaws.com:5432/de4hk500aj9f1q
JAVA_OPTS:                    -XX:+UseCompressedOops
=== mws-usp-app02 Config Vars
CLEARDB_DATABASE_URL:         mysql://<user>:<password>@<hostname>/<defaultschema>?reconnect=true
DATABASE_URL:                 postgres://...:...@ec2-23-21-73-32.compute-1.amazonaws.com:5432/dalmmfh5cs6nqa
HEROKU_POSTGRESQL_PURPLE_URL: postgres://...:...@ec2-50-19-219-148.compute-1.amazonaws.com:5432/de4hk500aj9f1q
JAVA_OPTS:                    -XX:+UseCompressedOops
...
=== mws-usp-app50 Config Vars
CLEARDB_DATABASE_URL:         mysql://<user>:<password>@<hostname>/<defaultschema>?reconnect=true
DATABASE_URL:                 postgres://...:...@ec2-23-21-73-32.compute-1.amazonaws.com:5432/dalmmfh5cs6nqa
HEROKU_POSTGRESQL_PURPLE_URL: postgres://...:...@ec2-54-243-204-86.compute-1.amazonaws.com:5432/d5gngl61fsq95o
JAVA_OPTS:                    -XX:+UseCompressedOops 

How to parse CLEARDB_DATABASE_URL to get all values of <user>, <password>, <hostname> and <defaultschema> and put them inside a text file?

======================================

The correct script after the response:

#!/bin/sh

max=9
for i in `seq 1 $max`
do
    echo "mws-usp-app0${i}";
    sudo -u ${USERNAME} heroku config --app mws-usp-app0${i} | sed -n 's/CLEARDB_DATABASE_URL.*mysql:\/\/\(.*\):\(.*\)@.*/\1 \2/p' >> usernames.txt
done

max=50
for i in `seq 10 $max`
do
    echo "mws-usp-app${i}";
    sudo -u ${USERNAME} heroku config --app mws-usp-app${i} | sed -n 's/CLEARDB_DATABASE_URL.*mysql:\/\/\(.*\):\(.*\)@.*/\1 \2/p' >> usernames.txt
done

$> chmod +x script.sh
$> sudo ./script.sh


Solution

  • Assuming that your output is in a file called input.txt you can use sed:

      sed -n 's/CLEARDB_DATABASE_URL.*mysql:\/\/\(.*:.*\)@.*/\1/p' input.txt > usernames.txt
    

    alternatively use the output of your shell script immediately:

      <your_script> | sed -n 's/CLEARDB_DATABASE_URL.*mysql:\/\/\(.*:.*\)@.*/\1/p' > usernames.txt
    

    There might be an easier way, but that's what came fastest into my mind ;)