Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Extract specific entires from a file based on unique tag identifier

Posted on 2004-04-14
3
Medium Priority
?
221 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 500 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
Suggested Courses

610 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