I am using
grep --include="*.org" --include="*.texi" -hir -C 8 "Abstract" ./
Now I would simply like to print the first n lines from each file without searching for any pattern.
I am using
grep --include="*.org" --include="*.texi" -hir -C 8 "Abstract" ./
Now I would simply like to print the first n lines from each file without searching for any pattern.
man head
Sure head could work. However I would have to specify a file name. It will not behave same way as when using grep for searching all files.
ok, how about using find
. To start with:
find . \( -name '*.org' -o -name '*.texi' \) | xargs head
grep -l
prints the file names so you can process them with xargs.
grep --include="*.org" --include="*.texi" -lir "Abstract" . | xargs head
But xargs has a problem with space characters in file names.
find -exec
handles spaces well:
find . \( -name '*.org' -o -name '*.texi' \) -exec grep -iq "Abstract" {} \; -exec head {} +
grep -q
suppresses output; the exit status decides on the following.
Probably all find versions cannot chain {} +
queues so there is a need for a slower {} \;
.
Many GNU utilities allow 0-byte terminated lines, so the following is fast and correct:
grep --include="*.org" --include="*.texi" -lirZ "Abstract" . | xargs -0 head
Unfortunately, awk
doesn't have the --recursive
option that grep
provides. But we can resort to bash
's "brace expansion" for sweeping across the directory tree and "extended globbing" ("extended pattern matching"). Try
$ awk -v"LINE=$n" 'FNR <= LINE' {*,*/*,*/*/*}.@(org|texi)
and adapt the */*/*
globs to taste. If you want the filename printed, extend to
$ awk -vLINE=$n 'FNR==1 {print FILENAME} FNR <= LINE' {*,*/*,*/*/*}.@(org|texi)
, and you even check the n lines for your pattern:
$ awk -vLINE=$n 'FNR==1 {print FILENAME} /Abstract/ && FNR <= LINE' {*,*/*,*/*/*}.@(org|texi)
Even without the shopt -s extglob
the folllowing is expanded: echo {*,*/*,*/*/*}.{opt,texi}
A shopt -s nullglob
suppresses non-matching patterns.
Hi
So, passed by ...
grep --include=*.{org,texi}
--- Post updated at 22:03 ---
Maybe I misunderstood something
grep -hrm8 '.' --include=*.{org,texi} ./