6 WC-kommandoeksempler til at tælle antal linjer, ord, tegn i Linux


Wc (ordtælling) -kommandoen i Unix/Linux-operativsystemer bruges til at finde ud af antallet af nye linjetællinger, antal ord, byte og tegn tæller i en fil, der er angivet af filargumenterne. Syntaksen for wc-kommandoen som vist nedenfor.

# wc [options] filenames

Følgende er de muligheder og brug, der leveres af kommandoen.

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.

Så lad os se, hvordan vi kan bruge kommandoen 'wc' med deres få tilgængelige argumenter og eksempler i denne artikel. Vi har brugt filen 'tecmint.txt' til at teste kommandoerne. Lad os finde ud af filens output ved hjælp af cat-kommandoen som vist nedenfor.

 cat tecmint.txt

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

1. Et grundlæggende eksempel på WC-kommando

Kommandoen 'wc' uden at videregive nogen parameter viser et grundlæggende resultat af "tecmint.txt" -filen. De tre nedenfor viste tal er 12 (antal linjer), 16 (antal ord) og 112 (antal bytes) i filen.

 wc tecmint.txt

12  16 112 tecmint.txt

2. Tæl antal linjer

For at tælle antallet af nye linjer i en fil skal du bruge indstillingen '-l', der udskriver antallet af linjer fra en given fil. Sig, den følgende kommando viser antallet af nye linjer i en fil. I output er det første arkiveret, der er tildelt som antal og andet felt, navnet på filen.

 wc -l tecmint.txt

12 tecmint.txt

3. Vis antal ord

Brug af '-w' argument med 'wc' kommando udskriver antallet af ord i en fil. Skriv følgende kommando for at tælle ordene i en fil.

 wc -w tecmint.txt

16 tecmint.txt

4. Tæl antal byte og tegn

Når du bruger indstillingerne '-c' og '-m' med 'wc', udskriver det samlede antal henholdsvis bytes og tegn i en fil.

 wc -c tecmint.txt

112 tecmint.txt
 wc -m tecmint.txt

112 tecmint.txt

5. Vis længde på længste linje

Kommandoen 'wc' tillader et argument '-L', den kan bruges til at udskrive længden af den længste (antal tegn) linje i en fil. Så vi har den længste tegnlinje ('Scientific Linux') i en fil.

 wc -L tecmint.txt

16 tecmint.txt

6. Kontroller flere toiletindstillinger

For mere information og hjælp til wc-kommandoen skal du blot køre 'wc –help' eller 'man wc' fra kommandolinjen.

 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'