Skip to main content

4.4 - Ordtelling og wc i Bash

Grunnleggende wc-bruk

wc (word count) er et kraftig verktøy for å telle linjer, ord og tegn i filer.

wc fil.txt          # Viser: linjer ord tegn filnavn
wc -l fil.txt # Bare antall linjer
wc -w fil.txt # Bare antall ord
wc -c fil.txt # Bare antall tegn (bytes)
wc -m fil.txt # Bare antall tegn (inkl. Unicode)

Praktiske eksempler

# Telle linjer i flere filer
wc -l *.txt

# Telle ord i alle Python-filer
wc -w *.py

# Telle linjer i et kommandoresultat
ls | wc -l # Antall filer i mappen

# Telle unike ord
cat fil.txt | tr ' ' '\n' | sort | uniq | wc -l

# Telle forekomster av et ord
grep -o "ordet" fil.txt | wc -l

# Telle tomme linjer
grep -c "^$" fil.txt

Kombinere med andre kommandoer

# Finn de 5 lengste filene
wc -l *.txt | sort -nr | head -n 5

# Telle antall filer i undermapper
find . -type f | wc -l

# Telle antall linjer kode (ignorer tomme linjer)
grep -v "^$" fil.py | wc -l

# Telle ord i et kommandoresultat
history | wc -w
Tips
  • Bruk -l, -w, -c sammen for spesifikke kombinasjoner
  • wc teller mellomrom som separate tegn
  • For mer presis telling av Unicode-tegn, bruk -m i stedet for -c