Grep text and see all section


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep text and see all section
# 1  
Old 06-02-2014
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

Code:
! 
Sharon123
deed
10000
class 360
! 
sharon456
2000
deed
! 
Sharon789
live
3000
!

To grep "deed "an see the all section

The output is
Code:
! 
Sharon123
deed
10000
class 360
! 
sharon456
2000
deed
!


Thanks
Sharon

Last edited by Scrutinizer; 06-02-2014 at 08:57 AM.. Reason: code tags, formatting
# 2  
Old 06-02-2014
Try
Code:
awk -vRS="!" -vORS="!" '/deed/' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-02-2014
Or:
Code:
awk '/^!/{ if(p~s)printf "%s", p; p=x} {p=p $0 ORS} END{print "!"} ' s=deed file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 06-02-2014
Quote:
Originally Posted by Scrutinizer
Or:
Code:
awk '/^!/{ if(p~s)printf "%s", p; p=x} {p=p $0 ORS} END{print "!"} ' s=deed file

thanks

and one more thing
To grep "deed " and "live" and see the all section

Code:
! 
Sharon123
deed
10000
class 360
! 
sharon456
2000
deed
! 
Sharon789
live
3000
!


To grep "deed " and "live" and see the all section

The output is
Code:
! 
Sharon123
deed
10000
class 360
! 
sharon456
2000
deed
! 
Sharon789
live
3000
!


Thanks
Sharon

Last edited by Scrutinizer; 06-02-2014 at 09:56 AM.. Reason: CODE tags; removed spurious formatting
# 5  
Old 06-02-2014
You can change s=deed to : s="deed|live"
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 06-02-2014
Or:
Code:
awk -vRS="!" -vORS="!" 'NR==1 || /deed|live/' file

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

How to Grep of by section?

I have a script that outputs this as a file 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:... (4 Replies)
Discussion started by: ajetangay
4 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

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

5. Shell Programming and Scripting

Extracting text from within a section of text using AWK

I have a command which returns the below output. How can I write a script to extract mainhost and secondhost from this output and put it into an array? I may sometimes have more hosts like thirdhost. I am redirecting this output to a variable. So I guess there should be a awk or sed command to... (7 Replies)
Discussion started by: heykiran
7 Replies

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

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

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

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

10. Shell Programming and Scripting

Sorting rules on a text section

Hi all My text file looks like this: start doc ... (certain number of records) REC3|Emma|info| REC3|Lukas|info| REC3|Arthur|info| ... (certain number of records) end doc start doc ... (certain number of records)... (4 Replies)
Discussion started by: Indalecio
4 Replies
Login or Register to Ask a Question