Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have bunch of ISO images in my hdd and I have their whole content listed inside a text file in the following format:

<immage>.iso, <dir structure>/<filename>.<extension>


OS Backups.iso, ­ubuntu-­12.­04-­desktop-­i386.­iso 
OS Backups.iso, xubuntu-12.04-desktop-i386.iso
OS Backups.iso, background/pictures.jpg
Pictures vacation 2011.iso, documents/cost_estimates.xls
Pictures vacation 2011.iso, italy/img1.jpg
Pictures vacation 2011.iso, italy/img2.jpg

Now I want to issue a grep command against that text file to find files that contain "pictures" in their names. The expected result would be (for the previous example):

Pictures vacation 2011.iso
OS Backups.iso, background/pictures.jpg

Any ideas on how to accomplish something like this using grep? Alternatives? Thanks!

share|improve this question
a bash script would do as well, ideas? – Pomario Aug 21 '12 at 20:11
Maybe I don't get something? Why grep -i pictures file.txt isn't enough for this? – hakermania Aug 21 '12 at 20:20
He wants the if in first half just give the iso, if in second half give the full line logic. – RobotHumans Aug 21 '12 at 20:37
Can I ask, how you will use the information afterwards. For example, will you read it later on to import to some system in a specific format? – Luis Alvarado Aug 21 '12 at 20:44
up vote 5 down vote accepted

Here's how to do it with grep, using Perl regular expression syntax -P, and the return-only-matching-part switch -o:

grep -Poi "(.*pictures.*\.iso.*pictures.*|.*pictures.*\.iso|.*pictures.*)"  | sort | uniq

which returns:

OS Backups.iso, background/pictures.jpg
Pictures vacation 2011.iso
  • for each line, grep first checks if it's an .iso files with "pictures" in its name, which also contains a file with "pictures" in its name, e.g.:
    Pictures vacation 2011.iso, italy/pictures5.jpg
  • If it finds, it prints the line and moves on; if not, it checks if this is an .iso files with "pictures" in its name;
  • If so, it prints just the ISO name; if not, it checks if this line contains a file with "pictures" in its name...
share|improve this answer
Good point. I hadn't considered the having pictures on both sides. – RobotHumans Aug 21 '12 at 21:09
although other solutions would do the job, this is the shortest and clearest of all. Thanks! – Pomario Aug 22 '12 at 19:58
$ awk -F ", " 'BEGIN { IGNORECASE=1 } $1 ~ /pictures/ { print $1 ; next } $2 ~ /pictures/ { print }' < context.txt | sort | uniq
OS Backups.iso, background/pictures.jpg
Pictures vacation 2011.iso
share|improve this answer
#!/usr/bin/env python

import re, sys
a_file = sys.argv[1]
a_string = sys.argv[2]

def uniquify(seq, idfun=None): 
   # order preserving
   if idfun is None:
       def idfun(x): return x
   seen = {}
   result = []
   for item in seq:
       marker = idfun(item)
       # in old Python versions:
       # if seen.has_key(marker)
       # but in new ones:
       if marker in seen: continue
       seen[marker] = 1
   return result

mylist = []

with open(a_file, 'r') as items:
    for line in items.readlines():
        if (, line, re.IGNORECASE)):
            temp = line.split(',',1)
            if (, temp[0], re.IGNORECASE)):

mylist = uniquify(mylist)
for item in mylist:

Produces the desired output when run as python index.txt pictures

share|improve this answer
import fileinput
import sys
lookFor = sys.argv[1]
for line in fileinput.input("textfile.txt"):
    if lookFor in line:
            print line

You can run this script with python WORDTOLOOKFOR For example, if I want to check for a line in the textfile containing the word "ubuntu", and the script is named I write the following

python ubuntu

Be sure to rename the textfile in the script.

Edit: This will only print the lines containing it, not store it anywhere, much like what grep can do.

share|improve this answer
your #!/bin/bash line is not correct. i edited in sys.argv in the other python answer – RobotHumans Aug 21 '12 at 21:12
if it were chmod +x-ed and run directly it would fail. it also doesn't address the entire point of not just using grep -i – RobotHumans Aug 21 '12 at 21:19
I'm not talking about this any more. You're stating that the file should be interpreted by bash. It shouldn't. It's python – RobotHumans Aug 21 '12 at 21:38
I agree with aking1012: either remove the misleading shebang line or change it so that the kernel can invoke your python program with a python interpreter. – glenn jackman Aug 22 '12 at 0:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.