Solved

Delete text block with awk

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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 …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

778 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