Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 742  / 2 Years ago, sun, january 16, 2022, 10:29:55

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!


More From » bash

 Answers
4

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...


[#36027] Sunday, January 16, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ionodest

Total Points: 252
Total Questions: 122
Total Answers: 100

Location: Liechtenstein
Member since Tue, Apr 27, 2021
3 Years ago
ionodest questions
Sat, Jan 1, 22, 06:31, 2 Years ago
Wed, May 12, 21, 03:21, 3 Years ago
Tue, Feb 22, 22, 09:46, 2 Years ago
Thu, Jun 30, 22, 12:12, 2 Years ago
;