Delete multiple lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete multiple lines in a file
# 1  
Old 12-03-2010
MySQL Delete multiple lines in a file

The high level requirement is as follows:
I have a file which has multiple line starting with pattern (which is fixed say "Hello"
and i need to search for one more pattern in that line which starts with "Hello" and if the pattern matches, i need to delete lines from that line to the next line which again starts with "Hello"

Can you please help me out how can we accomplish this with awk or sed or any other command.

Last edited by KeerthiReddy; 12-03-2010 at 02:06 PM..
# 2  
Old 12-04-2010
Code:
awk '/^Hello/{(/..*Hello.*/)?f=1:f=0}(!f){print $0}' infile

could also be shorten
Code:
awk '/^Hello/{f=(/..*Hello.*/)?0:1}f{print $0}' infile

Code:
$ cat tst
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
gfdsjlkgjkdfs
Hello jsdqkljklsd Hello gjfdsklgjfdls
klggfdjkslgkjlfdsogfjdskl
jgfdskl
jkglfds
Hello jsdqkljklsd Hello jgkfdls
gfdsjhl
jgfkldsjgf
kjglfdksl
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
Hello jsdqkljklsd Hello jgkfdls
gfdsjhl
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs

blue sections are removed
Code:
$ awk '/^Hello/{(/..*Hello.*/)?f=1:f=0}(!f){print $0}' tst
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
gfdsjlkgjkdfs
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs

You also could pass "Hello" through a variable (at least it works here below on FreeBDS)

Code:
awk '/^'"$a"'/{f=(/..*'"$a"'.*/)?0:1}f{print $0}' infile

Code:
$ a=Hello
$ awk '/^'"$a"'/{f=(/..*'"$a"'.*/)?0:1}f{print $0}' tst
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
gfdsjlkgjkdfs
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
Hello jsdqkljklsd gjfdsklgjfdls
jgklfdsgjldfs
$


Last edited by ctsgnb; 12-04-2010 at 09:45 AM..
# 3  
Old 12-04-2010
thanks !!!
i am pretty much new to UNIX scripting..
i think i understood the command but can you elaborately tell how it works.
# 4  
Old 12-04-2010
/^Hello/if a line start with "Hello" ...
{(/..*Hello.*/)?f=1:f=0}... then if Hello is found after any single character or more (this will match only if more than 1 occurrence of "Hello" in the line) then set flag f to 1 (otherwhise 0)
(!f){print $0}'for any lines for which the flag is not 1, print the line

(1=true, 0 or empty = false)
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 12-05-2010
thanks ctsgnb for you description !
# 6  
Old 12-05-2010
thanks for the explanation.
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

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. Shell Programming and Scripting

delete multiple lines by line number

I have file with 10000 records and i need to delete the lines in single shot based on line number range say from 10 to 51 , 53 to 59 , 105 to 107, 311 to 592 etc... between range works fine for me but how to achive for above case? please help sed '10,51 {d}' infile > outfile (5 Replies)
Discussion started by: zooby
5 Replies

5. Shell Programming and Scripting

Delete multiple lines from a file

Hi, I'm trying to delete some entry's, the source is a file1, from file2 what I have until now is this file1 : 68255706,234200801053269,447916926187,8944200006353029289F 73495477,234200101579319,447861769299,8944200006852033303F file2: 353851164675 NEW : 272050001241889 -ok ... (10 Replies)
Discussion started by: BlueRay86
10 Replies

6. UNIX for Dummies Questions & Answers

delete multiple lines by line number

I have been googling, but cannot find that works for me. I have a text file tmp.out with contents: sadfsdf sdfosuidhfousdhof soduhf osdfu osudfhosudhfd sdfgsdfg asdfiojhsdf asdoludhflsdjfhskldjfhsdjdlfsjdhnlj h sdja ouahsdjdafkljsa oljhljh I have another file... (11 Replies)
Discussion started by: ChicagoBlues
11 Replies

7. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies

8. Shell Programming and Scripting

Need to delete multiple lines in a file.

Hi, I'm new to this forum, and searched through the previous posts, but didn't see anything close enough to what i'm looking for. I have a radius file like this: testone Password = "11111" Service-Type = "Framed-User", Session-Timeout =... (6 Replies)
Discussion started by: kangdom
6 Replies

9. Shell Programming and Scripting

delete multiple empty lines

Hi, I need to delete the lines with empty name. What is the best way to do it? Thanks a lot for your help! EMID MMDDYY HOURS JOB EMNAME 0241 051605 11.40 52062 someone 0520 051605 10.63 52062 0520 051605 10.66 52062 0520 051605 10.65 52062 (3 Replies)
Discussion started by: whatisthis
3 Replies

10. Shell Programming and Scripting

Delete multiple lines w/ sed

Hi all, I am trying to figure out the syntx to delete multiple lines w/ sed. I know the following syntax will delete lines 1 THROUGH 5 from filex: sed 1,5d filex But I wan to delete lines 1 AND 5 (keeping lines 2,3, and 4). Does anyone know how to do this in a single sed statement? ... (2 Replies)
Discussion started by: bookoo
2 Replies
Login or Register to Ask a Question