sed command to parse Apache config file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to parse Apache config file
# 1  
Old 12-03-2009
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:

Code:
.....
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:
============
Code:
RewriteRule https://www.example1.com [P,NC,L]
ProxyPassReverse http://foo.example.com


Tried the following:
Code:
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.

Last edited by pludi; 12-03-2009 at 03:28 AM.. Reason: code tags, please...
# 2  
Old 12-03-2009
Try the following sed command :
Code:
sed -n -e '/^[[:space:]]*ProxyPassReverse.*https\{0,1\}:/p' \
       -e '/^[[:space:]]*RewriteRule.*https\{0,1\}:.*\[P/p'   inputfile

Jean-Pierre.
# 3  
Old 12-03-2009
$ 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]
# 4  
Old 12-03-2009
Jean-Pierre: thanks. The code didn't yield any result.
rdcwayx: code complain of syntax error.
Code:
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

# 5  
Old 12-03-2009
replace by nawk or GNU awk

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

# 6  
Old 12-03-2009
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:

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:
Code:
RewriteRule http://dev-individual.com [P,NC,L]
RewriteRule http://dev-obawls.com [P,NC,L]

Thank you.
# 7  
Old 12-04-2009
With your new output by below commands:

Code:
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.Smilie

Or by nawk:

Code:
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]


Last edited by rdcwayx; 12-04-2009 at 12:16 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parse apache log file with three different time formats

Hi, I want to parse below file and Write a function to extract the logs between two given timestamp. Apache (Unix) Log Samples - MonitorWare The challenge here is there are three date and time format. First :- 07/Mar/2004:16:05:49 Second :- Sun Mar 7 16:02:00 2004 Third :- 29-Mar... (6 Replies)
Discussion started by: sahil_shine
6 Replies

2. Shell Programming and Scripting

sed add line to config file

what is the sed command line to add a line to a config file config file name is "config" line to be added cpuid.7.edx = "----:00--:----:----:----:----:----:----" thanks (4 Replies)
Discussion started by: tdubb123
4 Replies

3. Red Hat

Apache virtual host config vs global config problem

Hi folks, I am trying to configure Apache webserver and also a virtual host inside this webserver. For Global server config: /var/www/html/index.html For virtual host config: /var/www/virtual/index.html Both client10 & www10 are pointing to 192.168.122.10 IP address. BUT, MY... (1 Reply)
Discussion started by: freebird8z
1 Replies

4. Shell Programming and Scripting

Parse config file data to script variable

I have a script with few pre defined variables. Also have a config file. Something like this. # config file # Define Oracle User MOD1_TAG=abcde MOD2_TAG=xyzabc MOD3_TAG=def I need to parse the config file and have each of the value copied to different variables. Please suggest what... (1 Reply)
Discussion started by: souryadipta
1 Replies

5. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

6. Shell Programming and Scripting

awk/sed Command: To Parse Stament between 2 numbers

Hi, I need an awk command that would parse the below expression Input Format 1 'Stmt1 ............................'2 'Stmt2 ............................'3 'Stmt3 ............................'4 'Stmt4 ............................'5 'Stmt5 ............................'6 'Stmt6... (1 Reply)
Discussion started by: rajan_san
1 Replies

7. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

8. Shell Programming and Scripting

How to parse slash / in sed command..

Hi All, I am trying to replace paths with \ (as in NT) with / (as in Unix) in a script using sed. Following is the command I use: sed "s/e:\Kenny_test\csv_files/var/opt/ciw/data/outbound/g" EMEA4710.SQL > test the string to replace is e:\Kenny_test\csv_files the target string needs to... (3 Replies)
Discussion started by: Abhidey
3 Replies

9. Shell Programming and Scripting

How to parse config variables from external file to shell script

How do i use a config.txt to recursively pass a set of variables to a shell script eg my config.txt looks like this : path=c://dataset/set1 v1= a.bin v2= b.bin path=c://dataset/set2 v1= xy.bin v2= abc.bin .................. and so on . and my testscript : (2 Replies)
Discussion started by: pradsh
2 Replies

10. UNIX for Dummies Questions & Answers

Apache Config

I need help doing two things in my Apache Config.... 1) Creating a Referer log where only domains other than my own (or a list I specify) are logged ...and... 2) I want to replace images loaded from my site, on other sites, it a standard RED "stolen" image. I currently have code in place... (2 Replies)
Discussion started by: l008com
2 Replies
Login or Register to Ask a Question