sed command to parse Apache config file

Hi there, am trying to parse an Apache 'server' config file. A snippet of the config file is shown below:

.....
ProxyPassReverse /foo http://foo.example.com/bar
.....
.....
RewriteRule ^/(.*)         http://www.example.com/$1 [L,R]
  RewriteRule /redirect      https://www.example1.com/$1 [P,NC,L]
.....
#RewriteRule /somepath      http://www.example2.com/$1 [P,NC,L]
......
#ProxyPassReverse /foo http://foo.example.com/bar
......

My requirements:

Parse http and https URLs that has
a) Active (not commented) RewriteRule directives with forced proxy (P flag) only; and
b) Active ProxyPassReverse directive (not commented)

Desired output:

RewriteRule https://www.example1.com [P,NC,L]
ProxyPassReverse http://foo.example.com

Tried the following:

grep ^RewriteRule file | cut -d" " -f3-
grep ^ProxyPassReverse file | cut -d" " -f3-

The output of the above commands somehow did the trick but fail if the matching lines is indented, tabbed or has spaces in front.
Is there a one-liner sed command to get on the desired output?

Appreciate any help. Thanks.

Try the following sed command :

sed -n -e '/^[[:space:]]*ProxyPassReverse.*https\{0,1\}:/p' \
       -e '/^[[:space:]]*RewriteRule.*https\{0,1\}:.*\[P/p'   inputfile

Jean-Pierre.

$ awk '{if (! ($1 ~/^#/)&&(/RewriteRule/&&($NF~/P/)||/ProxyPassReverse/)) {print $1,$3,$4}}' urfile
ProxyPassReverse http://foo.example.com/bar
RewriteRule https://www.example1.com/$1 [P,NC,L]

Jean-Pierre: thanks. The code didn't yield any result.
rdcwayx: code complain of syntax error.

bash-2.03# awk '{if (! ($1 ~/^#/)&&(/RewriteRule/&&($NF~/P/)||/ProxyPassReverse/)) {print $1,$3,$4}}' servers.bak
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

replace by nawk or GNU awk

nawk '{if (! ($1 ~/^#/)&&(/RewriteRule/&&($NF~/P/)||/ProxyPassReverse/)) {print $1,$3,$4}}' urfile

rdcwayx, wow quick reply...thanks. It does give me the output am looking for.

I was hoping I could sort the URL uniquely but output but seems impossible as the are subdirectories after the URL.

Here's the sample new output from your code:

RewriteRule http://dev-individual.com/SFC/$1 [P,NC,L]
RewriteRule http://dev-individual.com/SFC/intranet/$1 [P,NC,L]
RewriteRule http://dev-obawls.com:5313/advisor_PS_docs$1 [P,NC,L]
RewriteRule http://dev-obawls.com:5813/RTL_ADVISOR_DOCS/$1 [P,NC,L]
RewriteRule http://dev-obawls.com:8313/advisor_ho$1 [P,NC,L]
RewriteRule http://dev-obawls.com:8313/advisor_up$1 [P,NC,L]
RewriteRule http://dev-obawls.com:8313/RTL_ADVISOR/$1 [P,NC,L]

The final desired output is as shown:

RewriteRule http://dev-individual.com [P,NC,L]
RewriteRule http://dev-obawls.com [P,NC,L]

Thank you.

With your new output by below commands:

awk 'split($2,a,":") {$2=a[1]":"a[2]}1' urfile |awk 'split($2,a,"/") {$2=a[1]"//"a[3]}1' |sort -u
RewriteRule http://dev-individual.com [P,NC,L]
RewriteRule http://dev-obawls.com [P,NC,L]

But I can't make it in one command.:frowning:

Or by nawk:

nawk 'split($2,a,":") {$2=a[1]":"a[2]} {print}' urfile |nawk 'split($2,a,"/") {$2=a[1]"//"a[3]} {print}' |sort -u
RewriteRule http://dev-individual.com [P,NC,L]
RewriteRule http://dev-obawls.com [P,NC,L]

BIG thank you, rdcwayx!