Awk match a multiline pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk match a multiline pattern
# 8  
Old 10-13-2009
Try this:

Code:
sed -n '/^CACHE_SIZE/  N;/\n10000 M/ N;/\n}/p; ' filename

# 9  
Old 10-13-2009
Thanks but i need to use awk dennis, and i think that i can't use getline zaxxon, because i have a config file like this:

Code:
CACHE SIZE{
cache_size=1000M
}

CRONTAB{
crontab=1 12 2 10 5
}

POLITICS{
100MF   doc,docx,xls    <5600K  02/04/02/02/02K
55MF    jpg     >300M   03/03/02/03/05K
65%F    mpg     =000M   02/02/02/05/05M
}

3 differents structures in my config file and besides this, i'm reading this config file with an awk script "read_conf.awk" like this:

Code:
BEGIN {FS = "\t"
print "Starting to read the config file"
num=0;
bucle=0;

}
# first regexp
/^[0-9]*[\%|M|K|G][C|F]\t[A-Za-z|\,]*\t[\<|\>|\=][0-9]*[K|M|G]\t[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][M|K|G]$/     {

instructions...

}

#second regexp
/^cache_size=[0-9]*[K|M|G]/     {

instructions...

}

#third regexp
/crontab=[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*/ {

instructions...

}

Everyone of this regexp read the values between {...}, but i need to read
with the head and the end, in this case: "POLITICS{" and "}"
The structure "head", "regexp", "end"

Code:
POLITICS{
100MF   doc,docx,xls    <5600K  02/04/02/02/02K
55MF    jpg     >300M   03/03/02/03/05K
65%F    mpg     =000M   02/02/02/05/05M
}

I explain me correctly?

Thanks
# 10  
Old 10-14-2009
Try this

infile: -

Code:
123M     xls     <670K
234K     doc    >800K
 
dummy{
10N
}
 
CACHE_SIZE{
1000M
}

Code:
nawk ' /CACHE_SIZE{/{bo = 1}(/}/)&&(bo){print;exit}bo ' infile

Gives output: -

Code:
CACHE_SIZE{
1000M
}

Hope it helps Smilie

---------- Post updated at 10:17 PM ---------- Previous update was at 08:22 PM ----------

Is this what you need?

Code:
TX5XN:/home/brad/forum/claw82>cat infile
CACHE SIZE{
cache_size=1000M
}

CRONTAB{
crontab=1 12 2 10 5
}

POLITICS{
100MF   doc,docx,xls    <5600K  02/04/02/02/02K
55MF    jpg     >300M   03/03/02/03/05K
65%F    mpg     =000M   02/02/02/05/05M
}


TX5XN:/home/brad/forum/claw82>nawk ' /CACHE SIZE{/,/}/;/CRONTAB{/,/}/;/POLITICS{/,/}/ ' infile

CACHE SIZE{
cache_size=1000M
}
CRONTAB{
crontab=1 12 2 10 5
}
POLITICS{
100MF   doc,docx,xls    <5600K  02/04/02/02/02K
55MF    jpg     >300M   03/03/02/03/05K
65%F    mpg     =000M   02/02/02/05/05M
}



---------- Post updated 14-10-09 at 10:04 AM ---------- Previous update was 13-10-09 at 10:17 PM ----------

Looking at your awk script I think you probably need to edit it to something like this: -
Code:
nawk ' BEGIN {FS = "\t"        print "Starting to read the config file"        num=0;        bucle=0;} /POLITICS{/,/}/{        print        if( $0 ~ /^[0-9]*[\%|M|K|G][C|F]\t[A-Za-z|\,]*\t[\|\=][0-9]*[K|M|G]\t[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][M|K|G]$/ ) {                print "Instructions for politics"        }}  /CACHE SIZE{/,/}/{        print        if($0 ~ /^cache_size=[0-9]*[K|M|G]/ ) {                print "Instructions for cache size"        }} /CRONTAB{/,/}/{        print        if( $0 ~ /crontab=[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*/ ) {                print "Instructions for crontab"        }} ' infile

This gives the output: -
Code:
Starting to read the config fileCACHE SIZE{cache_size=1000MInstructions for cache size}CRONTAB{crontab=1 12 2 10 5Instructions for crontab}POLITICS{100MF   doc,docx,xls    300M   03/03/02/03/05K65%F    mpg     =000M   02/02/02/05/05M}

Hope that looks ok I cannot get the web form to enter the code tags for me so am doing them from memory.Please note that your pattern for the POLITICS stuff does not seem to work.Cheers

---------- Post updated at 10:07 AM ---------- Previous update was at 10:04 AM ----------

I can't get the formating to work, will have to wait till I get home tonight before I can post this properly. Smilie
# 11  
Old 10-14-2009
In the third part i think that you understood that i wanna do, but i think it's better for me to work with your seconds answer, because i only need to print or save in variables the values between {...}, but controlling that they are headed with something like CACHE_SIZE{

What means /,/ in your code?
# 12  
Old 10-14-2009
Still not 100% sure what you want as you have not given a complete output but take a look at this and tell me how close we are: -

OUTPUT: -

Code:
Starting to read the config file
CACHE SIZE{
cache_size=1000M
Instructions for cache
}
CRONTAB{
crontab=1 12 2 10 5
Instructions for crontab
}
POLITICS{
100MF   doc,docx,xls    <5600K  02/04/02/02/02K
55MF    jpg     >300M   03/03/02/03/05K
65%F    mpg     =000M   02/02/02/05/05M
}

CODE: -
Code:
TX5XN:/home/brad/forum/claw82>cat claw
nawk '

BEGIN {FS = "\t"
    print "Starting to read the config file"
    num=0
    bucle=0
}


/CACHE SIZE{/,/}/ {
    print
    if(  $0 ~ /^cache_size=[0-9]*[K|M|G]/ ){
        print "Instructions for cache"
    }
}

/CRONTAB{/,/}/ {
    print
    if ( $0 ~ /crontab=[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*/ ) {
        print "Instructions for crontab"
    }
}

/POLITICS{/,/}/ {
    print
    if ( $0 ~ /^[0-9]*[\%|M|K|G][C|F]\t[A-Za-z|\,]*\t[\<|\>|\=][0-9]*[K|M|G]\t[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][M|K|G]$/ ) {
        print "Instructions for politics"
    }
}

' infile

The expression: -

Code:
/CACHE SIZE{/,/}/

Means read from the pattern /CACHE SIZE{/ to the pattern /}/ as I learned yesterday on this forum. Within that range of lines we then have an if statement matching the patterns you supplied, although as I said one of them does not seem to work.

If this is not what you are looking for then copy my output above and change it to show what you are trying to achieve exactly.

Cheers
# 13  
Old 10-15-2009
Ok, my output must be something like this:
Code:
for CACHE_SIZE:
1000M

for CRONTAB:
1 12 2 10 5

for POLITICS:
100MF   doc,docx,xls    <5600K  02/04/02/02/02K
55MF    jpg     >300M   03/03/02/03/05K
65%F    mpg     =000M   02/02/02/05/05M

This 3 outputs will be in 3 differents executions or readings with my awk script.

I think maybe with this "/CACHE SIZE{/ to the pattern /}/" i can read the values between the {...} and apply another regexp for know if the content matches with this regexp, what do you think about?

Thanks

Last edited by zaxxon; 10-15-2009 at 05:28 AM.. Reason: code tags ^^
# 14  
Old 10-15-2009
This gives the output you suggest.

I have marked the places you should insert your code, they are inside the if statements containing the regular expressions you posted.

Code:
nawk '

BEGIN {FS = "\t"
    printf("%s\n\n", "Starting to read the config file")
    num=0
    bucle=0

}


/CACHE SIZE{/,/}/ {
    if ( $0 ~ /CACHE SIZE{/ ){
        printf("%s\n", "for CACHE SIZE:")
        next
    }
    if( $0 ~ /}/)
        next
    split($0, a, "=")
    printf("%s\n", a[2])
    if(  $0 ~ /^cache_size=[0-9]*[K|M|G]/ ){

        ## YOUR CODE HERE
        printf("%s\n\n", "Instructions for cache")
    }
}

/CRONTAB{/,/}/ {
    if ( $0 ~ /CRONTAB{/ ){
        printf("%s\n", "for CRONTAB:")
        next
    }
    if( $0 ~ /}/)
        next
    split($0, a, "=")
    printf("%s\n", a[2])
    if ( $0 ~ /crontab=[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*\t[0-9]*|\*/ ) {

        ## YOUR CODE HERE
        printf("%s\n\n", "Instructions for crontab")
    }
}

/POLITICS{/,/}/ {
    if ( $0 ~ /POLITICS{/ ){
        printf("%s\n", "for POLITICS:")
        next
    }
    if( $0 ~ /}/)
        next
    print
    if ( $0 ~ /^[0-9]*[\%|M|K|G][C|F]\t[A-Za-z|\,]*\t[\<|\>|\=][0-9]*[K|M|G]\t[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][M|K|G]$/ ) {

        ## YOUR CODE HERE
        print "Instructions for politics"
    }
}

' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

2. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

3. Shell Programming and Scripting

awk Pattern Match One File to Another

I want to read from file 1 and pattern match in file two and print field two from the next line. File 1: user1 user2 user3 File 2: name=user1 gud=12345 name=user2 gud=32456 I have this pattern hardcoded but can't work out how to pass file 1 to the pattern match: (6 Replies)
Discussion started by: u20sr
6 Replies

4. Shell Programming and Scripting

Using sed to pattern match within a particular multiline block and take action

Hi all, This is my first post, so please go easy if I broke some rules. Not accustomed to posting in forums... :) I'm looking for help on pattern matching within a multiline block and looking to highlight blocks/block-ids that do NOT contain a particular pattern. For example an input file... (5 Replies)
Discussion started by: tirodad
5 Replies

5. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

6. UNIX for Dummies Questions & Answers

sed multiline pattern match

How can I write a script that takes a cisco config file and outputs every occurrence of two, or more, pattern matches through the whole config file? For example, out of a config file, i want to print out every line with interface, description and ip address through the whole file, and disregard... (3 Replies)
Discussion started by: knownasthatguy
3 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

Multiline pattern search using sed or awk

Hi friends, Could you please help me to resolve the below issue. Input file :- <Node> <username>abc</username> <password>ABC</password> <Node> <Node> <username>xyz</username> <password>XYZ</password> <Node> <Node> <username>mnp</username> ... (3 Replies)
Discussion started by: haiksuresh
3 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

want to pattern match using awk

Hello Friends, My script gives an output like below:- but i only want the red part to be displayed. how to i do that. I am enclosing my shell script after that. id='CCRCWebServerINSTALLDIR' id='AdministrationTools-CINSTALLDIR' id='AdministrationTools-ent-CINSTALLDIR'... (3 Replies)
Discussion started by: asirohi
3 Replies
Login or Register to Ask a Question