ricardoramos ricardoramos - 20 days ago 11
Bash Question

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>@us-cdbr-iron-east-04.cleardb.net/heroku_cf843140f07f0a2?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>@us-cdbr-iron-east-04.cleardb.net/heroku_cf843140f07f0a2?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>@us-cdbr-iron-east-04.cleardb.net/heroku_b154acff411bcfc?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>
and
<password>
and put them inside a text file?

Answer

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 ;)

Comments