File Text Manipulation
File & Text Manipulation — Quick Reference
Section titled “File & Text Manipulation — Quick Reference”cat — Concatenate / Print Files
Section titled “cat — Concatenate / Print Files”cat file.txt # print filecat file1.txt file2.txt # concatenate two filescat -n file.txt # print with line numberscat file1.txt file2.txt > combined.txt # merge into new fileless — Paginated File Viewer
Section titled “less — Paginated File Viewer”Better than cat for large files — doesn’t load everything at once.
less file.txtless +F file.txt # follow mode (like tail -f)| Key | Action |
|---|---|
Space / f | Page down |
b | Page up |
g | Go to start |
G | Go to end |
/pattern | Search forward |
n / N | Next / previous match |
q | Quit |
head / tail — View Start or End of File
Section titled “head / tail — View Start or End of File”head file.txt # first 10 lines (default)head -n 20 file.txt # first 20 linestail file.txt # last 10 linestail -n 50 file.txt # last 50 linestail -f app.log # follow — stream new lines livetail -f app.log | grep ERROR # follow and filtersed — Stream Editor (Find & Replace)
Section titled “sed — Stream Editor (Find & Replace)”# Replace first occurrence per linesed 's/foo/bar/' file.txt
# Replace all occurrences (global flag)sed 's/foo/bar/g' file.txt
# Edit file in placesed -i 's/foo/bar/g' file.txt
# Delete lines matching a patternsed '/^#/d' file.txt # delete comment lines
# Print only matching linessed -n '/error/p' file.txt
# Replace on a specific line numbersed '5s/foo/bar/' file.txt
# Delete blank linessed '/^$/d' file.txtawk — Column / Field Processing
Section titled “awk — Column / Field Processing”# Print second column (whitespace-delimited)awk '{print $2}' file.txt
# Custom delimiter (e.g. CSV)awk -F',' '{print $1, $3}' file.csv
# Print lines where column 3 > 100awk '$3 > 100' file.txt
# Sum a columnawk '{sum += $2} END {print sum}' file.txt
# Print line numbers with contentawk '{print NR": "$0}' file.txt
# Print lines between two patternsawk '/START/,/END/' file.txtcut — Extract Columns
Section titled “cut — Extract Columns”# Extract characters 1-5cut -c1-5 file.txt
# Extract field 2 (tab-delimited by default)cut -f2 file.txt
# Custom delimitercut -d',' -f1,3 file.csv # fields 1 and 3 from CSV
# Extract from ls outputls -l | cut -d' ' -f1 # permissions columnsort — Sort Lines
Section titled “sort — Sort Lines”sort file.txt # alphabeticalsort -r file.txt # reversesort -n file.txt # numeric sortsort -u file.txt # unique lines onlysort -k2 file.txt # sort by 2nd columnsort -t',' -k2 -n file.csv # CSV, sort numerically by col 2uniq — Deduplicate Lines
Section titled “uniq — Deduplicate Lines”Works on sorted input.
sort file.txt | uniq # remove duplicate linessort file.txt | uniq -c # prefix each line with countsort file.txt | uniq -d # print only duplicatessort file.txt | uniq -u # print only unique lineswc — Count Lines, Words, Bytes
Section titled “wc — Count Lines, Words, Bytes”wc file.txt # lines, words, byteswc -l file.txt # line count onlywc -w file.txt # word count onlywc -c file.txt # byte count
# Count files in directoryls | wc -ldiff — Compare Files
Section titled “diff — Compare Files”diff file1.txt file2.txt # basic diffdiff -u file1.txt file2.txt # unified format (like git diff)diff -r dir1/ dir2/ # recursive directory diffdiff --color file1.txt file2.txt # colored outputtr — Translate / Delete Characters
Section titled “tr — Translate / Delete Characters”# Lowercase to uppercaseecho "hello" | tr 'a-z' 'A-Z'
# Delete charactersecho "hello 123" | tr -d '0-9' # remove digits
# Squeeze repeated charactersecho "aaabbbccc" | tr -s 'a-z' # → abc
# Replace newlines with spacescat file.txt | tr '\n' ' 'tail -fis the go-to for watching live logs.sed -iedits in place — make a backup first if unsure:sed -i.bak 's/foo/bar/g' file.- Chain
sort | uniq -c | sort -rnto get a frequency count of lines. awkis overkill for simple column extraction — usecutfor speed.