title: How to Return List of Packages from Pacman/Yaourt Search
body: <p>----EDIT----<br>
  Changed the name of the script from <code>pacsearch</code> to <code>pacdot</code>.<br>
  Apparently <code>yaourt -Ssaq</code> does this, so this script isn't as necessary as I thought. Although, I still find using pacdot -w to open the results in a text document helpful.<br>
  ----/EDIT----  </p>
  
  <p>This isn't a question, but I thought someone else might find this useful. Someone may end up on stackoverflow trying to find a solution like this.  </p>
  
  <p>On Arch Linux, I keep finding myself searching with pacman or yaourt and wishing I could get just the package names, not all of the extra stuff. For example, I'd love to be able to run <code>yaourt -Sa $(yaourt -Ssa package)</code>. Oddly enough, pacman and yaourt don't seem have this option (not that I can tell, at least), so I wrote a python script to do it. Copy it if you'd like. You can name it what you want, but I'll refer to it as <code>pacdot.py</code>.  </p>
  
  <p><code>pacdot.py package</code> will be like <code>yaourt -Ssa package</code> but only list the package names.  </p>
  
  <p>I added a few extra options:  </p>
  
  <ul>
  <li><code>pacdot.py -o package</code> will only list results from the official Arch repositories, not the AUR.</li>
  <li><p><code>pacdot.py -i package</code> will install all the found packages. If you've ever thought about running something like <code>yaourt -Sa $(yaourt -Ssa package)</code>, that's what this command does.</p></li>
  <li><p><code>pacdot.py -w package</code> will:</p>
  
  <blockquote>
    <ol>
    <li>Create a file called 'the-package-you-searched.txt',</li>
    <li>Write an example command that would install the found packages,<br>
     (yaourt -Sa all-of-the-results),</li>
    <li>Write each result on a new line, and</li>
    <li>Open the file for you (with your default text editor).  </li>
    </ol>
  </blockquote></li>
  </ul>
  
  <p>Here's the code:</p>
  
  <pre><code>#!/bin/python3
  import argparse
  import re
  from subprocess import Popen, PIPE, call
  from collections import deque
  
  
  desc = ''.join(('Search the official Arch and AUR databases ',
                  'and return package names only. ',
                  'e.g.: `pacdot.py arch` will return "arch", ',
                  'whereas `$ yaourt -Ssa arch` will return ',
                  '"community/arch 1.3.5-10',
                  '    A modern and remarkable revision control system."'
                  ))
  parser = argparse.ArgumentParser(description=desc)
  parser.add_argument('package',
                      help='Package to search with pacman')
  parser.add_argument('-o', '--official', action='store_true',
                      help='Search official repositories only, not the AUR')
  parser.add_argument('-i', '--install', action='store_true',
                      help='Install found packages')
  parser.add_argument('-w', '--write', action='store_true',
                      help='Write to file')
  
  #Set args strings.
  args = parser.parse_args()
  pkg = args.package
  official_only = args.official
  install = args.install
  write = args.write
  
  # Do yaourt search.
  package_search = Popen(['yaourt', '-Ssa', '%s' % pkg], stdout=PIPE).communicate()
  # Put each found package into a list.
  package_titles_descs = str(package_search[0]).split('\\n')
  # Strip off the packages descriptions.
  package_titles = [package_titles_descs[i]
                    for i in range(0, len(package_titles_descs), 2)]
  # Remove empty item in list.
  del(package_titles[-1])
  
  # Make a separate list of the non-aur packages.
  package_titles_official = deque(package_titles)
  [package_titles_official.remove(p)
      for p in package_titles if p.startswith('aur')]
  
  # Strip off extra stuff like repository names and version numbers.
  packages_all = [re.sub('([^/]+)/([^\s]+) (.*)',
                         r'\2', str(p))
                  for p in package_titles]
  packages_official = [re.sub('([^/]+)/([^\s]+) (.*)',
                              r'\2', str(p))
                       for p in package_titles_official]
  
  # Mark the aur packages.
  #     (Not needed, just in case you want to modify this script.)
  #packages_aur = packages_all[len(packages_official):]
  
  # Set target packages to 'all' or 'official repos only'
  #     based on argparse arguments.
  if official_only:
      packages = packages_official
  else:
      packages = packages_all
  
  # Print the good stuff.
  for p in packages:
      print(p)
  
  if write:
      # Write results to file.
      filename = ''.join((pkg, '.txt'))
      with open(filename, 'a') as f:
          print(''.join(('Yaourt search for "', pkg, '"\n')), file=f)
          print('To install:', file=f)
          packages_string = ' '.join(packages)
          print(' '.join(('yaourt -Sa', packages_string)), file=f)
          print('\nPackage list:', file=f)
          for p in packages:
              print(p, file=f)
      # Open file.
      call(('xdg-open', filename))
  
  if install:
      # Install packages with yaourt.
      for p in packages:
          print(''.join(('\n\033[1;32m==&gt; ', '\033[1;37m', p,
                         '\033[0m')))
          Popen(['yaourt', '-Sa', '%s' % p]).communicate()
  </code></pre>