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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:18:19+00:00 2026-06-12T05:18:19+00:00

I have an input file as following. I need to break them into multiple

  • 0

I have an input file as following. I need to break them into multiple files based on the columns 2,3&5. The file has more columns but i have used cut command to get only the required columns.

12,Accounts,India,free,Internal
13,Finance,China,used,Internal
16,Finance,China,free,Internal
12,HR,India,free,External
19,HR,China,used,Internal
33,Finance,Japan,free,Internal
39,Accounts,US,used,External
14,Accounts,Japan,used,External
11,Finance,India,used,External
11,HR,US,used,External
10,HR,India,used,External

Output files:

Accounts_India_Internal --
12,Accounts,India,free,Internal

Finance_China_Internal --
13,Finance,China,used,Internal
16,Finance,China,free,Internal

HR_India_External --
12,HR,India,free,External
10,HR,India,used,External

HR_China_Internal --
19,HR,China,used,Internal

and so on..

Please let me know how to achieve this.

As of now, I am thinking to sort the file based on these columns (2,3,5) and then run a loop on each record and start creating files. If a file does not exist, then create and add the record. Otherwise open the old file and add the record.

Is it possible to do this using shell scripting (bash)?

  • 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-06-12T05:18:20+00:00Added an answer on June 12, 2026 at 5:18 am

    Is it possible to do this using shell scripting (bash)?

    If you simply want to split the files based on fields 2, 3 and 5 you can do that quickly with awk:

    awk -F, '{print >> $2"_"$3"_"$5}' infile.txt 
    

    That appends each line to a file whose name is made up of fields 2, 3 and 5.

    Example:

    [me@home]$ awk -F, '{print >> $2"_"$3"_"$5}' infile.txt 
    [me@home]$ cat Accounts_India_Internal
    12,Accounts,India,free,Internal
    [me@home]$ cat Finance_China_Internal
    13,Finance,China,used,Internal
    16,Finance,China,free,Internal
    

    If you do want output sorted, you can first run the file through sort.

    sort -k2,3 -k5,5 -t, infile.txt  | awk -F, '{print >> $2"_"$3"_"$5}'
    

    That sorts the lines on fields 2, 3, and 5 before passing them on to the awk command.

    Do note that the we’re appending to the files so if you repeat the command without deleting the output files, you’ll end up with duplicate data in the output files. To address this, as well as include your additional requirements (using first line as header for all new files) as mentioned in the chat, see this solution.

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

Sidebar

Related Questions

I have the following file input box that allows for multiple upload: <input name=filesToUpload[]
Please help with the following: I have an input file that is quite heterogeneous
I have the following code to open a input and output file: if ((source_file_ptr
I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
In an HTML file I have the following: <input type=... name=myInput1 /> In a
I have the following target: <target name=promptforchoice> <input addproperty=choice> Copy the file?. [Y, n]
I have a list of input type=file with their names as list[] . Following
I need to upload multiple files into web server using MVC3 with RAZOR. I
I have a HTML page with the following input tag: ... <input type=file id=browseContactImageButton
I have an application using the following code to get input based on a

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.