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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:07:13+00:00 2026-06-18T02:07:13+00:00

I have to grep from a file (5MB) containing specific strings the same strings

  • 0

I have to grep from a file (5MB) containing specific strings the same strings (and other information) from a big file (27GB).
To speed up the analysis I split the 27GB file into 1GB files and then applied the following script (with the help of some people here). However it is not very efficient (to produce a 180KB file it takes 30 hours!).

Here’s the script. Is there a more appropriate tool than grep? Or a more efficient way to use grep?

#!/bin/bash

NR_CPUS=4
count=0


for z in `echo {a..z}` ;
do
 for x in `echo {a..z}` ;
 do
  for y in `echo {a..z}` ;
  do
   for ids in $(cat input.sam|awk '{print $1}');  
   do 
    grep $ids sample_"$z""$x""$y"|awk '{print $1" "$10" "$11}' >> output.txt &
    let count+=1
                                [[ $((count%NR_CPUS)) -eq 0 ]] && wait
   done
  done #&
  • 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-18T02:07:14+00:00Added an answer on June 18, 2026 at 2:07 am

    A few things you can try:

    1) You are reading input.sam multiple times. It only needs to be read once before your first loop starts. Save the ids to a temporary file which will be read by grep.

    2) Prefix your grep command with LC_ALL=C to use the C locale instead of UTF-8. This will speed up grep.

    3) Use fgrep because you’re searching for a fixed string, not a regular expression.

    4) Use -f to make grep read patterns from a file, rather than using a loop.

    5) Don’t write to the output file from multiple processes as you may end up with lines interleaving and a corrupt file.

    After making those changes, this is what your script would become:

    awk '{print $1}' input.sam > idsFile.txt
    for z in {a..z}
    do
     for x in {a..z}
     do
      for y in {a..z}
      do
        LC_ALL=C fgrep -f idsFile.txt sample_"$z""$x""$y" | awk '{print $1,$10,$11}'
      done >> output.txt
    

    Also, check out GNU Parallel which is designed to help you run jobs in parallel.

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

Sidebar

Related Questions

I have been using this so far system 'strings binary-file.dmp | grep search_string' Is
I have a simple grep command that returns lines that match from a file.
windows xp, gnu grep from http://gnuwin32.sourceforge.net/packages/grep.htm I have file with line breaks: test 123
I have output from grep I'm trying to clean up that looks like: <words>Http://www.path.com/words</words>
I am using GNU grep on Windows 7 command prompt. I have a file
I have a text file called db.txt. Some sample lines from the file goes
I have the following code snippet from a HTML file: <div id=rwImages_hidden style=display:none;> <img
I have a pcap file as input and I want to get particular strings
I'm writing a shell script that outputs all untranslated strings from gettext .po file,
I have a file, say input, containing patterns like below: quantum_mech_.* astrophysics_.* geology_.* economy_*

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.