Warning while sorting : A newline character was added to the end of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Warning while sorting : A newline character was added to the end of file
# 1  
Old 07-21-2014
Warning while sorting : A newline character was added to the end of file

Hi,

I am trying to sort a csv file which has say 10 lines each line having a row size that is upto 30183 no. of COLUMNS (Row length = 30183). There is a LINE FEED (LF) at the end of each line. When I try to sort this file say, based on the second FIELD using the below command,
Code:
sort -t ',' +1 -2 file1.dat

there is a warning that appears in the command line
"Warning : A newline character was added to the end of file file1.dat"

Also, there are 10 empty lines that get added to the file file1.dat along with the sorted 10 lines data.
If for a particular line I reduce the row size to say a few 100 COLUMNS (Row size= say, 250) then this warning doesn't appear and no empty line gets added for that particular line.

I am running the script on ksh under AIX server.

Can anyone tell me why this is happening? Is this really because of the huge row length?

Last edited by bartus11; 07-21-2014 at 10:22 AM.. Reason: Please use [code][/code] tags.
# 2  
Old 07-21-2014
Upload the file you're sorting so we can see it.
Did you try using
Code:
tr -d '<special-chars, like line feed>'

before sorting?
# 3  
Old 07-21-2014
I guess the last line does not end with LF (0a hex),
instead there is a sequence of zero-bytes.
Please check with
Code:
od -x file1.dat | tail

# 4  
Old 07-21-2014
The sort utility is defined to work on text files. By definition, text files contain lines that are no longer than LINE_MAX bytes (including the line terminating <newline> character. You can check the value of LINE_MAX on your AIX system using the command:
Code:
getconf LINE_MAX

The standards require LINE_MAX to be at least 2048 and that is the value supported on many UNIX systems.

I would guess that file1.dat contains lines longer than LINE_MAX bytes and you are seeing the undefined behavior that results when sort is asked to process a file that is not a text file.

You could use cut to split your input files with long lines into groups of files with shorter lines (copying the sort keys into each of the files), sort each of the files in the group, and then use paste to recreate your sorted output into a single file with long lines.

(Note, however, that sort -t "," +1 -2 will sort on the remainder of the input line if the sort key compares equal on some lines. So, if your sort keys are not unique, you need to duplicate enough fields to guarantee that you get the same sort order in all of your split files.)
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-21-2014
Hi.

There is a perl version of sort at http://cpansearch.perl.org/src/CWEST...ort/sort.pudge

I have not tried that code, but I generally have had good luck with ppt codes.

Posting a sample of the long-line input files would allow experimentation by responders.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 07-22-2014
Hi Don Cragon,
Thanks for the information about LINE_MAX. Wasn't aware of it. However, I would need a simpler method to solve this problem.

Hi drl,
Will check that perl version of sort.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

No newline at end of file

Hello all, I have maybe a simple Problem - but I do not know to handle it. All what I want, is to write a line to file without a newline at the end. It works with "echo -n" for all lines, but not for the last one. At the end of the file is always a "0a" (hex) My small script: ... (10 Replies)
Discussion started by: API
10 Replies

3. Shell Programming and Scripting

Script for removing newline character from file

Hi below is my file. cat input.dat 101,abhilash,1000 102,prave en,2000 103,partha,4 000 10 4,naresh,5000 (its just a example file) and my output should be: 101,abhilash,1000 102,praveen,2000 103,partha,4000 104,naresh,5000 below is my code cat input.dat |tr -d '\n' >... (6 Replies)
Discussion started by: abhilash_nakka
6 Replies

4. Shell Programming and Scripting

Appending newline character End of File

Hi Gurus, Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file. Sample Data: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#Output: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#\n -- append only... (13 Replies)
Discussion started by: Gouri Solleti
13 Replies

5. Shell Programming and Scripting

How to add newline character at end of file?

Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. foreach file (`ls $dd_PLAYCARD_EDI_IN`) if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then cat -n... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

6. Shell Programming and Scripting

Echo to file using SH without adding newline character

Hello! I am able to do this in bash, using: echo -ne HELLO > file.txt and then, 'HELLO' is written into file.txt without the newline character to be added in the end of the file. How is this possible to be done using sh instead of bash? If I try something similar is SH, then inside... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

How to check newline character in file?

Hi All, I have file with only one record,always be only one record. as like below. if that line contains newline end of the line..no need to add, if not just add the new line character. END OF FILE. ROW COUNT: 7 Please help me.. Thanks, (9 Replies)
Discussion started by: bmk
9 Replies

8. Shell Programming and Scripting

How to remove a newline character at the end of filename

Hi All, I have named a file with current date,time and year as follows: month=`date | awk '{print $2}'` date=`date | awk '{print $3}'` year=`date | awk '{print $6}'` time=`date +%Hh_%Mm_%Ss'` filename="test_"$month"_"$date"_"$year"_"$time".txt" > $filename The file is created with a... (2 Replies)
Discussion started by: amio
2 Replies

9. Shell Programming and Scripting

Append newline at the file end

Hi All, Is there any way to append a newline character at the end of a file(coma-separated file), through shell script? I need to check whether newline character exists at the end of a file, if it does not then append it. Regards, Krishna (1 Reply)
Discussion started by: KrishnaSaran
1 Replies

10. UNIX for Dummies Questions & Answers

extran NUll character added after end of line "\n"

Hi All, I am facing a strange situation and want to find why it is occuring . When i convert the whole line into Hexadecimal character i can find the junk value after new line (\n) . If i look in binary mode it is not visible. PLease let me know how possible the junk character is added... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question