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
# 15  
Old 03-03-2014
You did not upload your input file as I requested.

You did not tell us what operating system and version you're using as I requested.

You did not show us the output you are trying to get as I requested.

If your input file is encoded using ASCII and your locale has a collating order that matches ASCII unsigned character ordering, the simple command:
Code:
sort -o x.sorted x.raw

should do what I think you're trying to do. (And, on Mac OS X, it does.)

If the command:
Code:
sort -o x.sorted x.raw

or the command:
Code:
LC_ALL=C sort -t '|' -k1,1 -o x.sorted x.raw

doesn't produce the output:
Code:
Host=Fedora20.Bachelor           |          |                       scan from|/home/leslie/Development/scandirFeb23
1to8.txt                         |20140223 0056|25d55ad283aa400af464c76d713c07ad|/home/leslie/Development/scandirFeb23/md5-c
DATE1                            |20140223 0056|e0167034133516d3ad5d61a09bae8156|/home/leslie/Development/scandirFeb23
DATE2                            |20140223 0056|e606fe0237c786174d2087090f81644a|/home/leslie/Development/scandirFeb23
a.txt                            |20140223 0056|933222b19ff3e7ea5f65517ea1f7d57e|/home/leslie/Development/scandirFeb23/md5-c
alpha.txt                        |20140223 0056|83e065ac9ed97eca51391c20e9671373|/home/leslie/Development/scandirFeb23/md5-c
crc32.c                          |20140223 0056|4d7a5dbb246898ff9d3ba19c0ded7f5b|/home/leslie/Development/scandirFeb23
crc32.h                          |20140223 0056|c15674694592358889120712db73be69|/home/leslie/Development/scandirFeb23
crc32.o                          |20140301 1912|10a49aede5f82d00205c1f89a8931731|/home/leslie/Development/scandirFeb23
daycalc.c                        |20140223 0056|1dd882b48e5c156748aba7fb38dbba51|/home/leslie/Development/scandirFeb23
dirdepth                         |20140301 1912|9f2ff1bd8b133ca0de8d124ad7d761d2|/home/leslie/Development/scandirFeb23
dirdepth.c                       |20140223 0056|a7c3f1c02245aec9a1b651e11018ff82|/home/leslie/Development/scandirFeb23
dirent.h                         |20140223 0056|1906fd554bf036fdf6ffd0b054ca321d|/home/leslie/Development/scandirFeb23
empty.txt                        |20140223 0056|d41d8cd98f00b204e9800998ecf8427e|/home/leslie/Development/scandirFeb23/md5-c
gcc.txt                          |20140223 0056|b8917c1a087abbf74f0294dad9cbf698|/home/leslie/Development/scandirFeb23
inih_r27.tar                     |20140223 0056|a8da6db331c8fe638cbb8c6940ce303e|/home/leslie/Development/scandirFeb23
inih_r28Dec16.00.tar             |20140223 0056|6fe6356f0ba2e501c2713958f119d493|/home/leslie/Development/scandirFeb23
itcrftn.c                        |20140223 0056|b1f1444cfdc35b6427ad3b002a176e9f|/home/leslie/Development/scandirFeb23
log.txt                          |20140223 0056|187c3fdbde0febf71257f0c0da9e21e7|/home/leslie/Development/scandirFeb23/md5-c
makefile                         |20140223 0056|26955e927da56d1343af738a247b87e1|/home/leslie/Development/scandirFeb23/md5-c
makefile                         |20140301 1912|02c8266eb8c3d3b52eabb30378ef9895|/home/leslie/Development/scandirFeb23
md5

(which is what I think you're trying to get) when the 1st two characters of the first and last fields on the 1st line are <space><backspace>, I don't know what else to suggest. Both of the above commands produce this output when using sort on Mac OS X and should produce the same output on any system with a sort utility that conforms to the standards.

Are you sure that you don't have an alias in place for sort that is adding options you don't want in this case? (What output do you get from the command: type sort?)
# 16  
Old 03-05-2014
To All

I tried each of the examples you presented and I want to thank you for the help.

You asked for an uploaded file, and one is attached.

To upload, I had to change a filename from x.raw to x.txt

The line I confirmed worked on my system was the one proposed. It is the

Code:
LC_ALL=C sort -t '|' -k1,1 -o x.sorted x.txt

and it works. I am delighted.

I was not aware of how to specify the LC_ALL=C or its significance.

Here is the slightly larger file I was using (from the same population)

To upload, as I mentioned, I had to change the upload filename to x.txt x.txt

I intend to put the scanner into the public FOSS domain.

The purpose of the scanner is to obtain a checksum (md5 or sha1, or crc32) of all files beneath a given directory.

A blacklist facility to omit some subdirectories is part of the scanner.


Why do this? Auditors where I was working wanted to detect changes in production files (ERP object or source code). The scanner would be run daily (auditors did not require realtime notices), they wanted proof that any software implementation followed corporate implementation rules.

This software was developed on my own computers in my home.

Want a source copy? Just ask for one. Want to collaborate to extend it? ditto.

Thank you all,

Leslie

PS. My response was late. Your messages were filtered into SPAM. I check SPAM every few days and that is why today, now, I am responding.

Last edited by Scrutinizer; 03-06-2014 at 12:29 AM.. Reason: code tags
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