Sponsored Content
Full Discussion: How to Grep of by section?
Top Forums Shell Programming and Scripting How to Grep of by section? Post 302949128 by drl on Tuesday 7th of July 2015 12:02:17 PM
Old 07-07-2015
Hi.

An option in perl, -00, allows processing by paragraphs, provided they are separated by empty lines:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate processing paragraphs of lines, perl.

# 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 perl

PATTERN=${1-Sally}
FILE=data1

pl " Input data file $FILE, looking for \"$PATTERN\":"
cat $FILE

pl " Results:"
perl -00 -wn -e " print if m{$PATTERN}" $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
perl 5.10.0

-----
 Input data file data1, looking for "Sally":
John Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
A Member of 4 Groups

Sally Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
A Member of 3 Groups

Toby Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
memberOf: example5;sampletest;test
A Member of 5 Groups

-----
 Results:
Sally Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
A Member of 3 Groups

This also allows constructs such as:
Code:
$ ./s1 'John|Toby'

producing:
Code:
 Results:
John Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
A Member of 4 Groups

Toby Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
memberOf: example5;sampletest;test
A Member of 5 Groups

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

New section

What are the chances of possibly posting some of the more common how-to type stuff for the newbies so we can avoid the repititious stuff that appears every other day? Not so much like a Q&A forum, but more like a reference area for the mundane stuff. Beyond searching the forum, I think people... (3 Replies)
Discussion started by: ober5861
3 Replies

2. Post Here to Contact Site Administrators and Moderators

New Section

Just like we have a section "Unix for dummies..." , why not have a section on UNIX BACKUP AND RECOVERY Thanks :) (3 Replies)
Discussion started by: kapilv
3 Replies

3. Post Here to Contact Site Administrators and Moderators

New section

Hi Just a thought if it already hasn't been suggested. While looking at the forums I thought it might be a good idea under somewhere like 'special forums' add a section called 'projects'. I think this would be good for people to be able to post projects they have created. For example I am... (3 Replies)
Discussion started by: woofie
3 Replies

4. UNIX for Dummies Questions & Answers

how can i extract text section via grep

hi, i have a very long text file. i need to extract with grep command a certain part. for example text file include 1ooo rows: 1.... 2... 3... . . . 1000 i want to view with grep only rows 50-100. any ideas will be appreciated thanks... (8 Replies)
Discussion started by: meny
8 Replies

5. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

6. Shell Programming and Scripting

grep out and fix a section

I have a file that contains a section of information like this: 10_82_150_13.netshhcp Server 10.82.150.13 Scope 10.82.128.0 Add reservedip 10.82.130.54 0060b09f4b74 "PR_EMD_METALS1.san.local" "TSF 5th floor room 507 h plj4000" "BOTH" 10_82_150_13.netshhcp Server 10.82.150.13 Scope 10.82.128.0... (13 Replies)
Discussion started by: richsark
13 Replies

7. Shell Programming and Scripting

Prepend first line of section to each line until the next section header

I have searched in a variety of ways in a variety of places but have come up empty. I would like to prepend a portion of a section header to each following line until the next section header. I have been using sed for most things up until now but I'd go for a solution in just about anything--... (7 Replies)
Discussion started by: pagrus
7 Replies

8. Shell Programming and Scripting

Grep or print each section of a file on one line with a separator

I can obtain information from itdt inventory command however it display as below, I'd like to print each entity on one line but seperated by : the file is something like and each section ends with Volume Tag Drive Address 256 Drive State ................... Normal ASC/ASCQ... (3 Replies)
Discussion started by: gefa
3 Replies

9. Shell Programming and Scripting

Grep text and see all section

Hello I am looking for a way to look in files and to grep text and see the all section of the text ! Sharon123 deed 10000 class 360 ! sharon456 2000 deed ! Sharon789 live 3000 ! To grep "deed "an see the all section (5 Replies)
Discussion started by: sharong
5 Replies

10. UNIX for Beginners Questions & Answers

Grep a section from an UNIX file obtaining only part of the data

Hello, I have a log file that has several sections "BEGIN JOB, End of job" like in the following example: 19/06/12 - 16:00:57 (27787398-449294): BEGIN JOB j1(27787398-449294) JOB1 19/06/12 - 16:00:57 (27787398-449294): DIGIT: 0 number of present logs : 1 19/06/12 - 16:00:57... (4 Replies)
Discussion started by: mvalonso
4 Replies
Test::Harness::Results(3pm)				 Perl Programmers Reference Guide			       Test::Harness::Results(3pm)

NAME
Test::Harness::Results - object for tracking results from a single test file SYNOPSIS
One Test::Harness::Results object represents the results from one test file getting analyzed. CONSTRUCTION
new() my $results = new Test::Harness::Results; Create a test point object. Typically, however, you'll not create one yourself, but access a Results object returned to you by Test::Harness::Results. ACCESSORS
The following data points are defined: passing true if the whole test is considered a pass (or skipped), false if its a failure exit the exit code of the test run, if from a file wait the wait code of the test run, if from a file max total tests which should have been run seen total tests actually seen skip_all if the whole test was skipped, this will contain the reason. ok number of tests which passed (including todo and skips) todo number of todo tests seen bonus number of todo tests which unexpectedly passed skip number of tests skipped So a successful test should have max == seen == ok. There is one final item, the details. details an array ref reporting the result of each test looks like this: $results{details}[$test_num - 1] = { ok => is the test considered ok? actual_ok => did it literally say 'ok'? name => name of the test (if any) diagnostics => test diagnostics (if any) type => 'skip' or 'todo' (if any) reason => reason for the above (if any) }; Element 0 of the details is test #1. I tried it with element 1 being #1 and 0 being empty, this is less awkward. Each of the following fields has a getter and setter method. o wait o exit perl v5.10.0 2007-12-18 Test::Harness::Results(3pm)
All times are GMT -4. The time now is 05:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy