• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Delete text block with awk

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
IKeystone
Asked:
IKeystone
1 Solution
 
BenefordCommented:
You can do that with sed.

sed -e "/START SECTION/,/END SECTION/d" < input_file.txt > reduced_file.txt
0
 
tel2Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now