6 WC-kommandoeksempler for å telle antall linjer, ord, tegn i Linux
Kommandoen wc (ordtelling) i Unix/Linux-operativsystemer brukes til å finne ut antall nye linjer, antall ord, antall byte og tegn i en fil spesifisert av filargumentene. Syntaksen til wc-kommandoen som vist nedenfor.
# wc [options] filenames
Følgende er alternativene og bruken gitt av 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å la oss se hvordan vi kan bruke 'wc'-kommandoen med de få tilgjengelige argumentene og eksemplene i denne artikkelen. Vi har brukt 'tecmint.txt'-filen for å teste kommandoene. La oss finne ut utdataene til filen ved å bruke 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 grunnleggende eksempel på WC-kommando
'wc'-kommandoen uten å sende noen parameter vil vise et grunnleggende resultat av filen tecmint.txt. De tre tallene vist nedenfor er 12 (antall linjer), 16 (antall ord) og 112 (antall byte) av filen.
wc tecmint.txt 12 16 112 tecmint.txt
2. Tell antall linjer
For å telle antall nye linjer i en fil, bruk alternativet '-l', som skriver ut antall linjer fra en gitt fil. Si, følgende kommando vil vise antallet nye linjer i en fil. I utdataene er det første arkivet tildelt som teller og det andre feltet er navnet på filen.
wc -l tecmint.txt 12 tecmint.txt
3. Vis antall ord
Ved å bruke '-w'-argumentet med 'wc'-kommandoen skrives ut antall ord i en fil. Skriv inn følgende kommando for å telle ordene i en fil.
wc -w tecmint.txt 16 tecmint.txt
4. Tell antall byte og tegn
Når du bruker alternativene '-c' og '-m' med 'wc'-kommandoen vil det skrives ut det totale antallet byte og tegn i en fil.
wc -c tecmint.txt 112 tecmint.txt
wc -m tecmint.txt 112 tecmint.txt
5. Vis lengden på den lengste linjen
'wc'-kommandoen tillater et argument '-L', den kan brukes til å skrive ut lengden på den lengste (antall tegn) linjen i en fil. Så vi har den lengste tegnlinjen ('Scientific Linux') i en fil.
wc -L tecmint.txt 16 tecmint.txt
6. Kryss av for flere WC-alternativer
For mer informasjon og hjelp om wc-kommandoen, kjør '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'