How to Grep of by section?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Grep of by section?
# 1  
Old 07-07-2015
How to Grep of by section?

I have a script that outputs this as a file

Code:
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

I want to make it so that if some one uses (ex. grep Toby Smith) on the file, than they will get the name aswell as their memberships. Also, I can only use grep, so is there anything i can do in the script to allow grep to gather all the information for the each member.

Last edited by ajetangay; 07-07-2015 at 12:32 PM..
# 2  
Old 07-07-2015
Smash the group of lines into single lines and post process the grep to put it back.

Consider (assumes a Unix/Linux line terminated format in my case in a file called groups.txt):

Code:
sed -e 's/^$/^B/' <groups.txt |  tr '\012\002' '\011\012' | sed 's/^\o011//' | grep '^Sally Smith' | tr '\011' '\012'

You'll want the data to end with an empty line.

(oh and ^B is a Ctrl-B, I should have been more consistent in my approach)

Last edited by cjcox; 07-07-2015 at 12:23 PM..
# 3  
Old 07-07-2015
Check this ,it may help you
Code:
awk  'BEGIN{RS="----------------"}/John/{print;getline;print}'  file

# 4  
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
# 5  
Old 07-07-2015
Quote:
Originally Posted by ajetangay
[...]
I want to make it so that if some one uses (ex. grep Toby Smith) on the file, than they will get the name aswell as their memberships. Also, I can only use grep, so is there anything i can do in the script to allow grep to gather all the information for the each member.
How come that you can only use grep? Unfortunately, grep by itself does not have the functionality you are hoping for.

You make reference to a script. Could you post what you have been working on?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question