SORT empties my file?

Posted by Jonathan Sampson on Super User See other posts from Super User or by Jonathan Sampson
Published on 2010-05-18T14:46:21Z Indexed on 2010/05/18 14:52 UTC
Read the original article Hit count: 449

Filed under:
|
|

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,,,,[email protected],,,GA,United States,,
Jane Doe,Mrs,,,[email protected],,,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.

© Super User or respective owner

Related posts about linux

Related posts about command-line