Remove text between headers while leaving headers intact


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove text between headers while leaving headers intact
# 1  
Old 12-27-2008
Remove text between headers while leaving headers intact

Hi,
I'm trying to strip all lines between two headers in a file:

### BEGIN ###

[Header 1]
Text to remove, contains all kinds of characters
...
Antispyware-Downloadserver.com (Germany)=http://www.antispyware-downloadserver.c
om/updates/
Antispyware-Downloadserver.com #2 (Germany)=http://www.antispyware-downloadserve
r.com/updates/
ComputerFixerTools.com (USA)=http://67.202.99.48/spybot/
Evertje Networks (Europe)=http://sbsd.mirror.evertjenetworks.nl/files/updates/
FastSpeedTest.com (USA)=http://fastspeedtest.net/mirrors/spybot/
...

[Header 2]
More stuff, more headers

### END ###

I've tried several different sed commands, and the results have been either nothing is removed, or the line [Header 2] is removed as well.

Sorry, I haven't saved a list of what I've tried; here is my most recent failed attempt though:

sed -e '/\[Header\ 1\]/,/\[Header\ 2\]/ { /^\[Header\ 2\]$/!d }

Any help would be appreciated, and thank you in advance =)
# 2  
Old 12-27-2008
Hi,

i am not sure, if i understand you correctly, but if you
try to delete everything from "[Header 1]" to "[Header 2]" not
including "[Header 2]", this will do it:

Code:
sed '/[[]Header 1]/,/[[]Header 2]/{/Header 2/!d}' file

HTH Chris
# 3  
Old 12-27-2008
Awesome! Worked like a charm; now I've just got to read up on sed syntax to understand exactly how it works =)

Thank you for your quick reply!
# 4  
Old 12-27-2008
This should give you all the info you need:

Code:
http://www.catonmat.net/blog/sed-one-liners-explained-part-one/
http://www.catonmat.net/blog/sed-one-liners-explained-part-two/

Code:
sed '/[[]Header 1]/,/[[]Header 2]/{/Header 2/!d}' file

means: search for the text between [Header 1] and [Header 2]. The
construct [[] is used to escape the "[" character. If "Header 1"
and "Header 2" are found, delete everything except the line containg
the string Header 2.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove white space and duplicate headers

I have a file called "dsout" with empty rows and duplicate headers. DATE TIME TOTAL_GB USED_GB %USED --------- -------- ---------- ---------- ---------- 03/05/013 12:34 PM 3151.24316 2331.56653 73.988785 ... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

2. Shell Programming and Scripting

Remove headers thar dont match

Good evening I need your help please, im new at Unix and i wanted to remove the first 5 headers for 100000 records files and then create a control file .ctl that contains the number of records and all seem to work out but when i tested at production it didnt wotk. Here is the code: #!... (6 Replies)
Discussion started by: alexcol
6 Replies

3. Shell Programming and Scripting

Faster command to remove headers for files in a directory

Good evening Im new at unix shell scripting and im planning to script a shell that removes headers for about 120 files in a directory and each file contains about 200000 lines in average. i know i will loop files to process each one and ive found in this great forum different solutions... (5 Replies)
Discussion started by: alexcol
5 Replies

4. UNIX for Dummies Questions & Answers

Using sed command to remove multiple instances of repeating headers in one file?

Hi, I have catenated multiple output files (from a monte carlo run) into one big output file. Each individual file has it's own two line header. So when I catenate, there are multiple two line headers (of the same wording) within the big file. How do I use the sed command to search for the... (1 Reply)
Discussion started by: rebazon
1 Replies

5. Shell Programming and Scripting

Remove interspersed headers in .dat file with AWK

Heya there, A small selection of my data is shown below. DATE TIME FRAC_DAYS_SINCE_JAN1 2011-06-25 08:03:20.000 175.33564815 2011-06-25 08:03:25.000 175.33570602 2011-06-25 ... (4 Replies)
Discussion started by: gd9629
4 Replies

6. UNIX for Dummies Questions & Answers

sed remove two headers; writing more elegant code

Hi there, I have two questions. First, I was wondering how to use sed to remove two header lines or two tail lines. Here I just do the same operation twice...I'm sure there is a better way. Second, and more importantly, is there a better way to have these operations use files other than... (5 Replies)
Discussion started by: mikey11415
5 Replies

7. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

8. UNIX for Dummies Questions & Answers

Remove certain headers using mailx or sendmail

Hello, So i want to send mails in any way from a solaris 5.8 system, perhaps using mailx or sendmail. My purpose is to stay clear of systems name in head data. So i want to strip at least the "Message-Id" and the "Recieved" headers of the mail. Yet this seems to be a bit of a problem. Now i... (2 Replies)
Discussion started by: congo
2 Replies

9. Shell Programming and Scripting

Remove Headers throughout a data file

I have a data file with over 500,000 records/lines that has the header throughout the file. SEQ_ID Name Start_Date Ins_date Add1 Add2 1 Harris 04/02/08 03/02/08 333 Main Suite 101 2 Smith 02/03/08 01/23/08 287 Jenkins SEQ_ID Name ... (3 Replies)
Discussion started by: psmall
3 Replies

10. UNIX for Dummies Questions & Answers

help:how to remove headers in output file

Hi I am running a script (which compares two directory contents) for which I am getting an output of 70 pages in which few pages are blank so I was able to delete those blank lines. But I also want to delete the headers present for each page. can any one help me by providing the code... (1 Reply)
Discussion started by: raj_thota
1 Replies
Login or Register to Ask a Question