Linux
Command
Library
Basics
Tips
Commands
Text Processing
Substitute text in a file
$
sed
's/old/new/g' [file]
$
sed
-i 's/old/new/g' [file]
$
sed
-i 's/old/new/gI' [file]
Delete lines matching a pattern
$
sed
'/pattern/d' [file]
$
sed
-i '/^$/d' [file]
Print specific lines
$
sed
-n '5,10p' [file]
$
sed
-n '/pattern/p' [file]
Extract fields from text
$
awk
'{print $1}' [file]
$
awk
-F: '{print $1, $3}' [file]
$
awk
'{print NR, $0}' [file]
Filter lines with awk conditions
$
awk
'$3 > 100' [file]
$
awk
'/pattern/ {print $2}' [file]
$
awk
'NR>=5 && NR<=10' [file]
Sum a column of numbers
$
awk
'{sum += $1} END {print sum}' [file]
Sort lines
$
sort
[file]
$
sort
-n [file]
$
sort
-r [file]
$
sort
-t: -k3 -n [file]
$
sort
-u [file]
Filter duplicate lines
$
uniq
[file]
$
uniq
-c [file]
$
uniq
-d [file]
$
uniq
-u [file]
$
sort
[file] |
uniq
-c |
sort
-rn
Cut fields from text
$
cut
-d: -f1 [file]
$
cut
-d',' -f1,3 [file]
$
cut
-c1-10 [file]
Translate or delete characters
$
tr
'a-z' 'A-Z' < [file]
$
tr
-d '[:digit:]' < [file]
$
tr
-s ' ' < [file]
$
tr
'\n' ' ' < [file]
Compare two files
$
diff
[file1] [file2]
$
diff
-u [file1] [file2]
$
diff
-y [file1] [file2]
$
comm
[file1] [file2]
$
cmp
[file1] [file2]
Merge lines of files
$
paste
[file1] [file2]
$
paste
-d',' [file1] [file2]
$
paste
-s [file]
Join files on a common field
$
join
[file1] [file2]
$
join
-t: -1 1 -2 3 [file1] [file2]
Format text into columns
$
column
-t [file]
$
column
-t -s',' [file]
Wrap text to a specific width
$
fmt
-w 80 [file]
$
fold
-w 80 [file]
$
fold
-s -w 80 [file]
Number lines
$
nl
[file]
$
nl
-ba [file]
$
cat
-n [file]
Count lines, words, and characters
$
wc
[file]
$
wc
-l [file]
$
wc
-w [file]
$
wc
-c [file]
> TERMINAL_GEAR
Curated for the Linux community
Copied to clipboard