Grepping text block by block by using for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping text block by block by using for loop
# 8  
Old 09-24-2012
Quote:
Originally Posted by RudiC
It will not be too performant on large files, as awk always scans through the entire file!
Code:
for ((i=1;i<=5;i++))
do
echo Block: $i
awk     '/^Hi welcome/ {++fn}
     {if (fn==blockno) print}
    ' blockno=$i test
done

Hi Rudic,

Using for loop it will take too much of time and every time awk reads file from start. instead of using for loop we can use awk directlly.

try this..
Code:
awk '{if($0 ~ /^Hi welcome/){a++; s=$0}else{if($0 !~ /Bye\*\*/){if(s != "") { s=s"\n"$0}}else{s=s"\n"$0; print "Block : "a"\n"s"\n"}}}' file

another approach..

Code:
awk '{if($0 ~ /Bye\*\*/){a++;s=s"\n"$0;print "Block : "a"\n"s"\n";s=""}else{if(s){s=s"\n"$0}else{s=$0}}}' file

# 9  
Old 09-24-2012
If you don't have any escape sequence in "blah blah blah", try this (made in bash):
Code:
par=""
while IFS= read line; do
par="${par}${line}\n"
if [ "$line" = "Bye**" ]; then
 par=${par%\\n}
 echo -e "${par}" >tmpfile
 cat tmpfile # or do whatever you like with it
 par=""
fi
done <inputfile

--
Bye
# 10  
Old 09-24-2012
Dear RudiC,

Sorry, it may sound foolish but may I know where to enter input file name and what will be output file name? Also, wondering where is "Bye**" pattern is placed in the script.
# 11  
Old 09-24-2012
Sorry, the input filename I used is "test" - pls replace. The ouput is sent to stdout, you can use the redirection ">" to send it to e.g. "tempfile" or whatever filename you like. We dont need the "Bye**" pattern if the input file is structured like you posted: "Bye**" immediately followed by the next "Hi welcome".
# 12  
Old 09-24-2012
Wow Len RudiC and Pamu,

Thanks for your efforts and prompt help which was badly needed.
Special thanks to Len, it worked exactly how I wanted it to work.

Thank you once again.
God bless you all
Take care
Anu.
# 13  
Old 09-24-2012
Quote:
Originally Posted by pamu
Hi Rudic,

Using for loop it will take too much of time and every time awk reads file from start.

@pamu: I recognize this and commented on it, but the requestor asked to supply the blocks into a for loop:
Quote:
Here I am trying to get it block by block (From Hi welcome to Bye** is one block) in a temp file by using for loop.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: Performing "for" loop within text block with two files

I am hoping to pull multiple strings from one file and use them to search within a block of text within another file. File 1PS001,001 HLK PS002,004 MWQ PS004,002 RXM PS004,006 DBX PS004,006 SBR PS005,007 ML PS005,009 DBR PS005,011 MR PS005,012 SBR PS006,003 RXM PS006,003 >SJ PS006,010... (11 Replies)
Discussion started by: jvoot
11 Replies

2. UNIX for Dummies Questions & Answers

Add a block of code at the end of a specific block

I need to search for a block with the starting pattern say "tabId": "table_1", and ending pattern say "]" and then add a few lines before "]" "block1":"block_111" "tabId": "table_1", "title":"My title" ..... .... }] how do I achieve it using awk and sed. Thanks, Lakshmi (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

3. Shell Programming and Scripting

Extract a block of text

Hello all, I am working on a script which should parse a large file called input.txt which contains table definitions, index definitions and comments like these ones: ------------------------------------------------ -- DDL Statements for table "CMWSYS"."CMWD_TEC_SUIVI_TRT"... (12 Replies)
Discussion started by: kiki_riki_miki
12 Replies

4. 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

5. 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

6. UNIX for Dummies Questions & Answers

Deleting Block of Text from a File

Hi I am looking for the way to delete the block of data for example original file line1 line2 line3 line4 line5 input file line2 line3 original file should contain line1 line4 line5 (3 Replies)
Discussion started by: rakeshkumar
3 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

Extract a block of text??

Hello all, I have a large output file from which I would like to extract a single block of text. An example block of text is shown below: ***** EQUILIBRIUM GEOMETRY LOCATED ***** COORDINATES OF ALL ATOMS ARE (ANGS) ATOM CHARGE X Y Z ... (10 Replies)
Discussion started by: marcozd
10 Replies

9. Shell Programming and Scripting

How to insert text after a block of text?

Input: fstab is a configuration file that contains information of all the partitions and storage devices in your computer. The file is located under /etc, so the full path to this file is /etc/fstab. The >>>>> characters would be replaced by some texts. For example if i run a... (5 Replies)
Discussion started by: cola
5 Replies

10. Shell Programming and Scripting

Deleting text block in file

Need to delete a text block inside a file, that is marked with a start and an end pattern. Eg do not delete not delete <tag1> delete everything here here and here and here... <tag2> do not delete do not delete.... Believe sed is able to do this job but don't get it working. ... (1 Reply)
Discussion started by: andre123
1 Replies
Login or Register to Ask a Question