Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting to sort a csv on my machine, but I seem to be erasing the contents each time I use the sort command. I've basically created a copy of my csv lacking the first row:

sed '1d' original.csv > newcopy.csv

To confirm that my new copy exists lacking the first row I can check with head:

head 1 newcopy.csv

Sure enough, it finds my file and shows me the original second now (now first row). My csv consists of numerous values seperated by commas:

Jonathan Sampson,,,,foo@bar.com,,,GA,United States,,
Jane Doe,Mrs,,,bar@foo.com,,,FL,United States,32501,

As indicated above, some fields are empty. I want to sort based upon the email address field, which is either 4, or 5 - depending on whether the sort command uses a zero-based index. So I'm trying the following:

sort -t, +4 -5 newcopy.csv > newcopy.csv

So I'm using -t, to indicate that my fields are terminated by the comma, rather than a space. I'm not sure if +4 -5 actually sorts on the email field or not - I could use some help here. And then newcopy.csv > newcopy.csv to overwrite the original file with new sort results.

After I do this, if I try to read in the first line:

head 1 newcopy.csv

I get the following error:

head: cannot open `1' for reading: No such file or directory ==> newcopy.csv <==

Sure enough, if I check my directory the file is now empty, and 0 bytes.

share|improve this question
up vote 3 down vote accepted

Redirection truncates the output file before it's read on the input side. You'll need to use a temp file.

Edit:

Here's an example:

sort -t, -k 5,5 newcopy.csv > tmp.csv && mv tmp.csv newcopy.csv

Sort fields are one-based. This command says to sort based on field five only, output to a temp file and only move the temp file back to the original name if the sort exited with no errors.

share|improve this answer

Please note that sort has -o parameter which write results to the file, instead of standard output, so you should do:

sort -t, +4 -5 -o newcopy.csv newcopy.csv

since when using redirection operator (such as > or >>) shell creates/truncates file before the command is even invoked.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .