Delete a block of text delimited by blank lines when pattern is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete a block of text delimited by blank lines when pattern is found
# 15  
Old 12-15-2007
Quote:
Originally Posted by radoulov
Post a sample from your (real) file.
Code:
gawk '!/initials: 1/' ORS="\n\n" RS= myfile

Here's a sample of myfile where the second block should be deleted because of the pattern - initials: 1
Code:
dn: cn=LMP0330,ou=People,o=myOrg
destinationIndicator: FR
mail: a.b@myOrg.com
initials: 0
givenName: LMP0330
fullName: LMP0340 LMP0330
Language: FR
title: Mr
sn: LMP0340
street: 
postalCode: B106
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204107Z
description: LMP0330
cn: LMP0330
ACL: 2#subtree#cn=LMP0330,ou=People,o=myOrg
 #[All Attributes Rights]
ACL: 6#entry#cn=LMP0330,ou=People,o=myOrg#l
 oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0330,ou=People,o=myOrg#p
 rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress

dn: cn=LMP0339,ou=People,o=myOrg
destinationIndicator: BE
mail: a.b@myOrg.com
initials: 1
givenName: LMP0339
fullName: LMP0339 LMP0339
Language: NL
title: Mr
sn: LMP0339
street: bourget
postalCode: 1060
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204102Z
cn: LMP0339
ACL: 2#subtree#cn=LMP0339,ou=People,o=myOrg
 #[All Attributes Rights]
ACL: 6#entry#cn=LMP0339,ou=People,o=myOrg#l
 oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0339,ou=People,o=myOrg#p
 rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress

dn: cn=LMP0340,ou=People,o=myOrg
destinationIndicator: GB
mail: a.b@myOrg.com
initials: 0
givenName: LMP0340
fullName: LMP0340 LMP0340
Language: EN
title: Mr
sn: LMP0340
street: Waverley
postalCode: SE25
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204107Z
description: LMP0340
cn: LMP0340
ACL: 2#subtree#cn=LMP0340,ou=People,o=myOrg
 #[All Attributes Rights]
ACL: 6#entry#cn=LMP0340,ou=People,o=myOrg#l
 oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0340,ou=People,o=myOrg#p
 rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress

# 16  
Old 12-15-2007
Using copy/paste from your post it gets deleted:

Code:
zsh-4.3.4-dev-4% cat data                                
dn: cn=LMP0330,ou=People,o=myOrg
destinationIndicator: FR
mail: a.b@myOrg.com
initials: 0
givenName: LMP0330
fullName: LMP0340 LMP0330
Language: FR
title: Mr
sn: LMP0340
street:
postalCode: B106
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204107Z
description: LMP0330
cn: LMP0330
ACL: 2#subtree#cn=LMP0330,ou=People,o=myOrg
#[All Attributes Rights]
ACL: 6#entry#cn=LMP0330,ou=People,o=myOrg#l
oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0330,ou=People,o=myOrg#p
rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress

dn: cn=LMP0339,ou=People,o=myOrg
destinationIndicator: BE
mail: a.b@myOrg.com
initials: 1
givenName: LMP0339
fullName: LMP0339 LMP0339
Language: NL
title: Mr
sn: LMP0339
street: bourget
postalCode: 1060
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204102Z
cn: LMP0339
ACL: 2#subtree#cn=LMP0339,ou=People,o=myOrg
#[All Attributes Rights]
ACL: 6#entry#cn=LMP0339,ou=People,o=myOrg#l
oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0339,ou=People,o=myOrg#p
rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress

