Sponsored Content
Top Forums Shell Programming and Scripting [Solved] Extract information according to values ​​defined Post 302891583 by Don Cragun on Thursday 6th of March 2014 02:09:18 PM
Old 03-06-2014
Your script looks like at least two different people have been working on it.

Your specification does not say what should happen if both bad STATUS codes are found for a single ID and INDEX. The following script will print lines for both with the output order matching the order in which each bad error code was first seen for that ID and INDEX:
Code:
awk '
# This awk script will accumulate STATUS 14 and 98 counts for each ID and
# INDEX given on ocntiguous lines in the input file.  If both STATUS==14
# and STATUS==98 records are found, the ouput order will match the order
# of the 1st appearance of each STATUS value found.  (This script could be
# greatly simplified if the output order could always be the STATUS==14
# count first and the STATUS==98 count second.)

# Function to print STATUS==14 and ==98 data for previous ID and INDEX...
# We have a function here rather than inline code because we need to do
# this when the ID or INDEX changes and when we hit end-of-file.
function pr() {
        # Note that if we are processing the 1st line in the file or if the
        # previous ID and INDEX had no bad STATUS lines, cn will be 0 and the
        # code in the loop will not be executed at all.
        for(i = 1; i <= cn; i++) {
                printf("%s index %d has %d time%s status %d\n",
                        ID, INDEX, c[cc[i]], c[cc[i]] > 1 ? "s" : "",
                        cc[i])
                # We delete individual elements of the c[] because the standard
                # do not currently require that the command "delete array_name"
                # can be used to delete all element of an arry.  We need to
                # remove the entry rather than just set c[x] to zero so we will
                # not print lines showing lines with 0 occurrences of the bad
                # STATUS.
                delete c[cc[i]]
        }
        # Clear the number of types of bad STATUS seen for the next ID + INDEX.
        cn = 0
}
{       # Get ID and INDEX for this line...
        nID = substr($0, 12, 4) substr($0, 20, 4)
        nINDEX = substr($0, 26, 1)
        # If the ID and INDEX on this line do not match the previous line...
        if(nID != ID || nINDEX != INDEX) {
                # Print the results for the previous ID + INDEX
                pr()
                # Set the current ID and INDEX to compare against later lines.
                ID = nID
                INDEX = nINDEX
        }
        # Get the STATUS from this line...
        STATUS = substr($0, 91, 2)
        # If this line has one of the bad STATUS values...
        if(STATUS == 14 || STATUS == 98) {
                # If we have not seen this bad STATUS value for this ID +
                # INDEX...
                if(!(STATUS in c)) {
                        # Increment the number of bad STATUS codes seen (cn)
                        # and set cc[cn] to the bad STATUS code seen on this
                        # line.
                        cc[++cn] = STATUS
                }
                # Increment the number of times we have seen this bad STATUS
                # code for this ID + INDEX.
                c[STATUS]++
        }
}
END {   # Print the results for the last ID + INDEX in the input.
        pr()
}' eff8

I hope the comments will enable you to understand what it is doing. If not; ask questions. When using your sample input (with the following lines added at the end to test what happens when both bad STATUS codes codes are given for an ID and INDEX):
Code:
Added G1   1234.0  5678.01                                                                14
Added G1   1234.0  5678.01                                                                98
Added G1   1234.0  5678.01                                                                14
Added G2   1111.0  2222.01                                                                98
Added G2   1111.0  2222.01                                                                14
Added G2   1111.0  2222.01                                                                98

the output produced is:
Code:
31157145 index 3 has 2 times status 14
28037145 index 2 has 2 times status 14
31177140 index 1 has 1 time status 14
31157140 index 2 has 3 times status 98
12345678 index 1 has 2 times status 14
12345678 index 1 has 1 time status 98
11112222 index 1 has 2 times status 98
11112222 index 1 has 1 time status 14

These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk to extract lines with a defined number of characters

This is my problem, my file (file A) contains the following information: Now, I would like to create a file (file B) containing only the lines with 10 or more characters but less than 20 with their corresponding ID: Then, I need to compare the entries and determine their frequency. Thus, I... (7 Replies)
Discussion started by: Xterra
7 Replies

