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

  • Home
  • SEARCH
  • 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 7679747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:56:56+00:00 2026-05-31T17:56:56+00:00

I make the problem shorter. Actually I have data much longer than this. I

  • 0

I make the problem shorter. Actually I have data much longer than this.

I have a file like:

aa, bb, cc, dd, ee, 4
ff, gg, hh, ii, jj, 5
kk, ll, mm, nn, oo, 3
pp, qq, rr, ss, tt, 2
uu, vv, ww, xx, yy, 5
aa, bb, cc, dd, ee, 2

now I want to use awk to select each line with the same number in last column and redirect it into a new file, these new files will be different depending on the number in the last column.
eg. t2.txt, t3.txt, t4.txt, t5.txt will hold the lines with last number as 2,3,4,5 respectively.

in t2.txt:

pp, qq, rr, ss, tt, 2
aa, bb, cc, dd, ee, 2

in t3.txt:

kk, ll, mm, nn, oo, 3

in t4.txt:

aa, bb, cc, dd, ee, 4

in t5.txt:

ff, gg, hh, ii, jj, 5
uu, vv, ww, xx, yy, 5

I guess I need something like this:

BEGIN   {FS=","}
        {
        for (n=2; n<=5; n++)
        if ($6 ~/\$n/) {print > "t\$n.txt"}
        }       

But I just don’t know how to make it work.

This bash file do what I want, yet the problem is, each time it extracts lines with a specific number, it has to read in all the lines. How can I check ONLY TIME of the file and extract files for all numbers?

#!/bin/bash
for num in {2..5}; do      
gawk --assign FS="," "\$6 ~/${num}/" infile >> t${num}.txt
done
  • 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-31T17:56:57+00:00Added an answer on May 31, 2026 at 5:56 pm

    I get the answer, with the following it works:
    but any further explanation will be welcomed.

    BEGIN   {FS=","}
            {
            for (n=1; n<=5; n++)
            if ($6 ~/\$n/) {print > "new"$n".txt"}
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge problem with this method im trying to make. I have
I have used this tutorial to make a website, but I would like that
Does anybody have any idea how I would make this shorter and less redundant:
Possible Duplicate: PHP make if shorter I have an if statement that looks like
My essential problem is how to make arithmetic with floats on x86 behave like
I have to make a simple layout in android but have problem with the
I got an problem i'd like to solve. This expression works as long as
Showing dynamic data in table cell make problem for me. I am using [titleString
I have been trying to make this search engine for a MySQL database. Taking
So I'm having this strange problem. I have a ribbon moving behind the navigation

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.