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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:34:06+00:00 2026-05-14T06:34:06+00:00

I have this file file.txt which I want to split into many smaller ones.

  • 0

I have this file “file.txt” which I want to split into many smaller ones.
This a piece of it:


0 id:2293 7:0.78235 12:0.69205 17:0.79421 21:0.77818 ..
4 id:2293 7:0.78235 8:0.97904 12:0.69205 17:0.31709 ..
1 id:2294 7:0.78235 8:0.90994 17:0.49058 21:0.59326 ..

Each line of the file has an id field which looks like “id:1” for a line belonging to id 1.
For each id in the file, I like to create a file named idid.txt and put all lines that belong to this id in that file.
My brute force bash script solution reads as follows.

count=1
while [ $count -lt 19945 ]
do
cat file.txt | grep "id:$count " >> ./sets/id$count.txt
count='expr $count + 1'
done

Now this is very inefficient as I have do read through the file about 20.000 times.
Is there a way to do the same operation with only one pass through the file? –
What I’m probably asking for is a way to use the value that matches for a regular expression to name the associated output file.

  • 1 1 Answer
  • 1 View
  • 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-14T06:34:07+00:00Added an answer on May 14, 2026 at 6:34 am
    $ cat file
    0 id:2293 7:0.78235 12:0.69205 17:0.79421 21:0.77818 ..
    4 id:2293 7:0.78235 8:0.97904 12:0.69205 17:0.31709 ..
    1 id:2294 7:0.78235 8:0.90994 17:0.49058 21:0.59326 ..
    
    $ awk -F"[: ]" '{print $0 > "id_"$3".txt"}' file
    
    $ more id_2293.txt
    0 id:2293 7:0.78235 12:0.69205 17:0.79421 21:0.77818 ..
    4 id:2293 7:0.78235 8:0.97904 12:0.69205 17:0.31709 ..
    
    $ more id_2294.txt
    1 id:2294 7:0.78235 8:0.90994 17:0.49058 21:0.59326 ..
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this text file acmst501:57:Runningacmst506:201:Runningacmst506:203:Runningacmst506:209:Runningacmst506:213:Failed which I want to split with delimiters. I
I have a .txt file having some text. I want to read this file
I want to load a file, split its content into arrays, and have the
I have a .txt file with product data, which I want to read in
I have a .txt file which contains no of insert query like this. I
I have a file universities.txt which looks like this: Alabama Air University Alabama A&M
What I have is this: E:\folder\00000000\file.txt and I want to get this: 00000000\file.txt Problem
I have a file(.txt) which contains: [0 1,1 1,3 2,4 1] I want to
OK. For example I have this line in my txt file: 1|1,12;7,19;6,4;8,19;2,2 As you
I have one file (for example: test.txt), this file contains some lines and for

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.