Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.