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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:43:56+00:00 2026-06-08T02:43:56+00:00

I want to sort a file based on specific character in a .txt file.

  • 0

I want to sort a file based on specific character in a .txt file.

This is a file given to me:

12345678901234567890123456789012345
header     1stfoo   DDMMYYYY 2ndfoo
sltele     Hoodie   24051988 d12Hdq
sltele     Hoodie   07051987 d30Hdq
sltele     Hoodie   07082011 d08Hdq
sltele     Hoodie   09081961 d04Hdq
sltele     Hoodie   20041962 d14Hdq
sltele     Hoodie   20032000 d01Hdq
sltele     Hoodie   13062002 d05Hdq

As you can see there is a column contains date with DDMMYYY format. If I sort it with sort -n -k 3,3 thisfile.txt > sortedfile.txt i got this result:

sltele     Hoodie   07051987 d30Hdq
sltele     Hoodie   07082011 d08Hdq
sltele     Hoodie   09081961 d04Hdq
sltele     Hoodie   13062002 d05Hdq
sltele     Hoodie   20032000 d01Hdq
sltele     Hoodie   20041962 d14Hdq
sltele     Hoodie   24051988 d12Hdq

but, I want the result like this:

sltele     Hoodie   09081961 d04Hdq
sltele     Hoodie   20041962 d14Hdq
sltele     Hoodie   07051987 d30Hdq
sltele     Hoodie   24051988 d12Hdq
sltele     Hoodie   20032000 d01Hdq
sltele     Hoodie   13062002 d05Hdq
sltele     Hoodie   07082011 d08Hdq

As a valid sortedfile.txt based on DDMMYYYY date format.

can somebady help me?

thanks in advance

  • 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-08T02:43:58+00:00Added an answer on June 8, 2026 at 2:43 am

    You can use the sort command, specifying multiple keys, and key start end positions:

    sort -n -k 3.8,3.12 -k 3.6,3.7 -k 3.4,3.5 < input_file
    

    output:

    sltele     Hoodie   09081961 d04Hdq
    sltele     Hoodie   07051987 d30Hdq
    sltele     Hoodie   24051988 d12Hdq
    sltele     Hoodie   20032000 d01Hdq
    sltele     Hoodie   20042000 d14Hdq
    sltele     Hoodie   13062002 d05Hdq
    sltele     Hoodie   07082011 d08Hdq
    

    from sort man-page:

    KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where
    F is a field number and C a character position in the field; both are
    origin 1 … characters in a field are counted from the beginning of
    the preceding whitespace.

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

Sidebar

Related Questions

I have an input file that I want to sort based on timestamp which
I want to parse xml file in utf-8 and sort it by some field.
i want to sort dictionary based upon value of each dictioanry item.but if i
I want to write a following script: given a text file with the list
I want to sort a file in unix shell. Can I redirect my result
I have a file file1.txt with data: 2010/09/04,21:53:42.048,a 2010/09/04,21:53:40.923,b 2010/09/04,21:53:40.923,a 2010/09/04,21:50:42.048,a. I want to
I want to do the opposite of this question , and sort of the
I have this Perl script which is used to sort packages based on package
I want to sort a mutable array by date. My array contains several dict
I want to sort the items that have been previously selected with checkboxes. If

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.