Sponsored Content
Top Forums Shell Programming and Scripting print chunk of lines only if there is a pattern match in between them Post 302498084 by drl on Saturday 19th of February 2011 07:25:58 AM
Old 02-19-2011
cgrep for regular-expression-context grep situations

Hi.

The non-standard utility cgrep -- "context grep" -- may be easier to understand. You probably know that standard GNU grep allows previous and subsequent lines around the matched line to be listed. The cgrep allows you to specify the limits of a window to print around the matched line. The limits can themselves be regular expressions. So:
Code:
cgrep pattern filename

works just like GNU grep, but
Code:
cgrep -w beginning-of-window-pattern +w end-of-window-pattern main-pattern filename

will extract blocks of text that contain main-pattern.

Here is a demo script that uses a width-decreased version of your data, and the core of the script uses cgrep as noted above. The other lines are supporting code, showing the data, environment, versions, etc.
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate block extraction with embedded match constraint.
# cgrep home: http://sourceforge.net/projects/cgrep/

# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to test script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
C=$HOME/bin/context && [ -f $C ] && . $C cgrep
set -o nounset
pe

FILE=${1-data1}
# cut width to look better in demo.
cut -c 1-78 data0 > $FILE

# Section 2, display input file.
# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

# Section 3, solution.
pl " Results:"
cgrep -D -w NAME +w NAME LOCKED $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39
cgrep - (local: ~/executable/cgrep May 29 2009 )

 || start [ first:middle:last ]
Edges: 5:0:5 of 28 lines in file "data1"
NAME                                                                          
------------------------------------------------------------------------------
abcdefgheija;lksdf                                                            
*** LOCKED *** 
    PARENT                            3887                                    
   ---
                                           
*** LOCKED *** 
    MIDDLE INITIAL:                                                           
    DATE:                            03-JUN-2010                      
<<<<< EOF>>>>
 || end

-----
 Results:
NAME                                                                          
------------------------------------------------------------------------------
abcdefgheija;lksdf                                                            
*** LOCKED *** 
    PARENT                            3887                                    
    TRAN NO:                                03                                
    PARENT 
    PARENT 
NAME                                                                          
NAME                                                                          
------------------------------------------------------------------------------
cvbcdeegheija;lksdf                                                           
                                           
*** LOCKED *** 
    MIDDLE INITIAL:                                                           
    DATE:                            03-JUN-2010                      
<<<<< EOF>>>>

The code needs to be obtained from cgrep | Download cgrep software for free at SourceForge.net and compiled with a "c" compiler. I have done it on 32-bit and 64-bit systems without trouble. I have found cgrep to be very useful in situations like this.

Good luck ... cheers, drl

( Edit 1: clarification, minor typo )

Last edited by drl; 02-20-2011 at 07:36 AM..
These 2 Users Gave Thanks to drl For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies

2. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

3. Shell Programming and Scripting

print lines with exact pattern match

I have in a file domain.com. 1909 IN A 1.22.33.44 domain.com. 1909 IN A 22.33.44.55 ns1.domain.com. 1699 IN A 33.44.55.66 ns2.domain.com. 1806 IN A 77.77.66.66 I need to "grep" or "awk" out the lines starting with domain.com. as follows. domain.com. 1909 IN A 1.22.33.44 domain.com.... (3 Replies)
Discussion started by: anilcliff
3 Replies

4. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

7. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

8. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

9. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

10. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies
All times are GMT -4. The time now is 11:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy