?
Solved

AWK program question

Posted on 2004-11-11
16
Medium Priority
?
207 Views
Last Modified: 2013-12-23
BEGIN {
        FS = "[\t]+";
        first_attr = 1;
        first_state = 1;
        more_attr = 1;
        more_state = 1;
        hold_date = " ";
        }

/^STATE/ {
        # Remember the last one

        state = $2;
        if (first_state)
                {
                #
                # Make note of the first state date (Submitted)
                #

# Look for the completed state by presettting to the 4th line value = to "Completed"

                first_state = 0;

split($NF,a," ");
who = a[2];
                hold_date = who;
# Use split to dissect the ling into separte array values separated by spaces


                }

        if (more_state)
                {
                more_state = 4;
split($NF,a," ");
who = a[2];
                }

date = hold_date;
        next;
        }

$2 ~ /Priority/ { priority = $NF; next; }


#
# Assume that the first attribute is the title
#

$1 ~ /ATTRIBUTE/ {
        if (first_attr)
                {
                title = $NF;
                first_attr = 0;
#               next;
        }


        if (more_attr)
        {
        more_attr = 4;
        split($NF,a," ");
        impacts = a[2];
        }

                next;
        }


END {
        printf("%-9.9s %-8.8s %-8.8s %-8.8s %-6.6s %-10.10s  %-40.40s\n",
                ARGV[1], state, who, priority, impacts, date, title);
        }' $file >> $OUTFILE

done


###############################################################
This is the data ################################################################
LATEST_VERSION  1.2
LAST_MODIFIED   1003758812      2001/10/22,09:53:32
TIME_CHECKED_IN 1003758812      2001/10/22,09:53:32
CHECKSUM        0
FILE_ID         000000000
IS_A_BRANCH     0
IS_TERMINATED   0
CHECKED_OUT     0
BINARY          0
NUM_BRANCHES    0
NUM_STATES      5
EXPAND_KEYWORDS 0
STATE           Submitted               2001/01/29,13:32:16 twalter
STATE           No-Action               (skipped)
STATE           Active          (skipped)
STATE           Completed               (skipped)
STATE           Closed          2001/10/22,09:53:31 twalter
ATTRIBUTE       Title           Overloading Plant Alarms
ATTRIBUTE       Priority                High
ATTRIBUTE       Impact          Minimal
ATTRIBUTE       Projection              981349200       02/05/2001
ATTRIBUTE       Scope           Code,
ATTRIBUTE       Approved                0
ATTRIBUTE       Responsibility          Engineering

Help I need someone who can interpret some awk statements for me..

How can check for a second record that starts with /ATTRIBUTE/ but also has the pattern /Impact/ in the record. The reason for this is the /Impact/ record can be in any one of the records. So first I need the one with the /Title/ then I need the record with the /Impact/

thanks for all who help!


Don
0
Comment
Question by:dprice7
  • 6
  • 5
  • 5
16 Comments
 
LVL 62

Expert Comment

by:gheist
ID: 12567796
^ATTRIBUTE.*Impact
0
 
LVL 62

Expert Comment

by:gheist
ID: 12569135
For more information:

http://zh.wikipedia.org/wiki/SQL
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 800 total points
ID: 12570714
/ATTRIBUTE\W+Impact/ {
    impacts = $NF;
}

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dprice7
ID: 12572026
ozo,

Please explain what the /ATTRIBUTE\W+Impact/ does

thanks,

Don
0
 

Author Comment

by:dprice7
ID: 12572032
gheist,

Since the page you sent me is in another language it is not very useful to me.

Please explain what the ^ATTRIBUTE.*Impact does..

thanks,

Don
0
 
LVL 85

Expert Comment

by:ozo
ID: 12572041
It matches lines containing "ATTRIBUTE" and "Impact" separated by one or more characters that  are  not  word-constituent.
The following block is executed on lines which match the pattern.
0
 
LVL 85

Expert Comment

by:ozo
ID: 12572051
/^ATTRIBUTE.*Impact/
would be a pattern that matches "ATTRIBUTE" at the beginning of the line, followed by 0 or more of any character, followed by "Impact"
0
 

Author Comment

by:dprice7
ID: 12572067
ozo,

So does it require that one loop sits inside the other to get both values in separate records or can they stay separate?

$1 ~ /ATTRIBUTE/ {
        if (first_attr)
                {
                title = $NF;
                first_attr = 0;
#               next;
        }

/ATTRIBUTE\W+Impact/ {
    impacts = $NF;
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 12572107
/ATTRIBUTE\W+Impact/ {
    impacts = $NF;
}
would be a separate pattern-action statement
If you want something that goes inside the $1 ~ /ATTRIBUTE/ { you could do something like
$1 ~ /ATTRIBUTE/ {
        if (first_attr)
                {
                title = $NF;
                first_attr = 0;
#               next;
        }
        if( $2 ~ /Impact/ ){
              impacts = $3;
       }
}
0
 
LVL 62

Accepted Solution

by:
gheist earned 200 total points
ID: 12572862
what you look for is called regular expressions :

http://www.regular-expressions.info/tutorial.html
0
 

Author Comment

by:dprice7
ID: 12574480
ozo,

It gives me parse errors when I do this...

#
# Assume that the first attribute is the title
#

$1 ~ /ATTRIBUTE/ {
        if (first_attr)
                {
                title = $NF;
                first_attr = 0;
                next;
        }
        }
/ATTRIBUTE\W+Impact/{
impacts= $NF;
}
               
                next;



END {  
0
 
LVL 62

Expert Comment

by:gheist
ID: 12574824
brackets do not match ....
0
 

Author Comment

by:dprice7
ID: 12575395
gheist,

If you are talking about the bracket for END it has one.. it just didn't copy..

END {
        printf("%-9.9s %-8.8s %-8.8s %-8.8s %-6.6s %-10.10s  %-40.40s\n",
                ARGV[1], state, who, priority, impacts, date, title);
        }' $file >> $OUTFILE

0
 
LVL 85

Expert Comment

by:ozo
ID: 12576038
my awk complains about the "next" outside of any {}
0
 

Author Comment

by:dprice7
ID: 12576521
ozo,

thanks for hanging in there till the end with me.

gheist, appreciate your input as well

thanks again,

Don
0
 
LVL 62

Expert Comment

by:gheist
ID: 12577942
why you have program so complex ?


awk -F" " '/Title/ {printf $3} /LastData/ { print $3 $4 }'

or so ...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
In this article, the configuration steps in Zabbix to monitor devices via SNMP will be discussed with some real examples on Cisco Router/Switch, Catalyst Switch, NAS Synology device.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

807 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