Sponsored Content
Top Forums Shell Programming and Scripting Grep couple of consecutive lines if each lines contains certain string Post 302648887 by drl on Wednesday 30th of May 2012 01:35:32 PM
Old 05-30-2012
Hi.

This is a quickly-put-together script:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate comparison among cgrep, gawk, mawk.

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() { : ; }
cs() { echo "$1" | perl -wp -e '1 while s/^([-+]?\d+)(\d{3})/$1,$2/; ' ; }
clock() { /usr/bin/time --format="real %e\nuser %U\nsys %S" $*; }
C=$HOME/bin/context && [ -f $C ] && $C cgrep gawk mawk

FILE=${1-/tmp/100-mb.txt}
lines=$( wc -l < $FILE )
chars=$( wc -c < $FILE )
pl " Input file $FILE is $( cs $lines ) lines, $( cs $chars ) characters:"
specimen $FILE

pl " Results for cgrep:"
time cgrep -a '\| REQUEST \|.*\n.*\| RESPONSE \|' $FILE

pl " Results for gawk:"
time gawk -F\| '$2 ~ "REQUEST"{s=$0;next} s && $2~"RESPONSE"{print s RS $0; s=x}' $FILE

pl " Results for mawk:"
time mawk -F\| '$2 ~ "REQUEST"{s=$0;next} s && $2~"RESPONSE"{print s RS $0; s=x}' $FILE

exit 0

producing:
Code:
% ./s2

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.8 (lenny) 
bash GNU bash 3.2.39
cgrep ATT cgrep 8.15
gawk GNU Awk 3.1.5
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

-----
 Input file /tmp/100-mb.txt is 1,777,700 lines, 120,540,400 characters:
Edges: 5:0:5 of 1777700 lines in file "/tmp/100-mb.txt"
Preliminary Matter.  

This text of Melville's Moby-Dick is based on the Hendricks House edition.
It was prepared by Professor Eugene F. Irey at the University of Colorado.
Any subsequent copies of this data must include this notice  
   ---
AND FLOATED BY MY SIDE. +BUOYED UP BY THAT COFFIN, FOR ALMOST ONE WHOLE DAY
AND NIGHT, +I FLOATED ON A SOFT AND DIRGE-LIKE MAIN. +THE UNHARMING SHARKS,
THEY GLIDED BY AS IF WITH PADLOCKS ON THEIR MOUTHS; THE SAVAGE SEA-HAWKS SAILE
D WITH SHEATHED BEAKS. +ON THE SECOND DAY, A SAIL DREW NEAR, NEARER, AND PIC
KED ME UP AT LAST. +IT WAS THE DEVIOUS-CRUISING +RACHEL, THAT IN HER RETRACIN

-----
 Results for cgrep:

real	0m0.224s
user	0m0.104s
sys	0m0.100s

-----
 Results for gawk:

real	0m1.453s
user	0m1.328s
sys	0m0.092s

-----
 Results for mawk:

real	0m1.105s
user	0m0.988s
sys	0m0.096s

If there is something that takes a cache hit, it would be the wc, or at least the cgrep ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep string & next n lines

need help on this. let say i hv 1 file contains as below: STRING Description bla bla bla Description yada yada yada Data bla bla Data yada yada how do i want to display n lines after the string? thanks in advance! (8 Replies)
Discussion started by: ashterix
8 Replies

2. Shell Programming and Scripting

Grep string but also it will show the next 5 lines

Hi experts, I want to grep a number 9366109380 from a file but it will also show me the next 5 lines. Below is the example- when i grep 989366109380, i can also see the next 5 lines. Line 1. <fullOperation>MAKE:NUMBER:9366109380:PPAY2;</fullOperation> Line 2.... (10 Replies)
Discussion started by: thepurple
10 Replies

3. Shell Programming and Scripting

grep string & a few lines after

i need to grep a STRING_A & the next few lines after the STRING_A example file: STRING_A yada yada line 1 line 2 STRING_B yada yada line 1 line 2 line 3 STRING_A yada yada line 1 line 2 line 3 line 4 STRING_A yada yada line 1 line 2 line 3 line 4 (7 Replies)
Discussion started by: ashterix
7 Replies

4. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

5. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

8. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

9. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

10. Shell Programming and Scripting

Remove duplicate consecutive lines with specific string

Hello, I'm trying to remove the duplicate consecutive lines with specific string "WARNING". File.txt abc; WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 bcd; abc; 123 123 123 WARNING 1234 WARNING 2345 WARNING 2345 efgh; (6 Replies)
Discussion started by: Mannu2525
6 Replies
tracker-tag(1)							   User Commands						    tracker-tag(1)

NAME
tracker-tag - Add, remove and list tags. SYNOPSIS
tracker-tag [OPTION...] FILE [FILE...] DESCRIPTION
tracker-tag allows the caller add tags, remove tags and list tags by URN or to list all tags and the files associated with them. The FILE argument can be either a local path or a URI. It also does not have to be an absolute path. OPTIONS
-?, --help Show summary of options. -l, --limit=N Limit search to N results. The default is 512. -o, --offset=N Offset the search results by N. For example, start at item number 10 in the results. The default is 0. -r, --or-operator Use OR for search terms instead of AND (the default) -t, --list List all tags. Results include the number of files associated with that tag and the tag's unique identifier. You can show the files associated with each tag by using --show-files. -s, --show-files Show the files associated with each tag. This option is ONLY available WITH the --list option. -a, --add=TAG Add a tag with the name TAG. If no FILE arguments are specified, the tag is simply created (if it didn't already exist) and no files are associated with it. Multiple FILE arguments can be specified. -d, --delete=TAG Delete a tag with the name TAG. If no FILE arguments are specified, the tag is deleted for ALL files. If FILE arguments are speci- fied, only those files have the TAG deleted. -V, --version Print version. SEE ALSO
tracker-store(1), tracker-sparql(1), tracker-search(1), tracker-info(1). GNU
July 2009 tracker-tag(1)
All times are GMT -4. The time now is 12:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy