Solved

Delete text block with awk

Posted on 2013-06-11
2
332 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 12

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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