Solved

Delete text block with awk

Posted on 2013-06-11
2
310 Views
Last Modified: 2013-06-13
Hi,
I have big files with a lot of blocks of text like below

START SECTION - Description about block
Here text (can be a lot of lines)
END  SECTION

START SECTION - Description about block
Here text (can be a lot of lines)
END  SECTION

.....etc

I am redirecting each block to the separate file using command below:
 awk '/START SECTION/{n++}{print >"out" n ".txt" }' input_file.txt

I need to delete each block from input file after it was redirected to the separated file

Thanks
0
Comment
Question by:IKeystone
2 Comments
 
LVL 7

Accepted Solution

by:
Beneford earned 500 total points
ID: 39238602
You can do that with sed.

sed -e "/START SECTION/,/END SECTION/d" < input_file.txt > reduced_file.txt
0
 
LVL 11

Expert Comment

by:tel2
ID: 39242959
Hi IKeystone,

As you've probably noticed, Beneford's solution doesn't replace the awk solution you've provided, but needs to be run in addition to (and after) it.
Some notes:
- sed's '-e' switch is not necessary in this case.
- Change 'END SECTION' to 'END  SECTION' (extra space) so it will work with your input data.
- To do an in-place edit on the input file, you could do:
    sed -i '/START SECTION/,/END  SECTION/d' input_file.txt
Unfortunately, I don't think awk can do in-place edits, though this can be easily worked around.

Q1.  Would you accept a Perl solution which handles all the things you've asked for?
Q2.  Are you completely happy with what your awk solution does, apart from the fact that it doesn't delete the matched blocks from the input file?  (Please answer this one carefully.)

tel2
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video discusses moving either the default database or any database to a new volume.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now