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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:24:53+00:00 2026-06-10T16:24:53+00:00

I am trying to use a shell script (well a one liner) to find

  • 0

I am trying to use a shell script (well a “one liner”) to find any common lines between around 50 files.
Edit: Note I am looking for a line (lines) that appears in all the files

So far i’ve tried grep grep -v -x -f file1.sp * which just matches that files contents across ALL the other files.

I’ve also tried grep -v -x -f file1.sp file2.sp | grep -v -x -f - file3.sp | grep -v -x -f - file4.sp | grep -v -x -f - file5.sp etc… but I believe that searches using the files to be searched as STD in not the pattern to match on.

Does anyone know how to do this with grep or another tool?

I don’t mind if it takes a while to run, I’ve got to add a few lines of code to around 500 files and wanted to find a common line in each of them for it to insert ‘after’ (they were originally just c&p from one file so hopefully there are some common lines!)

Thanks for your time,

  • 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-10T16:24:55+00:00Added an answer on June 10, 2026 at 4:24 pm

    old, bash answer (O(n); opens 2 * n files)

    From @mjgpy3 answer, you just have to make a for loop and use comm, like this:

    #!/bin/bash
    
    tmp1="/tmp/tmp1$RANDOM"
    tmp2="/tmp/tmp2$RANDOM"
    
    cp "$1" "$tmp1"
    shift
    for file in "$@"
    do
        comm -1 -2 "$tmp1" "$file" > "$tmp2"
        mv "$tmp2" "$tmp1"
    done
    cat "$tmp1"
    rm "$tmp1"
    

    Save in a comm.sh, make it executable, and call

    ./comm.sh *.sp 
    

    assuming all your filenames end with .sp.

    Updated answer, python, opens only each file once

    Looking at the other answers, I wanted to give one that opens once each file without using any temporary file, and supports duplicated lines. Additionally, let’s process the files in parallel.

    Here you go (in python3):

    #!/bin/env python
    import argparse
    import sys
    import multiprocessing
    import os
    
    EOLS = {'native': os.linesep.encode('ascii'), 'unix': b'\n', 'windows': b'\r\n'}
    
    def extract_set(filename):
        with open(filename, 'rb') as f:
            return set(line.rstrip(b'\r\n') for line in f)
    
    def find_common_lines(filenames):
        pool = multiprocessing.Pool()
        line_sets = pool.map(extract_set, filenames)
        return set.intersection(*line_sets)
    
    if __name__ == '__main__':
        # usage info and argument parsing
        parser = argparse.ArgumentParser()
        parser.add_argument("in_files", nargs='+', 
                help="find common lines in these files")
        parser.add_argument('--out', type=argparse.FileType('wb'),
                help="the output file (default stdout)")
        parser.add_argument('--eol-style', choices=EOLS.keys(), default='native',
                help="(default: native)")
        args = parser.parse_args()
    
        # actual stuff
        common_lines = find_common_lines(args.in_files)
    
        # write results to output
        to_print = EOLS[args.eol_style].join(common_lines)
        if args.out is None:
            # find out stdout's encoding, utf-8 if absent
            encoding = sys.stdout.encoding or 'utf-8'
            sys.stdout.write(to_print.decode(encoding))
        else:
            args.out.write(to_print)
    

    Save it into a find_common_lines.py, and call

    python ./find_common_lines.py *.sp
    

    More usage info with the --help option.

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

Sidebar

Related Questions

I'm trying to use a shell script to start a command. I don't care
I'm trying to use the AWK in a unix shell script to substitue an
I'm trying to use python subprocess.call with args to call a shell script. The
I'm trying to use sed in a shell script in Applescript to strip this
I'm trying to use a for loop in a shell script. I am executing
I am trying to use: ls -la *randomString* in my shell script to list
I'm trying to use PHP to create a shell script on Windows. It's part
I have been trying use zenity with standard re-direction in a shell script, something
I'm trying to do use the sed command in a shell script where I
General use case I am trying to implement a basic shell. Description I need

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.