awk: How to search for a block within a block ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: How to search for a block within a block ?
# 1  
Old 12-26-2010
awk: How to search for a block within a block ?

Ok, I have some experience with awk, but I haven't touched it for years ... back again and I have one problem.

I have large char file, it has hundreds of major text blocks. First, I need to grab and process those, and ignore everything else. That bit is simple.

Within those major text blocks, there are 3 to 32 minor text blocks. Second, I need to process those. Here's what the data looks like this:
Code:
major_start
major data

minor_start
minor data 1
minor_end

minor_start
minor data 2
minor_end

...
minor_start
minor data 99
minor_end

major_end

This is what I would like to code (of course it doesn't work). Note, I need bits from the major_block while processing the minor_block, such as var1:
Code:
/major_start/,/major_end/ {

    if ( $1 == "fat" ) { var1=$7 }

    /minor_start/,/minor_end/ {

        / 0:/ { array1[0]=var1; array2[0]=$3; var3=$3 }
        if ( $1 == "thin" ) { var2=$9 }

        }

    }

I was certain I have done this before (without leaving awk), but I cannot find evidence of that. I looked at my old awk scripts, they seem to process the major_block; then use if-then-else. But the need was not quite the same.

I searched this forum, and the closest question is "parsing-file-extracting-useful-data-block". (close but not quite, because that particular bit did not get answered). I an seeking the following:

1. Instructions to get awk to do what I want.

2. Alternately, can I capture everything in major_block and write that to a file_B; then process it with "system(awk ... file_B)"

3. Alternately, can someone who is an awk geek confirm to me that awk is a single-pass design, with no iteration.

TIA

Last edited by DerekAsirvadem; 12-28-2010 at 02:26 AM..
# 2  
Old 12-26-2010
Hi, DerekAsirvadem.

Welcome to the forum.

Thanks for posting your code snippet.

Note that https://www.unix.com/shell-programmin...ata-block.html contains a sample of the input and the expected output.

There have been many reads of your post, but no replies, suggesting that such samples will probably help attract pertinent and useful replies ... cheers, drl

PS If you decide to post code and data, please use CODE tags, just as you did for the snippet. A simple method: select text with your mouse, then click the icon just above the editing window.. The correct one looks like a hand on a keyboard, resulting in something like the block below:
Code:
code or data displayed in a CODE block for easy reading

# 3  
Old 12-28-2010
DRL

Thanks. Done.
# 4  
Old 12-28-2010
Hi,

You cannot use a range pattern inside another range pattern, but you could do something like this:
Code:
/major_start/                             { major_block=1 }
major_block                               { execute code that is inside the major block }
major_block && /minor_start/,/minor_end/  { execute code that is inside a minor block inside the major block }
/major_end/                               { major_block=0 }'

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-06-2011
Scrutinzer

Sorry for the delayed response.

I didn't get an answer in time (had to rebuild a server over the holidays; needed the scripts working), so I implemented 32 IF statements. Works fine, but u.g.l.y.

Your solution is elegant. My need is a bit more complicated (I posted a simple example) but that's a minor problem on my side. I will use it, and re-implement my ugly code before the next execution. I have a similar need coming up in a few weeks, I can use it again.

Very RPG, incidentally, for the older scripters among us.

Thanks.
# 6  
Old 01-11-2011
Scrutinizer

Changed my code; it works well. The code is substantially more readable, and the program is shorter.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a String between start and end of a block in a file

Hi, I have a scenario where I want to display the output based on the pattern search between the start and end of a block in a file, we can have multiple start and end blocks in a file. Example give below, we need to search between the start block abc and end block def in a file, after that... (5 Replies)
Discussion started by: G.K.K
5 Replies

2. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

3. Shell Programming and Scripting

How can I extract XML block around matching search string?

I want to extract XML block surrounding search string Ex: print XML block for string "myapp1-ear" surrounded by "<application> .. </application>" Input XML: <?xml version="1.0" encoding="UTF-8"?> <deployment-request> <requestor> <first-name>kchinnam</first-name> ... (16 Replies)
Discussion started by: kchinnam
16 Replies

4. Shell Programming and Scripting

Search and comment block of text from apache httpd.conf

I want to search for a block of text in httpd.conf that between two strings and comment it. There are multiple blocks with "<Directory.. and </Directory>" <Directory "${ORACLE_INSTANCE}/config/${COMPONENT_TYPE}/${COMPONENT_NAME}/htdocs"> # # Possible values for the Options directive are... (3 Replies)
Discussion started by: kchinnam
3 Replies

5. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

6. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

7. UNIX for Advanced & Expert Users

Deciding whether to get a buffer cache block or inode block

I was reading a book on UNIX internals "The design of the UNIX Operating system." There are two memory structures that are confusing me: 1) Buffer cache 2) Inode cache My questions are 1) Does a process get both buffer cache and Indoe cache allocated when it opens/creates a file? 2) if no,... (1 Reply)
Discussion started by: sreeharshasn
1 Replies

8. Shell Programming and Scripting

Insert Block of Text into a File After a Ranged Search

Hello, I've been racking my brain trying to find a good way to accomplish a task. I need to insert a block of text into a file in the format of FirewallRuleSet proxy-users { FirewallRule allow to 0.0.0.0/0 } I need to insert this block of text (which could have sed special... (2 Replies)
Discussion started by: 0xception
2 Replies

9. Shell Programming and Scripting

Need help in sed: adding a line after each search block

Hi friends, I have written script that will search & display the block of sql statement. Since there are many blocks of sql statement i am finding it difficult to distinguish between them. The below is the sed command & its result exec sql abc abc abc... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. UNIX for Dummies Questions & Answers

Search/replace using visual block

Hi, how would we replace a few patterns on the same line with a change of it's own..using visual block on vim editor. ie a file contains lines such as the following: abccss (dfrss)) emailkk abdcss (dfrss)) dmailkk Using visual block once, replacement is indeed to get the following output:... (0 Replies)
Discussion started by: Manan
0 Replies
Login or Register to Ask a Question