Saturday, 12 January 2013

Examples Using wc

Here is a file:

Linux > cat file1
Increase productivity across your entire Oracle stack
with access to unique on-line tools and support
communities - all part of your Oracle Premier Support
service.
Use Oracle proactive support to reduce risks and
lower your organisations costs through preventive
maintenance. Benefit from health services, intelligent
fault management and timely, personalized advice,
backed by Oracle support experts.
Linux >

You can count the number of lines, words and characters in the file like this:


Linux > wc file1
      9      56     403 file1
Linux >

If you only need the number of lines, here is what you must do:


Linux > wc -l file1
      9 file1
Linux >

The following example counts the words in the file:


Linux > wc -w file1
     56 file1
Linux >

With wc -c you can count the number of characters in the file:

Linux > wc -c file1
    403 file1
Linux >

You can find the length of the longest line like this:


Linux > wc -L file1
     54 file1
Linux >

Here is another file:

Linux > cat file2
The 2012 threat landscape is one of constant and rapid
change.
Where new website vulnerabilities are tirelessly sought
and exploited.
Where new social engineering techniques can increasingly
circumvent external security measures and infect from
within.
And where hackers now employ multiple attack
methodologies and vectors to penetrate defences and
steal information.
Linux >

You can give more than one filename as follows:

Linux > wc -lc file1 file2
      9     403 file1
     10     369 file2
     19     772 total
Linux >

In the next example, cat is used to see the result without the filename:

Linux > cat file1|wc -L
     54
Linux >

If you give the name of a file which does not exist, wc replies with an appropriate error message:


Linux > wc file3
wc: file3: No such file or directory
Linux >