Skip to Content
Linux Shell Scripting Cookbook - Third Edition
book

Linux Shell Scripting Cookbook - Third Edition

by Clif Flynt, Sarath Lakshman, Shantanu Tushar
May 2017
Beginner
552 pages
28h 47m
English
Packt Publishing
Content preview from Linux Shell Scripting Cookbook - Third Edition

How to do it...

The wc command supports options to count the number of lines, words, and characters:

  1. Count the number of lines:
      $ wc -l file
  1. To use stdin as input, use this command:
      $ cat file | wc -l
  1. Count the number of words:
      $ wc -w file
      $ cat file | wc -w
  1. Count the number of characters:
      $ wc -c file
      $ cat file | wc -c

 To count the characters in a text string, use this command:

      echo -n 1234 | wc -c
      4

 Here, -n deletes the final newline character.

  1. To print the number of lines, words, and characters, execute wc without any options:
      $ wc file
      1435   15763  112200

Those are the number of lines, words, and characters.

  1. Print the length of the longest line in a file with the -L option:
      $ wc file -L
      205
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Linux Shell Scripting - Second Edition

Mastering Linux Shell Scripting - Second Edition

Mokhtar Ebrahim, Andrew Mallett

Publisher Resources

ISBN: 9781785881985