From UNIX shell, how to find all files containing a specific string, then print the 4th line of each file? -
i want find files within current directory contain given string, print 4th line of each file.
grep --null -l "$yourstring" * | # list files containing string xargs -0 sed -n '4p;q' # print fourth line of said files.
different editions of grep have different incantations of --null
, it's there in form. read manpage details.
update: believe 1 of null file list incantations of grep reasonable solution cover vast majority of real-world use cases, entirely portable, if version of grep
not support null output not safe use xargs
, must resort find
.
find . -maxdepth 1 -type f -exec grep -q "$yourstring" {} \; -exec sed -n '4p;q' {} +
because find
arguments can used predicates, -exec grep -q…
part filters files fed sed
down contain required string.
Comments
Post a Comment