Find and select complete paragraph


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and select complete paragraph
# 1  
Old 12-07-2015
Find and select complete paragraph

HI Friends,
I need your help once again.

I have following

Code:
$cat april_2015.txt
== :: ==
Gender: Female
Service: Tattoo
Nature: Permanent
Amt: 21000 INR
Date: 04/04/2015
Artist: Anushka
== :: ==
Gender: Female
Service: Makeup
Nature: Bridal
Amt: 19200 INR
Date: 05/04/2015
Artist: Jenn
== :: ==
Gender: Male
Service: Tattoo
Nature: Permanent
Amt: 9500 INR
Date: 05/04/2015
Artist: Anushka
== :: ==
$
$

I want to grep "Permanent" in such a way that It will select it as follows.

Code:
== :: ==
Gender: Female
Service: Tattoo
Nature: Permanent
Amt: 21000 INR
Date: 04/04/2015
Artist: Anushka
== :: ==
Gender: Male
Service: Tattoo
Nature: Permanent
Amt: 9500 INR
Date: 05/04/2015
Artist: Anushka
== :: ==


Kindly suggest.
Anu...

Last edited by Don Cragun; 12-07-2015 at 04:50 AM.. Reason: Add CODE tags again.
# 2  
Old 12-07-2015
Hi Anu,
Moderator's Comments:
Mod Comment To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Continuing to ignore requests to properly format your posts may lead to suspension of your account.


What have your tried to solved this problem?
# 3  
Old 12-07-2015
THank you for the suggestion.

I can not think of any solution for this selection criteria.
Kindly help.
# 4  
Old 12-07-2015
Given that you have asked us for help 61 times before with some of your problems being very similar to this one, it is disappointing that you are unable to guess that awk would be a good way to solve a problem like this and show us a start to a solution to this on your own.

You might try something like:
Code:
awk '
$0 == "== :: ==" {
	if(perm) {
		if(!pc++)
			print
		for(i = 1; i <= lc; i++)
			print l[i]
		print
		perm = 0
	}
	lc = 0
	next
}
{	l[++lc] = $0
	if($0 == "Nature: Permanent")
		perm = 1
}' april_2015.txt

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 5  
Old 12-07-2015
This might not work on all awk versions:
Code:
awk '/Nat.*ermanent/ || NR==1; END {printf "\n"}' RS="== :: ==" ORS="== :: ==" file
== :: ==
Gender: Female
Service: Tattoo
Nature: Permanent
Amt: 21000 INR
Date: 04/04/2015
Artist: Anushka
== :: ==
Gender: Male
Service: Tattoo
Nature: Permanent
Amt: 9500 INR
Date: 05/04/2015
Artist: Anushka
== :: ==

This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-07-2015
Quote:
Originally Posted by RudiC
This might not work on all awk versions:
Code:
awk '/Nat.*ermanent/ || NR==1; END {printf "\n"}' RS="== :: ==" ORS="== :: ==" file
...

On systems where it does work, this is a great solution. It doesn't work on all systems, and the standards state that the behavior is unspecified if RS is set to a string containing more than one character.

The standard doesn't place any limits like that on ORS; only on RS.
# 7  
Old 12-07-2015
Hi, just for fun, with grep (gnu version 2.16 or higher):
Code:
grep -Pzo '(?s)== :: ==(\n*[^=]\N)*Permanent(\n[^=]\N*)*|== :: ==(?=[\n]$)' file

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract paragraph that contains a value x<-30

I am using OSX. I have a multi-mol2 file (text file with coordinates and info for several molecules). An example of two molecules in the file is given below for molecule1 and molecule 2. The total file contains >50,000 molecules. I would like to extract out and write to another file only the... (2 Replies)
Discussion started by: Egy
2 Replies

2. UNIX for Advanced & Expert Users

Find command takes too long to complete

Hi, Below is my find command find /opt/app/websphere -name myfolder -perm -600 | wc -l At time it even takes 20 mins to complete. my OS is : SunOS mypc 5.10 Generic_150400-09 sun4v sparc SUNW,T5440 (10 Replies)
Discussion started by: mohtashims
10 Replies

3. Shell Programming and Scripting

How to grep paragraph?

Hi, I have A file like this: >Contig1 AAAAAAATTTTTTCCCAATATATGAT ATATATAEATATATAT >Contig2 AAAAAAATTTTTTCCCAATATATGAT ATATATAEAATTTTTAATTTTTTCCCA ATCCCAAATATATAT >Contig3 AAAAAAATTTTTTCCCAATATATGAT ATATATAEAATTTTTAATTTTTTCCCA ATCCCAAATAAATTTTTTCCCAATAT ATGATATATATAEAATTTTTAATTTTT... (3 Replies)
Discussion started by: the_simpsons
3 Replies

4. UNIX for Dummies Questions & Answers

Unable to execute the complete cmd - using find command

Hi, I'm unable to execute the below command completely ; it's not allowing me to type the complete command. It is allowing till "xargs" and i cannot even press enter after that. I'm using Solaris. Let me know if anything needs to be added so as to execute the complete command. Appreciate... (12 Replies)
Discussion started by: venkatesht
12 Replies

5. Shell Programming and Scripting

How to find complete file names in UNIX if i know only extention of file

Suppose I have a file which contains other file names with some extention . text file containt gdsds sd8ef g/f/temp_temp.sum yyeta t/unix.sum ghfp hrwer h/y/test.text.dat if then.... I want to get the complete file names, like for above file I should get output as temp_temp.sum... (4 Replies)
Discussion started by: panchal
4 Replies

6. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

7. UNIX for Dummies Questions & Answers

BASH complete-filename & menu-complete together

Hi, Does anyone know how to make BASH provide a list of possible completions on the first tab, and then start cycling through the possibilites on the next tab? Right now this is what I have in my .bashrc: bind "set show-all-if-ambiguous on" bind \\C-o:menu-complete This allows... (0 Replies)
Discussion started by: Mithu
0 Replies

8. UNIX for Dummies Questions & Answers

how to find complete path of a file in unix

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to capture... (5 Replies)
Discussion started by: yahoo!
5 Replies

9. UNIX for Advanced & Expert Users

how to find complete path of a file in unix

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to capture... (1 Reply)
Discussion started by: yahoo!
1 Replies

10. Shell Programming and Scripting

Bold the paragraph

Hi, I have a file with multiple paragraph. I want to look for some word and make that paragraph bold. How can I do that? Thanks, Karthik (3 Replies)
Discussion started by: caprikar
3 Replies
Login or Register to Ask a Question