Hacker News new | ask | show | jobs
by ibern 2405 days ago
Here are some ways you could simplify some of the tasks in the article, saving on typing:

    cat data.csv | sed 's/"//g'
can be simplified by doing this instead:

    cat data.csv | tr d '"'

This awk command:

    cat sales.csv | awk -F',' '{print $1}' | sort | uniq
Can be replaced with a simpler (IMO) cut instead:

    cat sales.csv | cut -d , -f 1 | sort | uniq

When using head or tail like this:

    head -n 3
You don't need the -n:

    head -3

Also shout out to jq, xsv, and zsh (extended glob), all nice complements to the typical command line utils.
3 comments

If you want to simplify things, don't employ "useless us of cat". Pass the file as a command arg or re-direct input. And sort has options, so the third/fourth commands can be

sort -u -t, sales.csv

However, those fail with quoted commas.

Also, head -3 is non-POSIX obsolete syntax.

Edit: I don't know why I didn't see other UUOC references initially.

I like cut and tr too, but I try to replace them by sed and awk when I can. I reduces the number of moving parts, and allows you to increase the complexity slowly.

Ex: | sed -e step1 becomes | sed -e step1 -e step2 instead of adding another pipe and another "moving part" like tr

The awk command

  cat sales.csv | awk -F',' '{print $1}' | sort | uniq
can even be further simplified to

  cut -d, -f1 sales.csv | sort -u