Solved

Extract specific entires from a file based on unique tag identifier

Posted on 2004-04-14
3
216 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

730 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