Deleting all the N lines after the line in which text is found.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting all the N lines after the line in which text is found.
# 1  
Old 12-02-2015
Deleting all the N lines after the line in which text is found.

Hi!
I want to delete N (say 10) lines after the line which text is found in a file "A".Also to delete the line in which the text is found.

Only one occurrence of the search string in the file "A"
The text to be deleted is in another text file "B". All the search texts in the file "B" are in separate lines.

File "A"
Code:
: (NDLS_UTN_10.106.1.206
			:AdminInfo (
				:chkpf_uid ("{BBCCD0A6-F2EA-4ED5-9E96-9F71E6540419}")
				:ClassName (host_plain)
				:table (network_objects)
				:LastModified (
					:Time ("Mon Mar 23 09:34:02 2015")
					:last_modified_utc (1427103242)
					:By (admin)
					:From (Vivek-PC)
				)
				:icon ("NetworkObjects/Nodes/Host")
				:name (NDLS_UTN_10.106.1.206)
			)
			:DAG (false)
			:NAT ()
			:SNMP (
				:AdminInfo (
					:chkpf_uid ("{6150133D-3E06-4563-9EA9-B27E9C5F3E26}")
					:ClassName (SNMP)
				)
			)
			:read_community ()
			:sysContact ()
			:sysDescr ()
			:sysLocation ()
			:sysName ()
			:write_community ()
			:VPN ()
			:add_adtr_rule (false)
			:additional_products ()
			:addr_type_indication (IPv4)
			:certificates ()
			:color (black)
			:comments ()
			:connectra (false)
			:connectra_settings ()
			:cp_products_installed (false)
			:data_source (not-installed)
			:data_source_settings ()
			:edges ()
			:enforce_gtp_rate_limit (false)
			:firewall (not-installed)
			:floodgate (not-installed)
			:gtp_rate_limit (2048)
			:interfaces ()
			:ipaddr (10.106.1.206)
			:ipaddr6 ()
			:macAddress ()
			:os_info ()
			:type (host)
		)
		: (NDLS_UTN_10.106.1.207
			:AdminInfo (
				:chkpf_uid ("{33FBDF75-0FC8-4F49-A6DF-5A144115FECF}")
				:ClassName (host_plain)
				:table (network_objects)
				:LastModified (
					:Time ("Mon Mar 23 09:34:02 2015")
					:last_modified_utc (1427103242)
					:By (admin)
					:From (Vivek-PC)
				)
				:icon ("NetworkObjects/Nodes/Host")
				:name (NDLS_UTN_10.106.1.207)
			)
			:DAG (false)
			:NAT ()
			:SNMP (
				:AdminInfo (
					:chkpf_uid ("{B7AA47C7-7F33-469C-940C-37903B669CBD}")
					:ClassName (SNMP)
				)
			)
			:read_community ()
			:sysContact ()
			:sysDescr ()
			:sysLocation ()
			:sysName ()
			:write_community ()
			:VPN ()
			:add_adtr_rule (false)
			:additional_products ()
			:addr_type_indication (IPv4)
			:certificates ()
			:color (black)
			:comments ()
			:connectra (false)
			:connectra_settings ()
			:cp_products_installed (false)
			:data_source (not-installed)
			:data_source_settings ()
			:edges ()
			:enforce_gtp_rate_limit (false)
			:firewall (not-installed)
			:floodgate (not-installed)
			:gtp_rate_limit (2048)
			:interfaces ()
			:ipaddr (10.106.1.207)
			:ipaddr6 ()
			:macAddress ()
			:os_info ()
			:type (host)
		)
		: (NDLS_UTN_10.106.1.208
			:AdminInfo (
				:chkpf_uid ("{0E10C9A9-4C39-4CAA-9E64-A3E96F0D391D}")
				:ClassName (host_plain)
				:table (network_objects)
				:LastModified (
					:Time ("Mon Mar 23 09:34:02 2015")
					:last_modified_utc (1427103242)
					:By (admin)
					:From (Vivek-PC)
				)
				:icon ("NetworkObjects/Nodes/Host")
				:name (NDLS_UTN_10.106.1.208)
			)
			:DAG (false)
			:NAT ()
			:SNMP (
				:AdminInfo (
					:chkpf_uid ("{04BFBAEF-E1AB-436D-8973-38DD8BF532A2}")
					:ClassName (SNMP)
				)
			)
			:read_community ()
			:sysContact ()
			:sysDescr ()
			:sysLocation ()
			:sysName ()
			:write_community ()
			:VPN ()
			:add_adtr_rule (false)
			:additional_products ()
			:addr_type_indication (IPv4)
			:certificates ()
			:color (black)
			:comments ()
			:connectra (false)
			:connectra_settings ()
			:cp_products_installed (false)
			:data_source (not-installed)
			:data_source_settings ()
			:edges ()
			:enforce_gtp_rate_limit (false)
			:firewall (not-installed)
			:floodgate (not-installed)
			:gtp_rate_limit (2048)
			:interfaces ()
			:ipaddr (10.106.1.208)
			:ipaddr6 ()
			:macAddress ()
			:os_info ()
			:type (host)
		)

