Solved

Extract specific entires from a file based on unique tag identifier

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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.:
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now