Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delete text block with awk

Posted on 2013-06-11
2
Medium Priority
?
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Beneford earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Suggested Courses

618 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