Gawk - to extract values from multi lined file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gawk - to extract values from multi lined file
# 1  
Old 11-11-2012
Question Gawk - to extract values from multi lined file

Hi,
I am new to awk and trying to extract some specific fields from the a large file. Can you please help me to write gawk code displaying the out put in the below format:

Desired Output:
Code:
name      fallback_ip   member    member 
www-trymps.extlb.plstry.com-pool-1      180.254.112.50  180.254.112.50:80 187.222.112.50:80
www-trymps.extlb.plstry.com-pool-2 180.254.112.52  180.254.112.52:80 187.222.112.52:80
www-trymps.extlb.plstry.com-pool-3 180.254.112.53  180.254.112.53:80 187.222.112.53:80

Sample file:
Code:
pool {
   name           "www-trymps.extlb.plstry.com-pool-1"
   ttl            30
   preferred      ga
   alternate      ga
   fallback       explicit_ip
   fallback_ip    180.254.112.50
   partition "Common"
   member         180.254.112.50:80
   member         187.222.112.50:80
}
pool {
   name           "www-trymps.extlb.plstry.com-pool-2"
   ttl            30
   preferred      ga
   alternate      ga
   fallback       explicit_ip
   fallback_ip    180.254.112.52
   partition "Common"
   member         180.254.112.52:80
   member         187.222.112.52:80
}
pool {
   name           "www-trymps.extlb.plstry.com-pool-3"
   ttl            30
   preferred      ga
   alternate      ga
   fallback       explicit_ip
   fallback_ip    180.254.112.53
   partition "Common"
   member         180.254.112.53:80
   member         187.222.112.53:80
}

Thank you and appreciate your help

REgards
PRatheesh

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.
# 2  
Old 11-11-2012
Code:
$ awk '/name/{name=$NF}/fallback_ip/{ip=$NF;i=0}/member/ && i==1{gsub("\"","",name);print name,ip,m1,$NF}/member/{m1=$NF;i++}' a.txt
www-trymps.extlb.plstry.com-pool-1 180.254.112.50 180.254.112.50:80 187.222.112.50:80
www-trymps.extlb.plstry.com-pool-2 180.254.112.52 180.254.112.52:80 187.222.112.52:80
www-trymps.extlb.plstry.com-pool-3 180.254.112.53 180.254.112.53:80 187.222.112.53:80

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 11-11-2012
Code:
awk 'BEGIN {
  print "name", "fallback_ip", "member", "member" 
  }
/^ *(name|fallback_ip|member)/ {
  data[++i] = $2
  }
/^}/ {
  for (j = 0; ++j <= i;)
    printf "%s", (data[j] (j < i ? OFS : ORS))
  i = 0
  }' infile

If you want to remove the double quotes:

Code:
awk 'BEGIN {
  print "name", "fallback_ip", "member", "member" 
  }
/^ *(name|fallback_ip|member)/ {
  $2 ~ /^"/ && $2 = substr($2, 2, length($2) - 2)
  data[++i] = $2
  }
/^}/ {
  for (j = 0; ++j <= i;)
    printf "%s", (data[j] (j < i ? OFS : ORS))
  i = 0
  }' infile

This User Gave Thanks to radoulov For This Post:
# 4  
Old 11-11-2012
Thank you..
That worked like a magic and thank you very much.
I am reading through the "sed &awk" book by Dale and Arnold to understand the logic :-)
# 5  
Old 11-11-2012
Hi PRatheesh,
I would suggest you to read Effective AWK programming, it covers the new features of GNU awk 4.1.
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick and easy way to comment out multi lined print statements

Is there a quick and easy way to comment out multi lined print statements? something like this? printf("3408 strings_line_tokens %s \n", strings_line_tokens); (6 Replies)
Discussion started by: cokedude
6 Replies

2. Shell Programming and Scripting

Find and extract values from one file and update other

Gents, Kindly can you help me to update a file extracting the data from other file. I have: file1 The key in this file is substr($0,4,21), and I need to update the columns 6 and 7 using the information for file2. S 21133.00 21535.00 1 0 919088.8 1843754.5 ... (2 Replies)
Discussion started by: jiam912
2 Replies

3. Shell Programming and Scripting

Extract values from multi lined url source

Hello, I want extract multi values from multi url source to a csv text. Thank you very much for help. my curl code : curl "http://www.web.com/cities//city.html Source code: div class="clear"></div> <table class="listing-details"> <tr> ... (1 Reply)
Discussion started by: hoo
1 Replies

4. Shell Programming and Scripting

Gawk - to extract values from multi lined file -I

Hi, Request your help in getting help with the below text formatting using awk. I am still learning awk and your help here is appreciated. Thanks in advance. Desireoutput ---------------- Virtual Pool Destination Profile Profile Profile Profile 1. virtual-1 pool-1 212.254.110.174:https... (2 Replies)
Discussion started by: pratheeshp
2 Replies

5. Shell Programming and Scripting

Extract values from an XML File

Hi, I need to capture all the attributes with delete next to it. The source XML file is attached. The output should contain something like this below: Attributes = legacyExchangeDN Action = Delete Username = Hero Joker Loginid = joker09 OU =... (4 Replies)
Discussion started by: prvnrk
4 Replies

6. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

7. Shell Programming and Scripting

Sorting multi-column values from a specific file

Hi, all. I need a shell script which gathers data from a remote XML file and then displays it according to my needs.. I need this for my job due to the fact that I need to keep track price changes of euro, usd, gold, etc. The XML file I am talking about is located at this page: cnnturk dot... (4 Replies)
Discussion started by: canimsin
4 Replies

8. UNIX for Dummies Questions & Answers

Extract Unique Values from file

Hello all, I have a file with following sample data 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26... (5 Replies)
Discussion started by: simonsimon
5 Replies

9. Shell Programming and Scripting

to extract specific values twice in a file

Hi Friends, I have a file with the following values.. xyz.txt,12345.xml abc.txt,04567.xml cde.txt,12134.xml I would like to extract all the 2nd column values twice as shown in the example like 12345,12345.xml 04567,04567.xml 12134,12134.xml Please advice!! In the formus one of... (7 Replies)
Discussion started by: techmoris
7 Replies

10. Shell Programming and Scripting

Extract values from log file

I would like to write a shell script that will parse through a file similar to the sample below. The data in the file is redirected from rsync into a log file. I would like to call a shell script to parse through and pick out the number beside the percent sign inside the parentheses in the last... (5 Replies)
Discussion started by: wdympcf
5 Replies
Login or Register to Ask a Question