Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm all for this kind of exploratory hacking around before booting up python/R/Excel/duckdb, especially in constrained environments. A classic pain point is having to deal with column numbers, so I'll share my favorite trick:

`head -n1 /path/to/file.csv | tr ',' '\n' | nl | grep desired_column`

gives you the column number of desired_column



Something to watch out for with nl is that by default it doesn't number empty lines. e.g.:

  $ printf 'one\n\nthree\n' | nl
     1  one

     2  three
Set -ba to enable numbering all lines.

For this use case I usually end up running cat -n instead since I find it easier to remember.


yep, without knowing about `nl` I used `...| grep -n column_header` or `...|grep -n .` to replicate the 'nl' behavior.

edit: I like your 'nl' better as it is using white space instead of colon as a separator.


oh, didn't realise you could do that - I've tended to use `... |awk '{print $NF, $0}'` to add line numbers to something


Unless there is a quoted comma or an empty column beforehand (nl “helpfully” skips empty lines for numbering purposes).


grep -n also works in place of `nl`!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: