2
1
What is the single command used to identify only the matching count of all lines within files under the /etc
directory that contain the word "HOST"?
I should list only the files with matches and suppress any error messages.
2
1
What is the single command used to identify only the matching count of all lines within files under the /etc
directory that contain the word "HOST"?
I should list only the files with matches and suppress any error messages.
8
To count the matches, listing only the filename(s) and count:
grep -src HOST /etc/*
Example output:
/etc/postfix/postfix-files:1
/etc/security/pam_env.conf:6
/etc/X11/app-defaults/Ddd.3.3.11:1
/etc/X11/app-defaults/Ddd:1
/etc/zsh/zshrc:0
/etc/zsh/zshenv:0
The -c
option supresses normal output and prints a match count for each file.
If you'd like to suppress the files with zero counts:
grep -src HOST /etc/* | grep -v ':0$'
To print the line number (-n
) and file name (-H
) for each matching line for any number of input files:
grep -srnH HOST /etc/*
Example output:
/etc/lynx-cur/lynx.cfg:254:.h2 LYNX_HOST_NAME
/etc/lynx-cur/lynx.cfg:255:# If LYNX_HOST_NAME is defined here or in userdefs.h, it will be
/etc/X11/app-defaults/Ddd.3.3.11:8005: DDD 3.3.11 (@THEHOST@) gets @CAUSE@\n\
/etc/X11/app-defaults/Ddd:8010: DDD 3.3.12 (@THEHOST@) gets @CAUSE@\n\
The option -r
causes grep
to recursively search files in each subdirectory at all levels under the specified directory. The -s
option suppresses error messages.
To suppress matches of binary files, use the -I
option.
See man grep
for more information.
You can also use the -l
flag to suppress files with zero counts, like so: grep -c -l HOST /etc/*
– Achal Dave – 2017-03-17T14:10:08.283
@AchalDave did you try it? – Yola – 2019-01-18T16:31:21.597
@AchalDave: Using -l
prevents the counts from being displayed. – Paused until further notice. – 2019-01-18T19:19:33.773
1
Its not one command, but it is one line
something like
grep -r ',,HOST' . | wc -l
1
grep -c HOST *
… should do it.
1
The question is worded a little odd. First it asks for the amount of lines that match in all the files, then it wants you to list the file names.
To count the matching lines in all files:
grep -R "HOST" /etc 2> /dev/null | wc -l
To list the file names:
grep -Rl "HOST" /etc 2> /dev/null
1man grep, look for count – mykhal – 2010-10-31T23:11:58.400
you should add grep as a tag, it's quite relevant! – barlop – 2010-11-01T01:05:28.860