Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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>

example:

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
1  
Maybe I don't get something? Why grep -i pictures file.txt isn't enough for this? –  hakermania Aug 21 '12 at 20:20
1  
He wants the if in first half just give the iso, if in second half give the full line logic. –  hbdgaf 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
add comment

4 Answers 4

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. –  hbdgaf 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
add comment
$ 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
add comment
#!/usr/bin/env python

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

#from http://www.peterbe.com/plog/uniqifiers-benchmark
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
       result.append(item)
   return result

mylist = []

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

mylist = uniquify(mylist)
for item in mylist:
    print(item)

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

share|improve this answer
add comment
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 scriptName.py WORDTOLOOKFOR For example, if I want to check for a line in the textfile containing the word "ubuntu", and the script is named script.py I write the following

python script.py 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 –  hbdgaf 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 –  hbdgaf Aug 21 '12 at 21:19
1  
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 –  hbdgaf 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
add comment

Your Answer

 
discard

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.