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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:19:25+00:00 2026-06-13T12:19:25+00:00

I have a csv (comma separated file). I would like to know how to

  • 0

I have a csv (comma separated file). I would like to know how to search for a pattern where the 7th and 8th field are the same using only grep (no using cut). I have tried something like this:

grep -E '[^,]*,{6,6}' input.csv | grep '\(.*\)\(,\)\(\1$\)' | less

Unfortunately, this does not print anything. How could I get the output I need?

  • 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-13T12:19:26+00:00Added an answer on June 13, 2026 at 12:19 pm

    Assuming there’s nothing awkward like fields with commas in them (because if there are such fields in the first 8 fields, you can’t process the files without a full CSV-cognizant tool), and that there is a 9th field (so the 7th and 8th fields are both followed by a comma) then:

    grep '^\([^,]*,\)\{6\}\([^,]*,\)\2' file.csv
    

    The first bit says 6 sequences of zero-or-more non-commas, each followed by a comma. Then there’s the 7th (possibly empty) field with its trailing comma; that’s followed by the same-thing-again (the \2).

    $ cat file.csv
    a,b,c,d,e,f,g,g,i
    a,b,c,d,e,f,g,h,i
    a,b,c,d,e,f,hhh,hhh,i
    ,b,c,d,e,f,hhh,hhh,i
    ,,c,d,e,f,hhh,hhh,i
    ,,,d,e,f,hhh,hhh,i
    ,,,,e,f,hhh,hhh,i
    ,,,,,f,hhh,hhh,i
    ,,,,,,hhh,hhh,i
    ,,,,,,hhh,hhh,
    $ grep '^\([^,]*,\)\{6\}\([^,]*,\)\2' file.csv
    a,b,c,d,e,f,g,g,i
    a,b,c,d,e,f,hhh,hhh,i
    ,b,c,d,e,f,hhh,hhh,i
    ,,c,d,e,f,hhh,hhh,i
    ,,,d,e,f,hhh,hhh,i
    ,,,,e,f,hhh,hhh,i
    ,,,,,f,hhh,hhh,i
    ,,,,,,hhh,hhh,i
    ,,,,,,hhh,hhh,
    $
    

    Note that the g,h,i line does not appear in the output (and it shouldn’t); the rest should and do appear.

    All of this is done using POSIX Basic Regular Expressions or BREs. If you use egrep or grep -E, you have Extended Regular Expressions or EREs at your disposal and you can forego all the backslashes except the \2; you could also deal with a file that has some lines with 8 fields and other lines with 9 or more, but that isn’t a regular CSV file. The BRE version can also be modified to work with a CSV file that has precisely 8 columns:

    grep '^\([^,]*,\)\{6\}\([^,]*\),\2$' file.csv
    

    Part of the art of using regular expressions is having a flexible mindset about different ways to achieve a given result; there is often more than one way to do it.

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

Sidebar

Related Questions

Let's say we have a comma separated file (csv) like this: name of movie,starring,director,release
I have a comma separated CSV file looks like: customer1,customer2,,customer4, ,customer2,,customer4, custome1,,customer3,, I want
I have a comma separated CSV file contains NASDAQ symbols . I use Scanner
I have a comma-separated value file that looks like this when I open it
I have idsfile.csv which is a comma separated file of ids (with no new
I have a csv File which is of course comma separated and want to
I have a comma separated file named foo.csv containing the following data: scale, serial,
I have a CSV (comma separated values) file that contains student information. The column
I would like to save my CSV file as unicode by using Excel. I
I have a csv file that has 2 columns separated by a comma -

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.