Need to sort text keeping first line always first


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to sort text keeping first line always first
# 1  
Old 02-28-2014
Need to sort text keeping first line always first

I have a file is created from standard output.

I have put a leading space to force the first line to collate low vis a vis the rest of the lines.

If I pass the entire file to the Linux sort, it ignores the leading space and the first line appears in somewhere in the list.

If I add lots of leading spaces, it does not help.

I was thinking of putting leading !!!, which collates lower than the alphabet, and then running sed thereafter to eliminate the !!!.


Is that the best way or are there better ways?
# 2  
Old 03-01-2014
Without knowing more about your data and the sort keys you're using to sort the data, it is hard to make suggestions about what might be the best way.
# 3  
Old 03-01-2014
Your
Code:
locale

might be non-ASCII.
Try
Code:
LC_ALL=C sort ...

or
Code:
LC_COLLATE=C sort ...

# 4  
Old 03-01-2014
Hi,
There was some version of "gnu sort" with a bug of '-b' option was enabled by default.
If you can not do otherwise than change your line with '!!!' , maybe this example will help you:
File example:
Code:
$ cat file
zzz
cat big 24
cat small   13
cat red 63
dog big 34
chicken plays   39
fish    red 294

The command that sort file without first line:
Code:
$ (head -1 file;sort <(sed -n '2,$p' file)) >file2

Resultat:
Code:
$ cat file2
zzz
cat big 24
cat red 63
cat small   13
chicken plays   39
dog big 34
fish    red 294

# 5  
Old 03-01-2014
The first line of the file is a header, containing..

I have a file consisting of fixed columns
title showing from where the file originates
lines containing
Code:
progname|date|sha1 checksum|path

I want to sort by progname but to keep the top line.
The top line had
Code:
!!!Date=yymmdd, ...system = ssssssssss

I have tried to set very low print values in the leading columns of the title, but that did not work (Fedora Linux 20). I am not sure that -b -d or -g works for the sort. (I could not detect differences).
I would just want a binary sort over the first 33 characters of the file. sort -k will limit the sort to those columns but it ignored the !!! and therefore the line with date=yyyy
appeared in line.

I can upload a small sample unsorted file, if that helps.

Last edited by Scrutinizer; 03-01-2014 at 11:32 AM.. Reason: code tags
# 6  
Old 03-01-2014
Perhaps it would be easiest to just exclude the first line from sorting, for example:
Code:
{ IFS= read -r line; printf "%s\n" "$line"; sort;} < file > file.sorted

-edit
Just saw disedorgue posted something similar. This could be an alternative then. Just add sort options ad libitum..

Last edited by Scrutinizer; 03-01-2014 at 02:02 PM..
# 7  
Old 03-01-2014
Try just having your first line in your input file have a <space> as the 1st character on the line and sort with the command:
Code:
sort -t '|'

Specifying a non-space field separator should make the leading space significant.

If you don't want a visible space at the start of the first line, make the first two characters in the file be a <space> character followed a <backspace> character.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

2. UNIX for Dummies Questions & Answers

sort by keeping the headings intact?

Hi all, I have a file with 3 columns separated by space. Each column has a heading. I want to sort according to the values in the 2nd column (ascending order). Ex. Name rank direction goory 0.05 --+ laby 0.0006 --- namy 0.31 -+- ....etc. Output should be Name rank direction laby... (3 Replies)
Discussion started by: Unilearn
3 Replies

3. Shell Programming and Scripting

Keeping the title of a text report

I am in need of keeping a title of a report and removing duplicates from a file like the one below. I will be using the `uniq –u` command for the removal of duplicate lines (let me know if there is a better way rather than the command `uniq`) but I need to keep the title (first 9 lines) of the... (2 Replies)
Discussion started by: petersf
2 Replies

4. Shell Programming and Scripting

sort text file

HI all i have a text file file1 like this 004002004545454000001 041002004545222000002 006003008751525000003 007003008751352000004 006003008751142000005 004001005745745000006 i want to sort the file according to position 1-5 and secondary sort by the last position of file 16-21... (4 Replies)
Discussion started by: naamas03
4 Replies

5. UNIX for Dummies Questions & Answers

Sort and uniq lines of a file while keeping a header line

So, I have a file that has some duplicate lines. The file has a header line that I would like to keep at the top. I could do this by extracting the header from the file, 'sort -u' the remaining lines, and recombine them. But they are quite big, so if there is a way to do it with a single... (1 Reply)
Discussion started by: Digby
1 Replies

6. 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

7. UNIX for Dummies Questions & Answers

Sort Text

Hello, I have a text file that I need to sort the lines by date record=5,French 9,2008-09-02T08:55:00,2008-09-02T10:00:00,2 record=79,Entrepreneurship 30,2008-09-17T11:00:00,2008-09-17T12:00:00,2 record=6,Computer Science 20,2008-09-02T09:55:00,2008-09-02T10:50:00,1... (5 Replies)
Discussion started by: Dallasbr
5 Replies

8. UNIX for Dummies Questions & Answers

deleting a line but keeping the same file

Hi, I want to delete a line in a file that contains a string. I tried: grep -v "mystring" Myfile > Myfile But this makes the Myfile empty. I read that I need to do something like: grep -v "mystring" Myfile > Myfile.new rm Myfile mv Myfile.new Myfile Is there a way to avoid creating a... (2 Replies)
Discussion started by: laiko
2 Replies

9. Shell Programming and Scripting

Sort a file line by line alphabetically

infile: z y x c b a desired output: x y z a b c I don't want to sort the lines into this: a b c x y z nor this: c b a z y x The number of fields per line and number of lines is indeterminate. The field separator is always a space. Thanks for the use of your collective brains.... (11 Replies)
Discussion started by: H2OBoodle
11 Replies

10. Shell Programming and Scripting

Need a Help with sort a text file with some fields

Ive got a file called listacdrs with this structure: 01/09/2006 12:13 p.m. 1.046.528 CF0155.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0156.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0157.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0158.DAT 01/09/2006 12:14 p.m. ... (3 Replies)
Discussion started by: alexcol
3 Replies
Login or Register to Ask a Question