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 6988773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:07:30+00:00 2026-05-27T19:07:30+00:00

I have the following sed commands to split a file in two: sed -n

  • 0

I have the following sed commands to split a file in two:

sed -n '1,/(-!-)/{/(-!-)/!p}' file.temp file1.txt
sed -n '/(-!-)/,${/(-!-)/!p}' file.temp file2.txt

the file is like 123 (-!-) 321

and I got this result:

file1:
123

file2:
321

this works just great but I wanted it split to 4 files, it means I will use 3 separators

Can someone give me a hint about how to do it?

  • 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-27T19:07:31+00:00Added an answer on May 27, 2026 at 7:07 pm

    sed is probably not the right tool for this job. awk, on the other hand, is able to split records on arbitrary regular expressions, which ends up making this very easy. First, here’s a way to extract a particular section from your file.

    Given this input:

    this is section 1
    (-!-)
    this is section 2
    (-!-)
    this is section 3
    

    We can extract just section three like this:

    awk -vRS='\n[(]-!-[)]\n' 'NR==3 {print}' file.temp
    

    Which gives us:

    this is section 3
    

    In this command, we’re setting awk’s record separator (the RS variable) to a regular expression matching the separator in your file. Normally, awk uses a newline as the record separator, so each line is a new record. By setting RS explicitly to this regular expression, we are making awk treat each section as a single entity.

    You can extract each section into a separate file like this:

    awk -vRS='\n[(]-!-[)]\n' -vprefix="file" '{print > prefix NR}' file.temp
    

    This will give you the files file1, file2, and file3 as output, each containing the content of the appropriate section. The filenames are generated from the value of the “prefix” variable (set by the -vprefix="file" option ) and the current record number.

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

Sidebar

Related Questions

I thought I understood sed but I guess not. I have the following two
I have a file say file.txt with following contents: Capsule arr[0] in state A
I have a file which has blank lines but the following sed command is
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I have a file which contains lines of the following format: w1#1#x w2#4#b w3#2#d
I would like one sed command to accomplish the following: $ sed s'/:/ /g'
I have a config file that contains the following line: pre_args='$REGION','xyz',3 Using perl from
Possible Duplicate: Join lines based on pattern I have the following file: test one
I have the following regular expression working in sed but I want to retain
I have a line in my.cnf file datadir = /var/lib/mysql The following changes it

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.