Tips on Unix utilities and related topics from @JohnDCook.

Joined August 2011
'The enjoyment of one's tools is an essential ingredient of successful work.' -- Donald E. Knuth
7
349
10
501
'It is easier to port a shell than a shell script.' -- Larry Wall
2
8
0
55
sed pattern for lines not containing spam: /spam/!
2
1
1
14
Count the number of commas in the first row of a file: awk -F, "NR == 1 {print NF}" foo.csv
1
8
0
49
Print the numbers of the lines that don't have 42 commas and print how many commas they do have. awk -F, "NF != 42 {print NR, NF}" foo.csv
3
9
0
23
Oops. Both of these are off by 1. NF is the number of fields, not the number of field separators. n commas results in n+1 fields.
2
2
0
26
Show how many times each line in a sorted file is repeated: uniq -c
1
6
0
34
The sed command p prints. For example, print lines 3 through 7 of a file: sed -n '3,7p' somefile
1
10
1
38
Unix tool tip retweeted
Print lines in foo.txt containing non-ASCII characters, along with line numbers. perl -wnlE 'say qq{$. $_} if /[^\x00-\x7f]/' foo.txt
1
9
1
36
Show this thread
Sort a file in case-insensitive order: sort -f. 'f' for 'fold,' i.e. fold upper and lower case together
0
5
0
21
Kill a line in Emacs from anywhere on that line: M-shift-backspace
4
3
1
16
“GUI-minded programs seldom lend themselves to being used as components in larger tools.” — Tom Christiansen
3
25
5
141
Output a file, displaying non-printing characters: cat -v
2
6
0
46
Set theory at the command line johndcook.com/blog/2019/11/2…
1
14
1
50
The name 'awk' comes from the initials of its creators: Aho, Weinberger, and Kernighan
2
15
0
66
Convert 1101 from binary to decimal in bash: > echo $((2#1101)) 13
3
36
1
158
The option -E or --ignore-tab-expansion tells diff to ignore the distinction between tabs and spaces.
2
6
0
20