Skip to content

Grokem · https://www.grokem.com/operating-systems/linux/linux-command-line-data-cheat-sheet

LinuxReference

The command line for data work: a cheat sheet that was run

Look at, count, filter, group and sum a CSV file with head, wc, cut, sort, uniq, grep and awk, each with the output it produced.

Grokem Editorial

Editorial desk

Published
  • 4 min read
Tested withGNU awk5.3GNU coreutils8.32GNU grep3.0

0 sec

The command line is a quick way to look at a file before you load it anywhere. These commands work on any Linux system and in Git Bash on Windows; macOS ships BSD versions of some tools, where a few options differ (check man). All the examples use this small file, orders.csv:

text
order_id,country,product,quantity,amount
1,DE,pen,2,2.40
2,FR,lamp,1,24.00
3,DE,lamp,1,24.00
4,ES,pen,5,6.00
5,DE,notebook,3,13.50
6,FR,pen,1,1.20

Look at it

Task Command Result here
First lines head -n 3 orders.csv the header and the first two orders
Last lines tail -n 2 orders.csv orders 5 and 6
Count lines wc -l orders.csv 7 orders.csv (including the header)
Show columns 2 and 5 cut -d, -f2,5 orders.csv country,amount, then DE,2.40, ...

cut -d, -f2,5 splits each line at commas (-d,) and keeps fields 2 and 5. It does not understand quoted commas inside a field, so for messy CSV use a proper parser such as Python's csv module.

Count and group

bash
tail -n +2 orders.csv | cut -d, -f2 | sort | uniq -c | sort -nr
text
      3 DE
      2 FR
      1 ES

Read it left to right as a pipeline. tail -n +2 skips the header, cut takes the country, sort brings equal values together (uniq only collapses neighbours), uniq -c counts them, and sort -nr puts the biggest first. This pattern answers "how many of each?" for any column.

For the distinct values of a column:

bash
tail -n +2 orders.csv | cut -d, -f3 | sort -u
text
lamp
notebook
pen
bash
grep -c ",lamp," orders.csv       # how many lines match
grep -n "DE" orders.csv            # show matching lines with line numbers
text
2
2:1,DE,pen,2,2.40
4:3,DE,lamp,1,24.00
6:5,DE,notebook,3,13.50

Useful options: -i ignores case, -v inverts (lines that do not match), -r searches folders recursively, -E enables extended regular expressions.

Calculate with awk

awk treats each line as fields ($1, $2, ...), and -F, sets the separator. NR is the line number.

Total amount by country:

bash
awk -F, 'NR>1 {sum[$2]+=$5} END {for (c in sum) printf "%s %.2f\n", c, sum[c]}' orders.csv | sort
text
DE 39.90
ES 6.00
FR 25.20

Keep the header and the rows where the amount is above 10:

bash
awk -F, 'NR==1 || $5 > 10' orders.csv
text
order_id,country,product,quantity,amount
2,FR,lamp,1,24.00
3,DE,lamp,1,24.00
5,DE,notebook,3,13.50

Sort

bash
tail -n +2 orders.csv | sort -t, -k5,5nr | head -2
text
2,FR,lamp,1,24.00
3,DE,lamp,1,24.00

-t, sets the separator, -k5,5 sorts by field 5 only, n means numeric and r reversed. Without n, 13.50 would sort before 2.40, because text is compared character by character.

Transform text

bash
echo "Hello World" | tr 'A-Z' 'a-z'        # hello world
echo "a,b;c" | sed 's/[,;]/ /g'            # a b c

tr translates characters one for one. sed 's/old/new/g' replaces text matching a pattern.

Files and differences

bash
find logs -name "*.log"         # logs/a.log
diff f1 f2                       # show what differs
comm -12 s1 s2                   # lines common to two SORTED files

diff prints nothing and exits with 0 when the files match, and exits with 1 when they differ, which makes it usable in scripts. comm needs both inputs sorted.

Make it readable

bash
column -s, -t orders.csv
text
order_id  country  product   quantity  amount
1         DE       pen       2         2.40
2         FR       lamp      1         24.00

Habits that prevent accidents

  • Look before you change. Run the pipeline without the final step first, and check the output.
  • Never edit the only copy. Write results to a new file (> result.csv), not over the input.
  • Mind the header. Many mistakes are the header line being counted, sorted or summed. tail -n +2 and NR>1 skip it.
  • Mind the separator and quotes. These tools split on a character. A comma inside a quoted field breaks them, so switch to a real CSV tool when the data is irregular.
  • Check line endings. Files from Windows end lines with \r\n. A stray carriage return can make a value fail to match. tr -d '\r' < in.csv > out.csv removes it.

From Grokem: https://www.grokem.com/operating-systems/linux/linux-command-line-data-cheat-sheet

Lesson 1 of 4 in Infrastructure basics for data work

How did this strike you?

Be the first to react – it is anonymous.

Community

Questions about this?

Ask in Questions and help and get help from other readers.

Ask a questionBrowse Questions and help

Comments

No comments yet. Share what worked for you, or ask a question.

Choose what we may use. You can change this at any time with “Privacy choices” at the foot of every page. Details are in the Cookie policy and the Privacy policy.

Strictly necessary

Remembers your theme, the lessons you marked as done and this choice. It stays on your device, tracks nothing and cannot be switched off.

Always on
What exactly is collected, and why

Grokem’s own counts

Collects
The pages you open and how long you stay, how far you scroll, where you came from, your device type, browser, screen size, language and time zone. Your IP address is turned into a code that changes every day; it is never stored as it is.
Why
To see how long lessons are read and where readers leave. Not shared with anyone.
Stored
A random visitor ID, a visit ID and the time of your last page view in your browser’s storage (gr_vid, gr_sid, gr_last). No name or email is attached.