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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:51:50+00:00 2026-06-02T10:51:50+00:00

I am looking for a more specific version of the :sort u command that

  • 0

I am looking for a more specific version of the :sort u command that would allow removing all duplicate lines from a file. I am working with a CSV file, and would like to remove all the lines that have duplicates in their second-column entry. In other words, two lines are declared to be duplicates if they have the same value in the second column.

For example, for the following file:

a,1,b
g,1,f
c,1,x
i,2,l
m,1,k
o,2,p
u,1,z

the command in question should yield:

a,1,b
i,2,l

The choice of the specific rows to be kept are not important, as long as the second column entries are all unique.

What Vim command will produce the output above?

Thanks!

  • 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-02T10:51:53+00:00Added an answer on June 2, 2026 at 10:51 am

    Since it is not possible to achieve the transformation in question in
    one run of the :sort command, let us approach it as a two-step process.

    1. The first step is sorting lines by the values of the second column
    (separated from the first one by a comma). In order to do that, we can
    use the :sort command, passing a regular expression that matches the
    first column and the following comma:

    :sort/^[^,]*,/
    

    As :sort compares the text starting just after the match of the
    specified pattern on each line, it gives us the desired sorting
    behavior. To compare the values numerically rather than
    lexicographically, use the n flag:

    :sort n/^[^,]*,/
    

    2. The second step involves running through the sorted lines and removing
    all lines but one in every block of consecutive lines with the same
    value in the second column. It is convenient to build our implementation
    upon the :global command, which executes a given Ex command on every
    line matching a certain pattern. For our purposes, a line can be
    deleted if it contains the same value in the second column as the
    following line. This formalization—accompanied with the initial
    assumption that commas cannot occur within column values—gives us
    the following pattern:

    ^[^,]*,\([^,]*\),.*\n[^,]*,\1,.*
    

    If we run the :delete command on every line that satisfies this
    pattern, going from top to bottom over them in sorted order, we will
    have only a single line for every distinct value in the second column:

    :g/^[^,]*,\([^,]*\),.*\n[^,]*,\1,.*/d_
    

    3. Finally, both of the steps can be combined in a single Ex command:

    :sort/^[^,]*,/|g/^[^,]*,\([^,]*\),.*\n[^,]*,\1,.*/d_
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a more jquery/cleaner way to be able to select all
I am looking for a more versatile way to get from a data.frame to
Actually this is a more specific version of a question I posted in past
I am looking for an addin that will allow me to click a button
I am looking at putting a code base that runs several website into version
Basically, I'm not looking for specific differences as you would get with a normal
I am looking for an efficient data structure, that'll allow me to cue events
I don't have a specific question here but I'm more looking for some guidance
I have heard about this problem and now I am looking for more specific
I am looking for more advanced alternatives to xsd.exe . I am just about

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.