Deleting a section based on search from other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting a section based on search from other file
# 1  
Old 04-06-2011
Deleting a section based on search from other file

Hi Everyone,

I need some help to accomplish the below. help is highly appriciated.
I have a 45 mb file with ldap entries. Each user entry is separated by a string # entry-id: 1 and so on. Some of the entries has a string xyz: true. I want to delete the section if the user section has xyz: true in it. Below is the sample

example file:

# entry-id: 100
cn: test@test1.com
abc: 111
xyz: true
def: 222
hij: 333

# entry-id: 101
cn: test@test1.com
abc: 222
def: 222
hij: 333

# entry-id: 103
cn: test@test1.com
abc: 222
xyz: true
def: 222
hij: 333

# entry-id: 104
cn: test@test1.com
abc: 222
def: 222
hij: 333

out put file
# entry-id: 101
cn: test@test1.com
abc: 222
def: 222
hij: 333

# entry-id: 104
cn: test@test1.com
abc: 222
def: 222
hij: 333


Thanks in advance
Samingla
# 2  
Old 04-06-2011
Code:
awk -vRS="\n\n" '! /xyz/' ORS="\n\n"

# 3  
Old 04-06-2011
Only needed to remove xyz: true, perhaps:
Code:
 awk -vRS="\n\n" '! /\nxyz: true/' ORS="\n\n"

# 4  
Old 04-06-2011
Hi Yinyuemi,

Thanks for the quick update. The awk that you have provided is not working as expected. I want the whole section to be deleted if the section has xyz: true in it. It is adding new line character in the string which is not required.

Thanks in advance.
Samingla

---------- Post updated at 06:57 PM ---------- Previous update was at 06:52 PM ----------

Hi chubler xl,
I need to delete the whole section if the section has xyz: true. Below is a section in the file that I am refering to.
# entry-id: 100
cn: test@test1.com
abc: 111
xyz: true
def: 222
hij: 333

Thanks,
Samingla
# 5  
Old 04-06-2011
I'm confused, that is what the code does, the output matches your output file with the exception of a couple of extra newlines on the end.
# 6  
Old 04-07-2011
Code:
awk 'BEGIN{RS="\n\n";ORS="\n\n"}!/xyz/{print}'  inputfile

# 7  
Old 04-07-2011
In case it's the cause of the problem (truthfully, I did not read up closely as I'm still mostly asleep Smilie), using more than one character for RS is a gawk extension. Perhaps what you want to do is set RS to an empty string. From the standard:

Quote:
The first character of the string value of RS shall be the input record separator; a <newline> by default. If RS contains more than one character, the results are unspecified. If RS is null, then records are separated by sequences consisting of a <newline> plus one or more blank lines, leading or trailing blank lines shall not result in empty records at the beginning or end of the input, and a <newline> shall always be a field separator, no matter what the value of FS is.
awk

Regards,
Alister

---------- Post updated at 02:20 AM ---------- Previous update was at 01:59 AM ----------

The following works fine (tested with nawk):
Code:
awk '!/(^|\n)xyz: true/' RS='' ORS='\n\n' file

Regards,
Alister

Last edited by alister; 04-07-2011 at 03:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying section of file based on search criteria

Hi Guru's, I am new to unix scripting. I have a huge file with user details in it(file2) and I have another file with a list of users(file1). Script has to search a user from file1 and get all the associated lines from file2. Example: fiel1: cn=abc cn=DEF cn=xyx File 2: dn:... (10 Replies)
Discussion started by: Samingla
10 Replies

2. Shell Programming and Scripting

Extract record from file based on section.

input file output file (1 Reply)
Discussion started by: lathigara
1 Replies

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

4. Shell Programming and Scripting

Search and Remove No data Section

Hello, I have written a script that removes duplicates within a file and places them in another report. File: ABC1 012345 header ABC2 7890-000 ABC3 012345 Content Header ABC5 593.0000 587.4800 ABC5 593.5000 587.6580 ABC5 593.5000 587.6580 ABC1 67890 header ABC2 1234-0001 ABC3... (2 Replies)
Discussion started by: petersf
2 Replies

5. Shell Programming and Scripting

Selecting A Section of A File Based On the Time Within It

Hi, I have a file formated like this: John 7.22 2010-01-25_17:01:36 George 8.22 2010-01-25_17:02:36 Bob 9.62 2010-01-25_17:04:36 Jane 10.11 2010-01-25_17:05:36 Emma 4.52 2010-01-25_17:01:36 What I want to do is cut out only the entries that have... (2 Replies)
Discussion started by: Donkey25
2 Replies

6. Shell Programming and Scripting

Deleting a line from a file based on one specific string instance?

Hello! I need to delete one line in a file which matches one very precise instance of a string only. When searching the forum I unfortunately only found a solution which would delete each line on which a particular string occurs. Let's assume I have a file composed of thousands of lines... (4 Replies)
Discussion started by: Black Sun
4 Replies

7. Shell Programming and Scripting

Search and extract by section from configuration

Hi, I understand either AWK or SED can do this, but I not sure how to extract the following configuration in section. Meaning when I need to find code with " ip helper-address 192.168.11.2" , it would start from "interface Serial0/0" and "interface FastEthernet0/1". Only displaying both section... (2 Replies)
Discussion started by: haphazard
2 Replies

8. UNIX for Dummies Questions & Answers

deleting lines from a delimited files based on a 2nd file

I need a little help... I have a file with 3 fields, Time/Date, IP address, UniqueID I have a 2nd file of UniqueIDs. I want to either (which ever is easier): 1. delete entries in file 1 that have a UniqueID in file 2 2. create a new file with the fields from File 1, excluding the... (4 Replies)
Discussion started by: goldie363
4 Replies

9. UNIX for Dummies Questions & Answers

deleting files based on file name and modified time

Hi, I have some log files created in the following fashion Ex: file name modified date 1) s.log1 01-jan-08 2) s.log2 02-jan-08 3) s.log3 03-jan-08 4) s.log4 04-jan-08 Now I want to have the latest 2 logs and delete the others. Can you tell me the one liner /... (1 Reply)
Discussion started by: ammu
1 Replies

10. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies
Login or Register to Ask a Question