Sponsored Content
Top Forums Shell Programming and Scripting Grep lines before a pattern having some other pattern Post 302934470 by drl on Saturday 7th of February 2015 01:31:50 PM
Old 02-07-2015
Hi.

I could not tell if only the 2 lines matching 2 patterns were desired, or if the entire section between those two lines was wanted. So here is a solution for both, using easily accessible grep-like utilities:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate search for previous secondary pattern, peg, cgrep.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C peg cgrep

FILE=${1-data1}

pl " Input data file $FILE, numbered:"
cat -n $FILE

p=$( basename $0 ) t1="$Revision: 1.11 $" v=${t1//[!0-9.]/}
[[ $# > 0 ]] && [[ "$1" =~ -version ]] &&  { echo "$p (local) $v" ; exit 0 ; }

pl " Results, secondary pattern and main pattern ONLY:"
peg -z '/header/' 'time' $FILE

pl " Results, cgrep, ENTIRE section, secondary pattern THROUGH main pattern:"
cgrep -w 'header' 'time' $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 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
peg (local) 3.10
cgrep ATT cgrep 8.15

-----
 Input data file data1, numbered:
     1	header 1
     2	a
     3	b
     4	time
     5	  c
     6		header 2
     7	d
     8	time
     9	e
    10	f
    11	 header 3
    12	time

-----
 Results, secondary pattern and main pattern ONLY:
**** (1) header 1
time
**** (6) 	header 2
time
**** (11)  header 3
time

-----
 Results, cgrep, ENTIRE section, secondary pattern THROUGH main pattern:
header 1
a
b
time
========================================
	header 2
d
time
========================================
 header 3
time

peg: http://www.cpan.org/authors/id/A/AD/ADAVIES/peg-3.14

cgrep: cgrep | SourceForge.net

Both codes have many more features than noted here. Regarding peg, because it is in perl, it can be taken to almost any platform that has perl. The cgrep code is c, which means that it must be compiled. I had no trouble compiling it on 32-bit and 64-bit systems with gcc.

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Solaris

How to grep (say)last-3 and next-3 lines of Desired Pattern

Hi All, OS-Type=Sun-OS 5.8 Sparc9 Processor Can I grep the previous 4 lines and next 4 lines of a matched pattern(context grep)? For example here we need to monitor logs of live traffic.The data obtained from "tail -f LiveTrafficData.log" looks something like this:-... (3 Replies)
Discussion started by: Sujan Banerjee
3 Replies

2. UNIX for Dummies Questions & Answers

Grep with 8 lines before and after pattern.

OK. I have a file I'd like to be able to grep, but on top of returning the line where the pattern matches, I'd like to be able to get the previous 8 lines and the following 8 lines. Is there a way to do this? (2 Replies)
Discussion started by: mrwatkin
2 Replies

3. Shell Programming and Scripting

grep: get last 3 lines containing PATTERN from many files

Hi all, I am looking for a quick solution for this: I have many log files of an iterative program, and I would like to display the parameters of the last three iteration from each of those files. Relevant lines have the keyword: ITER I am using: tac ~/modeling*/fitting.log | grep -m 3 -e... (2 Replies)
Discussion started by: pnemeth
2 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

6. Shell Programming and Scripting

Grep pattern and display all lines below

Hi I need to grep for a patter and display all lines below the pattern. For ex: say my file has the below lines file1 file2 file3 file4 file5 I NEED to grep for patter file3 and display all lines below the pattern. do we have an option to get this data. Let me know if you require... (5 Replies)
Discussion started by: venkidhadha
5 Replies

7. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

8. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

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

10. Shell Programming and Scripting

Grep all lines with the pattern .sh

Linux version : Oracle Linux 6.5 Shell : bash In the the below text file (someString.text), I want to grep all lines with .sh in it. ie. Only the lines mysript.sh and anotherscript.sh should be returned. My below attempts failed. I gather that in regular expression world, dot (.) is the... (3 Replies)
Discussion started by: John K
3 Replies
pt_introduction(n)						   Parser Tools 						pt_introduction(n)

__________________________________________________________________________________________________________________________________________________

NAME
pt_introduction - Introduction to Parser Tools SYNOPSIS
package require Tcl 8.5 _________________________________________________________________ DESCRIPTION
Welcome to the Parser Tools, a system for the creation and manipulation of parsers and the grammars driving them. What are your goals which drove you here ? [1] Do you simply wish to create a parser for some language ? In that case have a look at our parser generator application, pt, or, for a slightly deeper access, the package underneath it, pt::pgen. [2] Do you wish to know more about the architecture of the system ? This is described in the section Parser Tools Architecture, below [3] Is your interest in the theoretical background upon which the packages and tools are build ? See the Introduction to Parsing Expression Grammars. PARSER TOOLS ARCHITECTURE
The system can be split into roughly three layers, as seen in the figure below IMAGE: architecture These layers are, from high to low: [1] At the top we have the application and the packages using the packages of the layer below to implement common usecases. One example is the aforementioned pt::pgen which provides a parser generator. The list of packages belonging to this layer can be found in section User Packages [2] In this layer we have the packages which provide the core of the functionality for the whole system. They are, in essence, a set of blocks which can be combined in myriad ways, like Lego (tm). The packages in the previous level are 'just' pre-fabricated combina- tions to cover the most important use cases. The list of packages belonging to this layer can be found in section Core Packages [3] Last, but not least is the layer containing support packages providing generic functionality which not necessarily belong into the module. The list of packages belonging to this layer can be found in section Support Packages USER PACKAGES pt::pgen CORE PACKAGES This layer is further split into six sections handling the storage, import, export, transformation, and execution of grammars, plus grammar specific support packages. Storage pt::peg::container Export pt::peg::export pt::peg::export::container pt::peg::export::json pt::peg::export::peg pt::peg::to::container pt::peg::to::json pt::peg::to::peg pt::peg::to::param pt::peg::to::tclparam pt::peg::to::cparam Import pt::peg::import pt::peg::import::container pt::peg::import::json pt::peg::import::peg pt::peg::from::container pt::peg::from::json pt::peg::from::peg Transformation Execution pt::peg::interp pt::rde Support pt::tclparam::configuration::snit pt::tclparam::configuration::tcloo pt::cparam::configuration::critcl pt::ast pt::pe pt::peg SUPPORT PACKAGES pt::peg::container::peg text::write configuration paths char BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category pt of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. KEYWORDS
EBNF, LL(k), PEG, TDPL, context-free languages, expression, grammar, matching, parser, parsing expression, parsing expression grammar, push down automaton, recursive descent, state, top-down parsing languages, transducer CATEGORY
Parsing and Grammars COPYRIGHT
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net> pt 1 pt_introduction(n)
All times are GMT -4. The time now is 12:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy