Solved

Extract specific entires from a file based on unique tag identifier

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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 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.:
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…

809 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