how to add new lines using sort

Hello,

I'm trying to sort a file which has many lines with the sort command.
I'd like to send the sorted content in the same file.
But when I do that, I've the content which is sorted, but all is on one line... I view the content in Notepad++
Could somebody help me to keep the original multi-lines structure after sorting?

Thank you.

"Notepad++" is a windows program, while "sort" -- a UNIX utility -- is usually not installed on Windows. I can't tell how you're using the program, or on what system since you haven't said, but my first guess would be carriage returns are getting stripped out. Windows insists on those in text files, nearly anything else doesn't. How to remedy that depends on what you're doing where, and what utilities are available. You may even be able to just adjust settings in Notepad++ to not need carriage returns.

It's not a good idea to send it to the same file you're sorting for reasons you've already discovered: If something goes wrong, you've just destroyed all your input data. Which is very easy to do, because the most obvious way for people to try it (sort < inputfile > inputfile) is guaranteed to destroy your input data before sort even begins to read it.

Thanks for the advice.

Actually, I don't send it directly to the same file, I use a tmp file which I delete after copying the result to the first file.

I work on Vista, but it's not a choice, I've to make a project for my school and we don't use a Unix OS but Cygwin so I've to proceed like this...

If you have cygwin, you should be able to install sed I think, so you might be able to do:

sed 's/$/\r/g' < input > output

to add the carriage returns back.

It's just important to know what system you're on so we can give advice that's at all useful. This wouldn't work at all on BSD or OSX, for example, which have weird ancient versions of sed.

Explanation.

The line terminator in a unix format text file is the linefeed character.

The line terminator in a M$ format text file is two characters. Carriage-return linefeed. This is a long-standing error in M$ Operating Systems dating back to early MSDOS and its crude screen and print drivers.

In M$ Notepad program the absence of the Carriage-return character causes the program to not recognise the line terminator. Thus the lines are concatonated into one unformatted line when viewed with M$ Notepad program.

The simple solution is to learn the unix "vi" editor and to not edit unix scripts in M$ Notepad. Failing that you need to run a file conversion program.
I don't have Cygwin but I would expect the "dos2ux" (or "dos2unix") and the "ux2dos" (or "unix2dos") programs to be available. Perhaps a poster with Cygwin can clarify this.

If you don't own an adm3a keyboard, you could give nano a try. vi's weird key combos are because it's not designed for our keyboards...