UNIX geek challenge
Dec. 10th, 2013 02:39 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
no subject
Date: 2013-12-10 08:25 pm (UTC)grep -Pn '.*jpg.*\n.*jpg.*' * appears to work.
no subject
Date: 2013-12-11 12:25 am (UTC)no subject
Date: 2013-12-11 02:16 pm (UTC)Given that grep is defined to filter lines, and lines by definition do not contain
regexpsnewlines, 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.
no subject
Date: 2013-12-11 01:21 am (UTC)for I in *; do test -f "$I" && awk ' BEGIN { last = 0 ; status = 1 } { if (/jpg/) { if (last == 1) { print $0, "line number ", NR ; status = 0 } else last = 1 } else last=0 } END { if (status == 1) exit 1 }' < "$I" && echo " file $I matches"; done
Don't use this. There's *got* to be a horrible bug in there somewhere. Unless you hate your workplace. In that case, make sure it gets into some important automated build process.
no subject
Date: 2013-12-11 03:18 am (UTC)no subject
Date: 2013-12-11 10:22 am (UTC)no subject
Date: 2013-12-11 02:11 pm (UTC)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