awk - extracting a field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - extracting a field
# 1  
Old 06-13-2012
awk - extracting a field

Hi all,
I have automated a process of doing a sanity check for the DDL created by other DBA's. But i am stuck at a point.

while reading a file in the awk, PI of the table is defined as

Code:
)PRIMARY INDEX test_abc(ACCT_ID)
PARTITION BY RANGE_N(AUTHZN_MTCH_IND  BETWEEN DATE '2011-10-01' AND DATE '2012-12-31' EACH INTERVAL '1' MONTH, NO RANGE);

I need to extract the ACCT_ID field. If every one coded in the same way my life would have been easy. But some code it as :

Code:
)PRIMARY INDEX test_abc
                (
                 ACCT_ID
                )
PARTITION BY RANGE_N(AUTHZN_MTCH_IND  BETWEEN DATE '2011-10-01' AND DATE '2012-12-31' EACH INTERVAL '1' MONTH, NO RANGE);

and others can code it any different ways added extra spaces or new line characters, and database accepts it without any issue. My problem is i have to extract the ACCT_ID column and assign it to a variable in my awk. since i am not a unix expert , i am not sure how to do muti-line pattern search. Appreciate your inputs on how to extract the ACCT_ID alone (This is an example, not all scripts have ACCT_ID Smilie it could be a different column name).
# 2  
Old 06-13-2012
Perhaps the following snippet will be of some assistance:
Code:
/PRIMARY +INDEX/ {
    s = $0
    while (!match(s, /PRIMARY +INDEX[^(]*\([^)]*\)/)) {
        getline t
        s = s t
    }
    $0 = s
    sub(/.*PRIMARY +INDEX[^(]*\([ \t\n]*/, "", s)
    sub(/[ \t\n]*\).*, "", s)
    print "ACCOUNT ID: " s
}

When it finds a line that contains the word PRIMARY followed by space(s) and the word INDEX, it will append lines (if necessary) until a parenthesized expression is found. The contents of that parenthetical, minus leading and trailing whitespace (space/tab/newline), are extracted.

Regards,
Alister

Last edited by alister; 06-13-2012 at 01:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

Extracting field values from .csv

How can I select the bold fields from the following? "CLLI","SWREL","RPTDATE","RPTIME","TZ","RPTTYPE","RPTPD","IVALDATE","IVALSTART","IVALEND","NUMENTIDS" "tklc9010801","EAGLE5 45.0.0-64.70.1","2013-08-07","02:01:50","MST ","COMPONENT MEASUREMENTS ON... (4 Replies)
Discussion started by: leghorn
4 Replies

3. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

4. Shell Programming and Scripting

Extracting duplicates from a desired field

Hello, I have a file of group names and GID's (/etc/group) and I want to find the duplicate group names and put them in a file. So there are 2 fields, i.e.: audit 10 avahi 70 avahi-autoipd 103 bellrpi 605 bin 1 bin 2 bord 512 busobj 161 bwadm 230 cali81 202 card 323 cardiff 901 cbm... (2 Replies)
Discussion started by: mgb
2 Replies

5. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

6. Shell Programming and Scripting

Need help with extracting field

hi there, i'm new to shell scripting as i usually just use our solaris server for db administration and to get some basic system information. however i'm stuck with something that requires further scripting knowledge. I need to extract the Name, Desc and Status from pkginfo-l. I need them... (10 Replies)
Discussion started by: sbavanis
10 Replies

7. Shell Programming and Scripting

Extracting field from array

I have a problem with the following lines of code: SaveIFS=$IFS IFS="/" declare -a Array=($1)#take unix path, split by '/' website=${Array} #stores website name The confusing part is that it works on one machine, but not on the other. If the code is... (2 Replies)
Discussion started by: montana24
2 Replies

8. Shell Programming and Scripting

Extracting a field value from a line

Used TNSNAMES adapter to resolve the alias Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.12.11.255)(PORT = 1540)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = CSMFIRE03))) I would like to extract the CSMFIRE03. I have no clue which command to use. Could... (5 Replies)
Discussion started by: Naresh Akkal
5 Replies

9. Programming

Extracting Field values for XML file

i have an input file of XML type with data like <nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-charging:process> i want to extract data such that i get the output... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies

10. Shell Programming and Scripting

extracting a field from directory path ??????

Hi I'll be getting a directory path as the input to the script. E.g. 1 abc/fsg/sdfhgsa/fasgfsd/adfghad/XXX/fhsad e.g. 2 sadfg/sadgjhgds/sd/dtuc/cghcx/dtyue/dfghsdd/XXX/qytq This input will be stored in a variable. My query is how to extract the field in a variable VAR which occurs... (15 Replies)
Discussion started by: skyineyes
15 Replies
Login or Register to Ask a Question