Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6332447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:15:58+00:00 2026-05-24T18:15:58+00:00

I have a file with format like : [PATTERN] line1 line2 line3 . .

  • 0

I have a file with format like :

[PATTERN]
line1
line2
line3
.
.
.
line
[PATTERN]
line1
line2
line3
.
.
.
line
[PATTERN]
line1
line2
line3
.
.
.
line

I want to extract the following blocks from above file :

[PATTERN]
line1
line2
line3
.
.
.
line

Note: Number of lines between 2 [PATTERN] may varies, so can’t rely on number of lines.

Basically, I want to store each pattern and the lines following it to Database, so I wil have to iterate all such blocks in my file.

How do this with Shell Scripting ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:15:58+00:00Added an answer on May 24, 2026 at 6:15 pm

    This assumes you are using bash as your shell. For other shells, the actual solution can be different.

    Assuming your data is in data:

    i=0 ; cat data  | while read line ; do \
      if [ "$line" == "[PATTERN]" ] ; then \
        i=$(($i + 1)) ; touch file.$i ; continue ; \
      fi ; echo "$line" >> file.$i ; \
    done
    

    Change [PATTERN] by your actual separation pattern.

    This will create files file.1, file.2, etc.

    Edit: responding to request about an awk solution:

    awk '/^\[PATTERN\]$/{close("file"f);f++;next}{print $0 > "file"f}' data
    

    The idea is to open a new file each time the [PATTERN] is found (skipping that line – next command), and writing all successive lines to that file. If you need to include [PATTERN] in your generated files, delete the next command.

    Notice the escaping of the [ and ], which have special meaning for regular expressions. If your pattern does not contain those, you do not need the escaping. The ^ and $ are advisable, since they tie your pattern to the beginning and end of line, which you will usually need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom XML file format which can contain blocks of code within
I know of the US GIS TIGER file format from years ago, but have
I have a Java-application that loads data from a legacy file format into an
I have a file that I only want to extract cells B9, B19, B29,
I have a huge file of code with many lines like this: Enterprise::TextMessageBox::Show(String::Format(SВъзникнал е
I have a custom file format for graphs which looks like this: node1.link1 :
I have a custom file format that I'd like validated by eclipse. In other
I have to work a lot with the flexible file format Yaml, i like
I have a string in the following format in a comma delimited file: someText,
I have a file, the text format is like this: .640 .070 -.390 -.740

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.