Awk - find string, search lines below string for other strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk - find string, search lines below string for other strings
# 1  
Old 03-29-2011
Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info.

Basically my file looks like this:
Code:
...
<VirtualHost *:>
        ServerName server.name1.com
        ServerAlias svrname
        Redirect Permanent / http://www.sitename1.gov
# Disable HTTP TRACE Support (see http://www.kb.cert.org/vuls/id/867593)
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
        RewriteRule .* - [F]
</VirtualHost>

<VirtualHost *:>
        ServerName www.servername2
        ServerAlias srvname2
        ServerAlias srvname.2.com
        DocumentRoot /location/of/document/root
        DirectoryIndex index.shtml
        ScriptAlias /cgi-bin/ /tst/cgi-bin/
        ErrorDocument 403 http://www.error.com/errors/error403.html
#       ErrorDocument 404 http://www.error.com/errors/error404.html
# Disable HTTP TRACE Support (see http://www.kb.cert.org/vuls/id/867593)
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
        RewriteRule .* - [F]
</VirtualHost>
...

And I want it to look like this:
Code:
...
<VirtualHost *:>
                 ServerName server.name1.com
<VirtualHost *:>
                 ServerName www.servername2
                 DocumentRoot /location/of/document/root
...

There a simple or not so simple awk script for this? Thanks.
# 2  
Old 03-29-2011
If you're on Solaris, use gawk, nawk or /usr/xpg4/bin/awk.

Code:
awk 'END {
    if (ok) print r
  }
/VirtualHost/ {
  if (ok) print r
  r = $0; ok = x  
  }
/ServerName|DocumentRoot/ { 
  r = r RS $0; ok++  
  }' infile

This User Gave Thanks to radoulov For This Post:
# 3  
Old 03-29-2011
Code:
grep -E 'VirtualHost|ServerName|DocumentRoot' *

# 4  
Old 03-29-2011
Code:
awk '/<VirtualHost \*:>|ServerName|DocumentRoot/{print}' file
<VirtualHost *:>
        ServerName server.name1.com
<VirtualHost *:>
        ServerName www.servername2
        DocumentRoot /location/of/document/root

# 5  
Old 03-29-2011
Quote:
Originally Posted by radoulov
If you're on Solaris, use gawk, nawk or /usr/xpg4/bin/awk.

Code:
awk 'END {
    if (ok) print r
  }
/VirtualHost/ {
  if (ok) print r
  r = $0; ok = x  
  }
/ServerName|DocumentRoot/ { 
  r = r RS $0; ok++  
  }' infile

Works like a charm. Thanks a bunch!
# 6  
Old 03-29-2011
Quote:
Originally Posted by ctsgnb
Code:
grep -E 'VirtualHost|ServerName|DocumentRoot' *

The above command will output the lines containing the string VirtualHost regardless of the presence of ServerName and DocumentRoot directives.
# 7  
Old 03-29-2011
Ooops you true.... i didn't see he wanted to discard those not having these ServerName and DocumentRoot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

2. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

3. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

4. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

5. Shell Programming and Scripting

awk search an output string between two strings

I would like to search for strings stored in searchstringfile.txt in inputfiles. searchstringfile.txt J./F. Gls. Wal F. Towerinput1.txt What is needed is J./F. 12 var Gls. Wal 16 interp. Tower 12 input2.txt Awk shall search for F. 16 pt. J./F. 22 output.txt input1.txt J./F. = 12 var... (3 Replies)
Discussion started by: sdf
3 Replies

6. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

7. Solaris

Search string between the strings

File name : Sample.txt <ownername>Oracle< ownername> I am new to unix world , i would like to search string and return back to another sh script. bascially i want to read file Sample.txt find the string between <ownername> Sample.txt < ownername> . Gerneric way to find the string... (15 Replies)
Discussion started by: balajikalai
15 Replies

8. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

9. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

10. Shell Programming and Scripting

Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this? ---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ---------- my working file looks like this: <empty> <empty> <empty> NAME :ABC AGE :15 GENDER... (6 Replies)
Discussion started by: kingpeejay
6 Replies
Login or Register to Ask a Question