File "B"
Code:
NDLS_UTN_10.106.1.208
NDLS_UTN_10.106.1.206

No search pattern is required, only the search string / word is the pattern.

---------- Post updated at 10:29 PM ---------- Previous update was at 10:25 PM ----------

File A 500000 lines, file b contains 1500 texts
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for full-line and multi-line sample input, output, and code.

Last edited by Don Cragun; 12-02-2015 at 04:20 PM.. Reason: Change ICODE tags to CODE tags.
# 2  
Old 12-02-2015
None of the lines in file B is exactly matched in file A, but as a substring, they are found twice:
Code:
grep -ffileB fileA
: (NDLS_UTN_10.106.1.206
:name (NDLS_UTN_10.106.1.206)
: (NDLS_UTN_10.106.1.208
:name (NDLS_UTN_10.106.1.208)

Which one should be the target?
# 3  
Old 12-02-2015
deletion in file A for string in file B

Sorry. modified file "B"
Code:
: (NDLS_UTN_10.106.1.206
: (NDLS_UTN_10.106.1.208


Last edited by Don Cragun; 12-02-2015 at 04:21 PM.. Reason: Change ICODE tags to CODE tags.
# 4  
Old 12-02-2015
Try
Code:
awk 'FNR==NR {T[$0]; next}  $0 in T {P=NR+10}  NR>P ' fileB fileA

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting specific lines from text file via scripting

Hi, I'm trying to search for some number and from that line, i need to delete the 5th line exactly. Eg: Consider below as text file data: 10000 a b c d e . . . 10000 w q t (8 Replies)
Discussion started by: Gautham
8 Replies

2. Shell Programming and Scripting

Deleting Multiple Lines in a File1 using critera found from File 2

Hi Everyone! I would like ask if there's a better way I can delete multiple lines in a file1 by collecting all criteria from file2. file1: a b c d e f file2: a e f The expected value will be: b (3 Replies)
Discussion started by: zzavilz
3 Replies

3. UNIX for Dummies Questions & Answers

Display n lines after match found and other line

I have a file like this DoctorName Address1 Address2 DOB InsuredName Address1 Address2 DOB PatientName Address1 Address2 DOB ClaimNo1 DoctorName Address1 Address2 DOB InsuredName (2 Replies)
Discussion started by: nsuresh316
2 Replies

4. Shell Programming and Scripting

Deleting a matching string(line) which is also in other lines

Hi, i need help with my shell script I have a file input.txt containing the following contents /. /usr /usr/share /usr/share/doc /usr/share/doc/wine /usr/share/doc/wine/copyright /usr/share/doc/wine/changelog.Debian.gz I need output as /usr/share/doc/wine /usr/share/doc/wine/copyright... (3 Replies)
Discussion started by: Amit0991
3 Replies

5. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Shell Programming and Scripting

help with finding text and deleting line

HI All, I need to search for a particular pattern input by the user in order to delete the line. My username.txt has username@email.com:John:149.0.3.4:1 username1@email.com:Harry:149.0.3.4:1 username1@email.net:Alex:149.0.3.4:1 username1@email.edu:Nemo:149.0.3.4:1 The program i written ... (3 Replies)
Discussion started by: ichar
3 Replies

7. Shell Programming and Scripting

Multiple lines into one line when ; is found

I have sql's in a file separated by ";", need to put the sql in one single line till i find a ";" The input file is like this SELECT s.sid, s.serial#, p.spid as "OS PID", s.username, s.module, ... (6 Replies)
Discussion started by: ilugopal
6 Replies

8. UNIX for Dummies Questions & Answers

Deleting lines in text file

Hi everyone, I have text files that I want to delete lines from. I have searched through this forum for quite some time and found examples of both awk and sed. Unfortunately, I was not able to successfully do what I want. Well to some extent. I did manage to delete the first 15 lines from each... (5 Replies)
Discussion started by: hern14
5 Replies

9. Shell Programming and Scripting

deleting lines from multiple text files

I have a directory full of text data files. Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application. I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate... (3 Replies)
Discussion started by: vrms
3 Replies

10. Shell Programming and Scripting

Deleting lines above a certain line

Hi, I have a file that gets automatically generated and it would look something like sakjsd adssad {{word}} sddsasd dsdsasa . . . So basically what I want to do is just keep the stuff below the {{word}} marker. The marker includes the brackets. Is there any command to delete the... (3 Replies)
Discussion started by: eltinator
3 Replies
Login or Register to Ask a Question