2. HP-UX

[Solved] processor type and bit information

Hi, I'm trying to download a compatible Oracle Client software for a HP-UX machine. I'd like to know if ... 1) HP-UX is 32 bit or 64 bit? 2) Processor type - Itanium or regular? when I execute uname -a I get HP-UX B.11.11 U 9000/800 728684161 unlimited-user license Based on the... (7 Replies)
Discussion started by: luft
7 Replies

3. Shell Programming and Scripting

[solved] awk: placement of user-defined functions

Hi folks, is there any recommendation, especially from a point of performance, about where to place a user-defined function in awk, like in BEGIN{} or if it is only need once at the end in END{}? Or doesn't it matter at all since, awk is so clever and only interprets it once, wherever it is... (3 Replies)
Discussion started by: zaxxon
3 Replies

4. Shell Programming and Scripting

[Solved] Editing the alphabet's based on position information

I do have a file of the following format file 1 >SAM ATGCTCCTTAGCTACGTAGCAAGTAGAAAAAA AGCGCGAGCATTGAAGCGGAGGAGAGGAGGA TGAGATGATGACCCAGTATAAAGAGTGATGAT like this above file. file 1 has 1000's of lines. I would like to edit this file1 using the information from file2 (see below), by... (16 Replies)
Discussion started by: Lucky Ali
16 Replies

5. UNIX for Advanced & Expert Users

[Solved] Unable to fetch the UNIX variable information

I am having a file called variable_info.ksh in that file I am having following global variable information like… EMAIL_PATH=/var/mail TMP_PATH=/home/tmp And we are having another temporary parameter file abcd.txt, in that file we are having the following information like… EMAIL|EMAI_PATH I... (4 Replies)
Discussion started by: mesahammad
4 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Variable defined in .bashrc not intializing in script

I have the variable defined in .bashrc BIN_DIR="/usr/local/dw" and in my shell script i am using below. #!/bin/bash echo "Bin Dir: ${BIN_DIR}" . "${BIN_DIR}"/dwh_Loadfuncs.sh Output: Bin Dir: /usr/local/dw/dwh_LoadXMLFileIntoStage.sh: line 7: /dwh_Loadfuncs.sh: No such file or... (3 Replies)
Discussion started by: Ariean
3 Replies

7. Shell Programming and Scripting

[Solved] How can I pull specific information from PS?

I need to grab information from the output of the ps command. For each line of ps output that contains _progres -b I need to get the word that follows -p. The "-p" can be anywhere after "_progres -b". Using grep to select the correct lines is no problem (e.g. ps -ef|grep "_progres \-b|grep -v... (3 Replies)
Discussion started by: Papa Lee
3 Replies

8. Shell Programming and Scripting

[Solved] Extracting information from DDL's

Dear Experts, I need your help here. I have lot of teradata DDL's as follows, i want to extract field names , field attributes and NOT NULL information from DDL.Could you please help here. Sample DDL: CREATE MULTISET TABLE APS_CALL_IN_PICKUP_CANCELED ,NO FALLBACK , NO BEFORE... (2 Replies)
Discussion started by: srikanth38
2 Replies

9. SCO

How to measure disk IO 5.0.7? (sar, return values ​​are not valid)

Hello I am analyzing disk performance OSR5.0.7 running inside VirtualBox. GUEST: osr5.0.7; 1GB ram; raw disk HOST: SLES11SP3, 4GB ram; 1 disc SATA2-7200rpm But I'm not sure how to do it right (the values ​​returned by sar not match the values ​​of the physical machine) The attributes... (0 Replies)
Discussion started by: flako
0 Replies

10. Shell Programming and Scripting

Sum values of specific column in multiple files, considering ranges defined in another file

I have a file (let say file B) like this: File B: A1 3 5 A1 7 9 A2 2 5 A3 1 3 The first column defines a filename and the other two define a range in that specific file. In the same directory, I have also three more files (File A1, A2 and A3). Here is 10 sample lines... (3 Replies)
Discussion started by: Bastami
3 Replies
All times are GMT -4. The time now is 04:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy