text: user9772: So I have around 10,000 FASTA files of Influenza A virus. These files contains sequences of each of the 8 segments of the viral genome and I want to separate these files into different locations based on the content of these FASTA files.
  In each FASTA file, first line has the segment number.
  Eg.
  >KM368312.1 Influenza A virus (A/swine/Shandong/01/2009(H1N1)) segment 3 polymerase PA (PA) and PA-X protein (PA-X) genes, complete cds
  
  Okay imagine you have 10,000 text files in one folder and there are 8 types of these text files based on the content of them. Eg. file1.txt has segment 1 written in it, file2.txt has segment 2 written in it upto segment 8. So I want to group the files containing segment 1 together in one folder, segment 2 in other folder together and so on.
  
  user29: The general idea is:
  
  iterate over all files
  
  read the first line of each file, extract the segment bit
  move the file based on that
  
  
  
  In Python, this can be done in many ways. Here’s one way:
  import os
  import re
  
  
  def find_segment(id):
      return re.search(r'segment (\d)', id).group(1)
  
  
  for segment in range(8):
      os.mkdir(f'segment-{segment + 1}')
  
  for entry in os.scandir():
      if not entry.is_file():
          continue
  
      with open(entry.path, 'r') as file:
          segment = find_segment(next(file))
      os.rename(entry.path, f'segment-{segment}/{entry.path}')
  
  Note the absence of error handling — this code assumes that every operation succeeds; empty files, or files without “segment X” in their FASTA ID, will trigger an error, as will usual IO failures (missing permissions, etc.).
  
  But many people wouldn’t use Python to solve this but a shell scripting language. For example, here’s something equivalent in Bash (untested):
  mkdir segment-{1..8}
  
  for file in *.fa *.fasta; do
      segment=$(sed 's/.*segment \([[:digit:]]\).*/\1/; q' "$file")
      if [[ "$segment" != '' ]]; then
          mv "$file" "segment-$segment"
      done
  done
  
  
  user-1: That's quite easy with cmd:
  @echo off
  for /l %%i in (1,1,8) do (
    for /f "delims=" %%f in ('findstr  /mc:" segment %%i " *.txt') do (
      ECHO move "%%f" "segment %%i\"
    )
  )
  
  findstr /mc:"string" *.txt searches each matching file for the string and outputs the filename, if the string was found. The for /f loop around catches those filenames, so you are able to move them. The for /l loop just iterates from 1 to 8.
  See for /? and findstr /? for more information about those commands.
  You probably want to adapt the filemask *.txt to your needs.
  Note: I "disarmed" the move command for troubleshooting. Remove the ECHO when you are sure it does what you want.
  
original_text: 9772: <p>So I have around 10,000 FASTA files of Influenza A virus. These files contains sequences of each of the 8 segments of the viral genome and I want to separate these files into different locations based on the content of these FASTA files.
  In each FASTA file, first line has the segment number.
  Eg.</p>
  <pre><code>&gt;KM368312.1 Influenza A virus (A/swine/Shandong/01/2009(H1N1)) segment 3 polymerase PA (PA) and PA-X protein (PA-X) genes, complete cds
  </code></pre>
  <p>Okay imagine you have 10,000 text files in one folder and there are 8 types of these text files based on the content of them. Eg. file1.txt has segment 1 written in it, file2.txt has segment 2 written in it upto segment 8. So I want to group the files containing segment 1 together in one folder, segment 2 in other folder together and so on.</p>
   29: <p>The general idea is:</p>
  <ol>
  <li>iterate over all files
  <ol>
  <li>read the first line of each file, extract the <code>segment</code> bit</li>
  <li>move the file based on that</li>
  </ol>
  </li>
  </ol>
  <p>In Python, this can be done in many ways. Here’s one way:</p>
  <pre class="lang-py prettyprint-override"><code>import os
  import re
  
  
  def find_segment(id):
      return re.search(r'segment (\d)', id).group(1)
  
  
  for segment in range(8):
      os.mkdir(f'segment-{segment + 1}')
  
  for entry in os.scandir():
      if not entry.is_file():
          continue
  
      with open(entry.path, 'r') as file:
          segment = find_segment(next(file))
      os.rename(entry.path, f'segment-{segment}/{entry.path}')
  </code></pre>
  <p>Note the absence of error handling — this code assumes that every operation succeeds; empty files, or files without “<code>segment X</code>” in their FASTA ID, will trigger an error, as will usual IO failures (missing permissions, etc.).</p>
  <hr />
  <p>But many people wouldn’t use Python to solve this but a shell scripting language. For example, here’s something equivalent in Bash (untested):</p>
  <pre class="lang-sh prettyprint-override"><code>mkdir segment-{1..8}
  
  for file in *.fa *.fasta; do
      segment=$(sed 's/.*segment \([[:digit:]]\).*/\1/; q' &quot;$file&quot;)
      if [[ &quot;$segment&quot; != '' ]]; then
          mv &quot;$file&quot; &quot;segment-$segment&quot;
      done
  done
  </code></pre>
   -1: <p>That's quite easy with <code>cmd</code>:</p>
  <pre><code>@echo off
  for /l %%i in (1,1,8) do (
    for /f &quot;delims=&quot; %%f in ('findstr  /mc:&quot; segment %%i &quot; *.txt') do (
      ECHO move &quot;%%f&quot; &quot;segment %%i\&quot;
    )
  )
  </code></pre>
  <p><code>findstr /mc:&quot;string&quot; *.txt</code> searches each matching file for the string and outputs the filename, if the string was found. The <code>for /f</code> loop around catches those filenames, so you are able to move them. The <code>for /l</code> loop just iterates from 1 to 8.</p>
  <p>See <code>for /?</code> and <code>findstr /?</code> for more information about those commands.</p>
  <p>You probably want to adapt the filemask <code>*.txt</code> to your needs.</p>
  <p>Note: I &quot;disarmed&quot; the <code>move</code> command for troubleshooting. Remove the <code>ECHO</code> when you are sure it does what you want.</p>
  