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
ATF-TEST-PROGRAM(1)					    BSD General Commands Manual 				       ATF-TEST-PROGRAM(1)

NAME
atf-test-program -- common interface to ATF test programs SYNOPSIS
atf-test-program [-r resfile] [-s srcdir] [-v var1=value1 [.. -v varN=valueN]] test_case atf-test-program -l DESCRIPTION
Test programs written using the ATF libraries all share a common user interface, which is what this manual page describes. NOTE: There is no binary known as atf-test-program; what is described in this manual page is the command-line interface exposed by the atf-c, atf-c++ and atf-sh bindings. In the first synopsis form, the test program will execute the provided test case and print its results to the standard output, unless other- wise stated by the -r flag. Optionally, the test case name can be suffixed by ':cleanup', in which case the cleanup routine of the test case will be executed instead of the test case body; see atf-test-case(4). Note that the test case is executed without isolation, so it can and probably will create and modify files in the current directory. To execute test cases in a controller manner, refer to atf-run(1), which is the preferred way to run test cases. You should only execute test cases by hand for debugging purposes. In the second synopsis form, the test program will list all available test cases alongside their meta-data properties in a format that is machine parseable. This list is processed by atf-run(1) to know how to execute the test cases of a given test program. The following options are available: -l Lists available test cases alongside a brief description for each of them. -r resfile Specifies the file that will receive the test case result. If not specified, the test case prints its results to stdout. If the result of a test case needs to be parsed by another program, you must use this option to redirect the result to a file and then read the resulting file from the other program. Note: do not try to process the stdout of the test case because your program may break in the future. -s srcdir The path to the directory where the test program is located. This is needed in all cases, except when the test program is being executed from the current directory. The test program will use this path to locate any helper data files or utilities. -v var=value Sets the configuration variable var to the value value. SEE ALSO
atf-run(1), atf(7) BSD
February 6, 2011 BSD
All times are GMT -4. The time now is 12:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy