6 primerov ukazov WC za štetje števila vrstic, besed, znakov v Linuxu


Ukaz wc (word count) v operacijskih sistemih Unix/Linux se uporablja za ugotavljanje števila novih vrstic, števila besed, bajtov in števila znakov v datotekah, določenih z argumenti datotek. Sintaksa ukaza wc, kot je prikazano spodaj.

# wc [options] filenames

Sledijo možnosti in uporaba, ki jih nudi ukaz.

wc -l : Prints the number of lines in a file.
wc -w : prints the number of words in a file.
wc -c : Displays the count of bytes in a file.
wc -m : prints the count of characters from a file.
wc -L : prints only the length of the longest line in a file.

Poglejmo torej, kako lahko uporabimo ukaz ‘wc’ z nekaj razpoložljivimi argumenti in primeri v tem članku. Za testiranje ukazov smo uporabili datoteko 'tecmint.txt'. Ugotovimo izhod datoteke z ukazom cat, kot je prikazano spodaj.

 cat tecmint.txt

Red Hat
CentOS
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Pearl Linux
Slackware
Mandriva

1. Osnovni primer ukaza WC

Ukaz 'wc' brez podajanja katerega koli parametra bo prikazal osnovni rezultat datoteke 'tecmint.txt'. Spodaj prikazane tri številke so 12 (število vrstic), 16 (število besed) in 112 (število bajtov) datoteke.

 wc tecmint.txt

12  16 112 tecmint.txt

2. Štetje števila vrstic

Če želite prešteti število novih vrstic v datoteki, uporabite možnost -l, ki natisne število vrstic iz dane datoteke. Recimo, naslednji ukaz bo prikazal število novih vrstic v datoteki. V izhodu je prva datoteka, dodeljena štetju, drugo polje pa ime datoteke.

 wc -l tecmint.txt

12 tecmint.txt

3. Prikaži število besed

Uporaba argumenta -w z ukazom 'wc' natisne število besed v datoteki. Vnesite naslednji ukaz za štetje besed v datoteki.

 wc -w tecmint.txt

16 tecmint.txt

4. Štetje števila bajtov in znakov

Pri uporabi možnosti '-c' in '-m' z ukazom 'wc' bo natisnilo skupno število bajtov oziroma znakov v datoteki.

 wc -c tecmint.txt

112 tecmint.txt
 wc -m tecmint.txt

112 tecmint.txt

5. Prikaz dolžine najdaljše črte

Ukaz 'wc' dovoli argument '-L', z njim lahko natisnete dolžino najdaljše (število znakov) vrstice v datoteki. V datoteki imamo torej najdaljšo vrstico znakov ('Scientific Linux').

 wc -L tecmint.txt

16 tecmint.txt

6. Preverite več možnosti WC-ja

Za več informacij in pomoč pri ukazu wc preprosto zaženite 'wc –help' ali 'man wc' iz ukazne vrstice.

 wc --help

Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help			display this help and exit
      --version			output version information and exit

Report wc bugs to [email 
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'wc invocation'