Work with awk and text blocks

Posted on 2012-08-14
Last Modified: 2012-08-16
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)

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


I need to forward each block to the separated file with unique file name (can be number of block).

Question by:IKeystone
    LVL 26

    Accepted Solution


    gawk '/START SECTION/{n++}{print >"out" n ".txt" }' input_file.txt

    Author Comment

    Tested in awk, working. Last question - how to add to the output files process id ?

    LVL 26

    Expert Comment


    gawk -v pid=$$  '/START SECTION/{n++}{print >"out" n "." pid ".txt" }' input_file.txt

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    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…
    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 …
    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…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now