dn: cn=LMP0340,ou=People,o=myOrg
destinationIndicator: GB
mail: a.b@myOrg.com
initials: 0
givenName: LMP0340
fullName: LMP0340 LMP0340
Language: EN
title: Mr
sn: LMP0340
street: Waverley
postalCode: SE25
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204107Z
description: LMP0340
cn: LMP0340
ACL: 2#subtree#cn=LMP0340,ou=People,o=myOrg
#[All Attributes Rights]
ACL: 6#entry#cn=LMP0340,ou=People,o=myOrg#l
oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0340,ou=People,o=myOrg#p
rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress
zsh-4.3.4-dev-4% awk '!/initials: 1/' ORS="\n\n" RS= data
dn: cn=LMP0330,ou=People,o=myOrg
destinationIndicator: FR
mail: a.b@myOrg.com
initials: 0
givenName: LMP0330
fullName: LMP0340 LMP0330
Language: FR
title: Mr
sn: LMP0340
street:
postalCode: B106
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204107Z
description: LMP0330
cn: LMP0330
ACL: 2#subtree#cn=LMP0330,ou=People,o=myOrg
#[All Attributes Rights]
ACL: 6#entry#cn=LMP0330,ou=People,o=myOrg#l
oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0330,ou=People,o=myOrg#p
rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress

dn: cn=LMP0340,ou=People,o=myOrg
destinationIndicator: GB
mail: a.b@myOrg.com
initials: 0
givenName: LMP0340
fullName: LMP0340 LMP0340
Language: EN
title: Mr
sn: LMP0340
street: Waverley
postalCode: SE25
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: Person
objectClass: ndsLoginProperties
objectClass: Top
loginTime: 20070303204107Z
description: LMP0340
cn: LMP0340
ACL: 2#subtree#cn=LMP0340,ou=People,o=myOrg
#[All Attributes Rights]
ACL: 6#entry#cn=LMP0340,ou=People,o=myOrg#l
oginScript
ACL: 2#entry#[Public]#messageServer
ACL: 2#entry#[Root]#groupMembership
ACL: 6#entry#cn=LMP0340,ou=People,o=myOrg#p
rintJobConfiguration
ACL: 2#entry#[Root]#networkAddress

zsh-4.3.4-dev-4%

What is the output from this command:

Code:
awk '!/initials: 1/' ORS="\n\n" RS= data|fgrep initial

# 17  
Old 12-15-2007
What is the output from this command:

Code:
awk '!/initials: 1/' ORS="\n\n" RS= data|fgrep initial

nothing at all (after replacing data with myfile).
hmm...looks like my gawk (and awk) is broken on my linux box.

And thanks for all your patience and help. I will try it when I get back to work on Monday and hopefully it will work.
# 18  
Old 12-16-2007
awk

Hi,
Try follow one. It should be ok.
iuput:
Code:
a A
b B
pattern
c C
d D

e E
f F
g G
h H

i I
j J
pattern
k K 
l L

a A
b B
c C
d D

output:
Code:
e E
f F
g G
h H


a A
b B
c C
d D

Code:
code:
awk 'BEGIN{
RS=""
FS="\r"
}
{
if ($0 !~ /pattern/)
	print 
print ""
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

Tried many options but unable to delete blank lines from text file

Hi, I tried the following options but was unable to delete blank lines from file Input file = temp.hash.txt temp.hash.txt content 90 0 89.56 0 0 57575.4544 56.89 (9 Replies)
Discussion started by: uuuunnnn
9 Replies

3. UNIX for Dummies Questions & Answers

How to delete blank line/s before and after a search pattern?

Hi, Test file x.txt below. This file is generated by a program that I unfortunately do not have control on how it gets presented/generated. create PACKAGE "XXX_INTERFACE_DEFECT_RPT_TEST" is TYPE refCursor IS REF CURSOR; Function queryRecords ( p_status varchar2, ... ... ... )... (4 Replies)
Discussion started by: newbie_01
4 Replies

4. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

5. Shell Programming and Scripting

Remove blank columns from a tab delimited text file

Hello, I have some tab delimited files that may contain blank columns. I would like to delete the blank columns if they exist. There is no clear pattern for when a blank occurs. I was thinking of using sed to replace instances of double tab with blank, sed 's/\t\t//g' All of the examples... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

6. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

7. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

8. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

9. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

10. Shell Programming and Scripting

delete block of lines when pattern does not match

I have this input file that I need to remove lines which represents more than 30 days of processing. Input file: On 11/17/2009 at 12:30:00, Program started processing...argc=7 Total number of bytes in file being processed is 390 Message buffer of length=390 was allocated successfully... (1 Reply)
Discussion started by: udelalv
1 Replies
Login or Register to Ask a Question