chhotii: (caffeine)
chhotii ([personal profile] chhotii) wrote2013-12-10 02:39 pm
Entry tags:

UNIX geek challenge

UNIX Geek Of The Day bragging rights to whoever can propose the best solution to this problem: Identify all text files in the current directory which contain the string "jpg" on two consecutive lines. Using the typical command-line tools such as grep, not by writing a program. Extra points if the location (line number) of these consecutive lines of interest are output.

Posted via LiveJournal app for Android.

ceo: (blueshirt)

[personal profile] ceo 2013-12-10 08:25 pm (UTC)(link)
With GNU grep, the -P option enables Perl regular expressions, which handle newlines better than plain ol'regexes do.

grep -Pn '.*jpg.*\n.*jpg.*' * appears to work.

[identity profile] chhotii.livejournal.com 2013-12-11 12:25 am (UTC)(link)
Whoa, clever! Thanks!

[identity profile] achinhibitor.livejournal.com 2013-12-11 02:16 pm (UTC)(link)
[edited for correctness]

Given that grep is defined to filter lines, and lines by definition do not contain regexps newlines, is there any guarantee that this works in general?

Also, what does it do if there are three jpg lines in sequence? There are *two* matches that should be reported.
Edited 2013-12-12 20:10 (UTC)