Bourne Shell Quick Reference Guide:

Bourne Shell Quick Reference Guide
(Publisher: ASP, Inc.)
Author(s): Anatole Olczak
ISBN: 093573922x
Publication Date: 12/01/91

Previous Table of Contents Next


wc

Count Characters, Lines and Words

    $wc [ –clw ] [ files ]

Description:

The wc command counts the characters, lines, or words in the specified files. A total count for all files is kept.

Options:

–c display number of characters (default all options)
–l display number of lines
–w display number of words
files read standard input if no files are specified

EXAMPLE COMMANDS

# Execute multiple commands on one line
     $ pwd ; ls tmp ; echo "Hello world"
# Run the find command in the background
     $ find . -name tmp.out -print &
# Connect the output of who to grep
     $ who | grep fred
# Talk to fred if he is logged on
     $ { who | grep fred ;  } && talk fred
# Send ls output to ls.out
     $ ls > ls.out
# Append output of ls to ls.out
     $ ls >> ls.out
# Send invite.txt to  dick, jane, and spot
     $ mail dick jane spot < invite.txt
# Send the standard error of xsend to stderr.out
     $ xsend file 2>stderr.out
# List file names that begin with z
     $ ls z*
# List two, three, and four character file names
     $ ls ?? ??? ????
# List file names that begin with a, b, or c
     $ ls [a-c]*
# List file names that do not end with .c
     $ ls *[!.c]
# Set NU to the number of users that are logged on
     $ NU=`who | wc -l`
# Set TOTAL to the sum of 4 + 3
     $ TOTAL=`expr 4 + 3`
# Set and export the variable LBIN
     $ LBIN=/usr/lbin; export LBIN
# Unset variable LBIN
     $ unset LBIN
# Set SYS to the hostname if not set, then display its value
     $ echo ${SYS:=`uuname -l`}
# Display an error message if XBIN is not set
     $ : ${XBIN:?}
# Display $HOME set to /home/anatole
     $ echo '$HOME set to' $HOME
# Display the value of $TERM
     $ echo $TERM
# Bring background job 3 into the foreground
     $ fg %3
# Stop the find job
     $ stop %find
# Display the number of positional parameters
     $ echo "There are $# positional parameters"
# Display the value of positional parameter 2
     $ echo $2
# Display all information about current jobs
     $ jobs -l
# Terminate job 5
     $ kill %5
# Increment variable X
     $ X=`expr $X + 1`
# Set variable X to 20 modulo 5
     $ X=`expr 20 % 5`
# Set diagnostic mode
     $ set -x
# Run the dbscript in noexec mode
     $ sh -n dbscript
# Check for new mail every 2 minutes
     $ MAILCHECK=120; export MAILCHECK
# Set the primary prompt string
     $ PS1='Good morning!'; export PS1
# Check if VAR is set to null
     $ [-z "$VAR"] && echo "VAR is set to null"
# Check if VAR is set to ABC
     $ ["$VAR" = ABC ]
# Check if xfile is empty
     $ test ! -s xfile
# Check if tmp is a directory
     $ [ -d tmp ]
# Check if file is readable and writable
     $ test -r file -a -w file
# Display an error message, then beep
     $ echo "Unexpected error!\007"
# Display a message on standard error
     $ echo "This is going to stderr" >&2
# Display a prompt and read  the reply into ANSWER
     $ echo "Enter response: \c"; read ANSWER
# Create a function md that creates a directory and cd's to it
     $ md() { mkdir $1 && cd $1 ; pwd ; }
# Set a trap to ignore signals 2 and 3
     $  trap "" 2 3
# Set X to 1 and make it readonly
     $ X=1 ; readonly X
# Set VAR to 1 and export it
     $ VAR=1 ; export VAR
# Set the positional parameters to A B C
     $ set A B C
# Set the file size creation limit to 1000 blocks
     $ ulimit 1000
# Disable core dumps
     $ ulimit -c 0
# Add group write permission to the file creation mask
     $ umask 013
# Display the first and third fields from file
     $ awk '{print $1, $3}' file
# Display the first seven characters of each line in tfile
     $ cut –c1–7 tfile
# Display the first and third fields from the /etc/passwd file
     $ cut –f1,3 –d":" /etc/passwd
# Display lines in names that begin with A, B, C, or Z
     $ egrep '[A–C,Z]*' names
# Display lines from dict that contain four character words
     $ egrep '....' dict
# Display password entries for users with the Korn shell
     $ grep ":/bin/ksh$' /etc/passwd
# Display number of lines in ufile that contain unix; ignore case
     $ grep –c 'unix' ufile
# Display the lengths of field 1 from file
     $ nawk'{TMP=length($1);print $TMP}' file
# Display the first ten lines of tfile
     $ nawk '{for (i=1; i<10; i++) \
     printf "%s\n", getline}' tfile
# List the contents of the current directory in three columns
     $ ls | paste d" " – – –
# Display file with all occurrences of The substituted with A
     $ sed 's/The/A/g' file
# Display your user name only
     $ id | sed 's/).*//' | sed 's/.*(//'
# Display file with lines that contain unix deleted
     $ sed '/unix/d' file
# Display the first 50 lines of file
     $ sed 75q file
# Sort the /etc/passwd file by group id
     $ sort -t":" -n +3 -4 /etc/passwd
# Translate lower case letters in file to upper case
     $ cat file | tr a-z A-Z
# Display adjacent duplicate lines in file
     $ uniq –d file
# Display the numbers of characters and words in file
     $ wc –l file
# Display the number of .c files in the current directory
     $ ls *.c | wc -l


Previous Table of Contents Next