Solved

Extract specific entires from a file based on unique tag identifier

Posted on 2004-04-14
3
218 Views
Last Modified: 2012-05-04
I have several files which I need to extract information from.  Each of these files has 'records' in them which have multiple lines per record.  Each record is seperated by a blank line.  Each line in the file consists of a 'tag' field and a 'value' field, seperated by a "=" character.

Some of the records have a tag called "super_pid", where other records don't.  However, all records contain a tag called "IP_address".  If the record contains a "super_pid", then the "IP_address" tag will always be 2 lines after the "super_pid" line.

For every record that contains a "super_pid", I need to print out the "super_pid" value with the corresponding record's "IP_address" value, on the same line, seperated by a comma.

I would like to have this done in awk or some sort of loop, as it will be imbedded as a funtion in a seperate ksh script.

Thanks in advance!

-Brad
0
Comment
Question by:bradjensen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Assisted Solution

by:lwadwell
lwadwell earned 125 total points
ID: 10831335
try...

BEGIN {
      FS="\n"
      RS=""
}
{
      supid=""
      ipadd=""
      prtok=0
      for (x=1; x<=NF; x++) {
            split($x, a, "=")
            if (a[1] == "super_pid") {
                  supid=a[2]
                  prtok=1
            }
            if (a[1] == "ip_address") {
                  ipadd=a[2]
            }
      }
      if (prtok == 1) {
            print supid "," ipadd
      }
}

BTW...just because you want to embed it in a ksh script, don't exclude perl as an option...you can just use the 'perl -e' method.
0
 
LVL 7

Accepted Solution

by:
glassd earned 125 total points
ID: 10831750
This might work. I assumes that the ip_address is always exactly two lines after the super_pid:
awk -F"=" '/super_pid/{a=$2;getline;getline;b=$2;print a","b}' file
0
 

Author Comment

by:bradjensen
ID: 10831986
Wonderful.  Both answers seem to work well.  Thanks!!
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question