Skip to content

File Text Manipulation

File & Text Manipulation — Quick Reference

Section titled “File & Text Manipulation — Quick Reference”

Terminal window
cat file.txt # print file
cat file1.txt file2.txt # concatenate two files
cat -n file.txt # print with line numbers
cat file1.txt file2.txt > combined.txt # merge into new file

Better than cat for large files — doesn’t load everything at once.

Terminal window
less file.txt
less +F file.txt # follow mode (like tail -f)
KeyAction
Space / fPage down
bPage up
gGo to start
GGo to end
/patternSearch forward
n / NNext / previous match
qQuit

Terminal window
head file.txt # first 10 lines (default)
head -n 20 file.txt # first 20 lines
tail file.txt # last 10 lines
tail -n 50 file.txt # last 50 lines
tail -f app.log # follow — stream new lines live
tail -f app.log | grep ERROR # follow and filter

Terminal window
# Replace first occurrence per line
sed 's/foo/bar/' file.txt
# Replace all occurrences (global flag)
sed 's/foo/bar/g' file.txt
# Edit file in place
sed -i 's/foo/bar/g' file.txt
# Delete lines matching a pattern
sed '/^#/d' file.txt # delete comment lines
# Print only matching lines
sed -n '/error/p' file.txt
# Replace on a specific line number
sed '5s/foo/bar/' file.txt
# Delete blank lines
sed '/^$/d' file.txt

Terminal window
# 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 > 100
awk '$3 > 100' file.txt
# Sum a column
awk '{sum += $2} END {print sum}' file.txt
# Print line numbers with content
awk '{print NR": "$0}' file.txt
# Print lines between two patterns
awk '/START/,/END/' file.txt

Terminal window
# Extract characters 1-5
cut -c1-5 file.txt
# Extract field 2 (tab-delimited by default)
cut -f2 file.txt
# Custom delimiter
cut -d',' -f1,3 file.csv # fields 1 and 3 from CSV
# Extract from ls output
ls -l | cut -d' ' -f1 # permissions column

Terminal window
sort file.txt # alphabetical
sort -r file.txt # reverse
sort -n file.txt # numeric sort
sort -u file.txt # unique lines only
sort -k2 file.txt # sort by 2nd column
sort -t',' -k2 -n file.csv # CSV, sort numerically by col 2

Works on sorted input.

Terminal window
sort file.txt | uniq # remove duplicate lines
sort file.txt | uniq -c # prefix each line with count
sort file.txt | uniq -d # print only duplicates
sort file.txt | uniq -u # print only unique lines

Terminal window
wc file.txt # lines, words, bytes
wc -l file.txt # line count only
wc -w file.txt # word count only
wc -c file.txt # byte count
# Count files in directory
ls | wc -l

Terminal window
diff file1.txt file2.txt # basic diff
diff -u file1.txt file2.txt # unified format (like git diff)
diff -r dir1/ dir2/ # recursive directory diff
diff --color file1.txt file2.txt # colored output

Terminal window
# Lowercase to uppercase
echo "hello" | tr 'a-z' 'A-Z'
# Delete characters
echo "hello 123" | tr -d '0-9' # remove digits
# Squeeze repeated characters
echo "aaabbbccc" | tr -s 'a-z' # → abc
# Replace newlines with spaces
cat file.txt | tr '\n' ' '

  • tail -f is the go-to for watching live logs.
  • sed -i edits in place — make a backup first if unsure: sed -i.bak 's/foo/bar/g' file.
  • Chain sort | uniq -c | sort -rn to get a frequency count of lines.
  • awk is overkill for simple column extraction — use cut for speed.

← Linux