how to add new lines using sort


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to add new lines using sort
# 1  
Old 12-11-2010
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.
# 2  
Old 12-11-2010
"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.
# 3  
Old 12-11-2010
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...
# 4  
Old 12-11-2010
If you have cygwin, you should be able to install sed I think, so you might be able to do:
Code:
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.
# 5  
Old 12-11-2010
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.
# 6  
Old 12-11-2010
Quote:
Originally Posted by methyl
The simple solution is to learn the unix "vi" editor
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...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i sort lines lexigraphical in bash?

I am currently having some problems with my script not sorting my files lexiographically. The error seem to be localized here where i sort the utt2spk file, which is done like this.. for x in test train; do for f in text utt2spk; do sort data/$x/$f -o... (7 Replies)
Discussion started by: kidi
7 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

How to sort lines according words?

Hello I greped some lines from an xml file and generated a new file. but some entries are missing my table is unsorted. e.g. NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" NUMBER="2" POSTCODE="33333"... (5 Replies)
Discussion started by: witchblade
5 Replies

4. Shell Programming and Scripting

grep from 3 lines and sort

Pseudo name=hdiskpower54 Symmetrix ID=000190101757 Logical device ID=0601 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ============================================================================== ---------------- Host --------------- - Stor - -- I/O Path - -- Stats --- ### HW... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

5. Shell Programming and Scripting

how to sort lines in the string

Hi guys, I am trying to sort numbers in the string in descending order but for some reason sort fails. n129$ echo "81 240" | sort -r 81 240 n129$ I am not sure what am I doing wrong. Is there a 100% reliable way to make sure that sort will always work. I mean on SUNS and IBM machines. ... (4 Replies)
Discussion started by: aoussenko
4 Replies

6. UNIX for Dummies Questions & Answers

How to sort lines by strings between ()?

Hello,everyone. I am learning some Info commands.I put all commands and their explanations in a file. This is a part of it: ESC PgUp (scroll-other-window-backward)Scroll the other window backward ESC Right (forward-word) Move forward a word ESC r (move-to-window-line) ESC TAB... (3 Replies)
Discussion started by: vic005
3 Replies

7. Shell Programming and Scripting

Need awk to extract lines and sort

Hi, My data looks like this. CHR SNP BP A1 TEST NMISS OR STAT P 0 SNP_A-8282315 0 2 ADD 1530 1.074 0.7707 0.4409 0 SNP_A-8282315 0 2... (11 Replies)
Discussion started by: genehunter
11 Replies

8. Shell Programming and Scripting

Need Help to sort text lines

I need to sort input file as below to display as below: input.txt User: my_id File: oracle/scripts/ssc/ssc_db_info User: your_id File: pkg_files/BWSwsrms/request User: your_id File: pkg_files/BWSwsco/checkConfig.sh OUTPUT: User: my_id File: ... (3 Replies)
Discussion started by: tqlam
3 Replies

9. Shell Programming and Scripting

How to sort lines by substring

Dear all There is a file which contains the following formatted files. I need to sort it by substring(strings after dot) in order to process efficiently. Please give me any idea how to sort it. Sample file: 1AAABBBCCC.20080401 1AAABBBCCC.20080402 2AAABBBCCC.20080401... (3 Replies)
Discussion started by: mr_bold
3 Replies

10. Shell Programming and Scripting

remove duplicated lines without sort

Hi Just wondering whether or not I can remove duplicated lines without sort For example, I use the command who, which shows users who are logging on. In some cases, it shows duplicated lines of users who are logging on more than one terminal. Normally, I would do who | cut -d" " -f1 |... (6 Replies)
Discussion started by: lalelle
6 Replies
Login or Register to Ask a Question