Computing.Net > Forums > Unix > Sort help req'd

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sort help req'd

Reply to Message Icon

Name: Malcs72
Date: May 6, 2003 at 04:39:28 Pacific
OS: UNIX
CPU/Ram: Pentium
Comment:

Does anybody know if it if possible to sort a file excluding the first row (Header) or do I have to tail the remaining rows to another file and then sort in that file.

If this is so, I need a little further help with awk and NR to utilise the number of rows, and then subtract by 1 to tail the file.

I've nearly got it but just can't get it to work.




Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: May 6, 2003 at 05:07:52 Pacific
Reply:

I don't know about skipping a one line header but

((line_count = `wc -l filename | awk '{ print $1 }` - 1))

should set line_count to one less than the length of the file.


0

Response Number 2
Name: Malcs72
Date: May 6, 2003 at 05:59:53 Pacific
Reply:

Thanks

helped a lot....


0

Response Number 3
Name: LANkrypt0
Date: May 6, 2003 at 06:00:06 Pacific
Reply:

Using David's example, and taking it a bit further, you should be able to:

#!/bin/ksh
((line_count = `wc -l filename | awk '{ print $1 }` - 1))
tail -$line_count filename | sort


0

Response Number 4
Name: James Boothe
Date: May 6, 2003 at 07:08:30 Pacific
Reply:

"tail -nnn" will go back only so far, so this solution fails on larger files.

To skip the first part of a file, you don't need to know the line count:

tail +2 filename | sort


0

Response Number 5
Name: LANkrypt0
Date: May 6, 2003 at 11:01:21 Pacific
Reply:

Quite nice to know James. Learning something new every day. Thanks!

if thats the case then, why need the line count of the file? Why not simply use the
tail +2 filename|sort
?.


0

Related Posts

See More



Response Number 6
Name: James Boothe
Date: May 6, 2003 at 14:19:10 Pacific
Reply:

Yep, that is my solution. Like I said, you do not need the line count to skip the first n lines of a file.


0

Response Number 7
Name: David Perry
Date: May 7, 2003 at 03:29:10 Pacific
Reply:

sed 1d filename | sort


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Sort help req'd

Sorting in C www.computing.net/answers/unix/sorting-in-c/5628.html

Help with sorting directory www.computing.net/answers/unix/help-with-sorting-directory-/6619.html

more newbie question www.computing.net/answers/unix/more-newbie-question/6443.html