The wc command can be used in combination with other commands through piping. The number of words is shown in the first column: 513 /home/linuxize/Documents/file.txt The following example counts the number of words in the ~/Documents/file.txt file: wc -w /etc/passwd To count only the number of words in a text file use wc -w followed by the file name. The first column is the number of lines and the second one is the name of the file: 44 /etc/passwd For example, to count the number of lines in the /etc/passwd The wc command is mostly used with the -l option to count only the number of lines in a text file. The output will show the number of lines for all files in the /etc directory whose names start with “host”: 4 /etc/nf For example, you can search for files using the find commandĪnd provide those files as an input to wc: find /etc -name 'host*' -printf0 | wc -l -files0-from=. If F is - then read names from standard input. The -files0-from=F option allows wc to read input from the files specified by NUL-terminated names in file F. wc -lL /proc/cpuinfo 448 792 /proc/cpuinfo Here is another example that will print the number of lines and the length of the longest line. When using multiple options counts are printed in the following order: newline, words, characters, bytes, maximum line length.įor example, to display only the number of words you would use: wc -w /proc/cpuinfo 3632 /proc/cpuinfo -L, -max-line-length - Print the length of the longest line.-c, -bytes - Print the number of bytes.-m, -chars - Print the number of characters.-w, -words - Print the number of words.-l, -lines - Print the number of lines.The options below allow you to select which counts are printed. The command will give you information about each file and a line including total statistics: 448 3632 22226 /proc/cpuinfo To display information about more than one file, pass the filenames, as arguments, separated by space: wc /proc/cpuinfo /proc/meminfo When using the standard input, the file name is not shown: wc < /proc/cpuinfo 448 3632 22226 The output will look something like the following: 448 3632 22226 /proc/cpuinfo When using the standard input the fourth column (filename) is not displayed.įor example, the following command will display information about the virtual file /proc/cpuinfo: wc /proc/cpuinfo In it’s simplest form when used without any options, the wc command will print four columns, the number of lines, words, byte counts and the name of the file for each file passed as an argument. A word is a string of characters delimited by a space, tab, or newline. If no FILE is specified, or when FILE is -, wc will read the standard input. This is a pretty lazy effort, and you may need to adjust it for the format of your particular log, but I hope I at least helped you get started.The wc command can accept zero or more input FILE names. Running the same script on /var/log/syslog with the search string 'kernel' produced this output: 3/2 5:54: 11 Mar 2 05:35:01 home-pc-02 CRON: (root) CMD (command -v debian-sa1 > /dev/null & debian-sa1 1 1) Mar 2 05:25:01 home-pc-02 CRON: (root) CMD (command -v debian-sa1 > /dev/null & debian-sa1 1 1) Mar 2 05:21:46 home-pc-02 anacron: Normal exit (1 job run) Mar 2 05:21:46 home-pc-02 anacron: Job `cron.daily' terminated Mar 2 05:21:28 home-pc-02 rsyslogd: rsyslogd was HUPed Which coincides with the output from head: Mar 2 05:21:28 home-pc-02 rsyslogd: rsyslogd was HUPed Running it on /var/log/syslog with an empty search string gives the following output: 3/2 5:21: 4 Time.month, time.day, time.hour, time.minute, Minute_timestamp = datetime.strptime(entry, '%b %d %H:%M')
0 Comments
Leave a Reply. |