如何提取并解析shellscript的命令到文件?

如何提取并解析shellscript的命令到文件?

我有以下程序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

for命令每一行的结果为:

=== 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://...:[email protected]:5432/dalmmfh5cs6nqa
HEROKU_POSTGRESQL_PURPLE_URL: postgres://...:[email protected]: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://...:[email protected]:5432/dalmmfh5cs6nqa
HEROKU_POSTGRESQL_PURPLE_URL: postgres://...:[email protected]: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://...:[email protected]:5432/dalmmfh5cs6nqa
HEROKU_POSTGRESQL_PURPLE_URL: postgres://...:[email protected]:5432/d5gngl61fsq95o
JAVA_OPTS:                    -XX:+UseCompressedOops 

如何解析以获取和CLEARDB_DATABASE_URL的所有值并将它们放入由空格分隔的文本文件中?<user><password>

笔记:<user>仅为<password>字母数字字符。<user> = b69ab75f269r50和的一个例子<password> = c6f5230r6

答案1

... | perl -nE '/CLEARDB_DATABASE_URL:.*mysql:..(.*?):(.*?)@/ and say "$1:$2"'

找到您的模式(/.../)并打印相关的捕获元素(and say ...

答案2

这应该有帮助(未经测试)

./<your-shell-script> | grep "^CLEARDB_DATABASE_URL" | awk -F: '{print $3":"$4}' > output-file

相关内容