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.

[identity profile] achinhibitor.livejournal.com 2013-12-11 02:11 pm (UTC)(link)
I can do this without using anything that is programmable (like sed or awk):

for FILE in *
do
    paste <(head --lines=-1 $FILE) \
         <(while read X; do echo Throat-warbler-mangrove ; done <$FILE) \
         <(tail --lines=+2 $FILE) |
    grep -n $'jpg.*\tThroat-warbler-mangrove\t.*jpg' &&
    echo $FILE
done
Edited 2013-12-11 14:12 (UTC)