- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Sort problem
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 07:33 AM
03-02-2009 07:33 AM
I am trying to sort on the employee number in column 10-19, but I get undesirable results.
File is (file.txt):
1998945 Warble
8 5032 Rebholz
1124688 Barrera
2327703 Rebholz
Result is:
1998945 Warble
8 5032 Rebholz
1124688 Barrera
2327703 Rebholz
Command script is:
export LC_COLLATE=en_US.ISO8859-1
export LC_CTYPE=en_US.ISO8859-1
export LC_MESSAGES=en_US.ISO8859-1
export LC_MONETARY=en_US.ISO8859-1
export LC_NUMERIC=en_US.ISO8859-1
export LC_TIME=en_US.ISO8859-1
sort -t# -k1.10n,1.19n file.txt -o test.sorted
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 07:39 AM
03-02-2009 07:39 AM
Re: Sort problem
Posting a textual attachment of the input file would be more helpful.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 07:44 AM
03-02-2009 07:44 AM
Re: Sort problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:00 AM
03-02-2009 08:00 AM
SolutionYou have unequal numbers of fields as shown with the record with an "8" as the first field.
You could do:
# awk '{if (NF>2) {print $2,$0} else {print $1,$0}}' file|sort -kn1,1|cut -d" " -f2-
...which produces:
8 5032 Rebholz
1124688 Barrera
1998945 Warble
2327703 Rebholz
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:09 AM
03-02-2009 08:09 AM
Re: Sort problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:17 AM
03-02-2009 08:17 AM
Re: Sort problem
maybe you made it more complicate then simply?
Where does the '8' comes from?
Is it the last number of the previous column?
Can you give a better sample?
Volkmar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:28 AM
03-02-2009 08:28 AM
Re: Sort problem
In your example, you are expecting sort to recognize a certain range of characters (say 10-to-19 for the employee number) as a field. It is not going to happen as far as my understanding of sort man pages go.
You will need to use some interim parsing of the input file, otherwise all the spaces trailing any line, in the first field is going to be interpreted as a field separator and your first key field for sort is going to be the employee number, whereas when the line starts with that digit "8" your employee number will be key field 2. So, as JRF said, you have non-uniform fields for sort to interpret. And to be perfectly honest, JRF's solution, using awk in conjunction with sort is the most elegant solution you can come up with in your case. If the pipes in that command chain is not working due to the large input file size, the only thing you can do is to parse that command into separate commands and create interim files as you go. That should give you some flexibility.
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:30 AM
03-02-2009 08:30 AM
Re: Sort problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:38 AM
03-02-2009 08:38 AM
Re: Sort problem
$ cat myinputfile | while read line ; do
f1=`echo $line |cut -c 1-9`
f2=`echo $line |cut -c 10-19`
f3=`echo $line |cut -c 20-`
echo $f1","$f2","$f3
done
and see where the character counted fileds are being cut-off.
Hence the need for some external utility like awk to force them to unformity
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:45 AM
03-02-2009 08:45 AM
Re: Sort problem
sort -t# -k1.10nb,1.19nbb file.txt -o test.sorted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:50 AM
03-02-2009 08:50 AM
Re: Sort problem
sort -t'#' -k1.10n,1.19n file.txt -o test.sorted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:51 AM
03-02-2009 08:51 AM
Re: Sort problem
I am not trying to hide anything, but the total file length of the actual file is 1250 bytes in total length. It would be too large to effectively work with here since the small file still behaves the same way.
Result:
1124688 Barrera
1998945 Warble
2327703 Rebholz
8 5032 Rebholz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 08:59 AM
03-02-2009 08:59 AM
Re: Sort problem
I tried "sort -t'#' -k1.10n,1.19n" and the result was:
Usage: sort [-AbcdfiMmnru] [-T Directory] [-tCharacter] [-y kilobytes] [-o File]
[-k Keydefinition].. [[+Position1][-Position2]].. [-z recsz] [File]..
1124688 Barrera
1998945 Warble
2327703 Rebholz
8 5032 Rebholz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 09:02 AM
03-02-2009 09:02 AM
Re: Sort problem
> The "8" is another data component in column 1. It is just a data item. Other data characters can be accounted for in column 1 - 9, but not always.
Exactly. It (or any group of characters surrounded by whitespace, as the default field delimiter) constitutes a "field" in your file. This influences what 'sort()' sees as the n-th field you use as a sort key.
Re-read Mel's explanation of my original post and his second offering to you, too.
> Thanks for the input, but I am trying to use the sort utility only.
You _are_ the 'sort' to solve your problem. You simply need to invariantly define the _field_ you want to sort on. All I did was to manufacture a temporary sort-key; perform the requisite sort; and snip the manufacture key from the output.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 09:02 AM
03-02-2009 09:02 AM
Re: Sort problem
I really appreciate the efforts here as I am at my wits end over this....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 09:09 AM
03-02-2009 09:09 AM
Re: Sort problem
Mel's output returned:
1998945 W,arble,
8 5032 Re,bholz,
1124688 B,arrera,
2327703 R,ebholz,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 09:18 AM
03-02-2009 09:18 AM
Re: Sort problem
sort -t'#' -k1.10nb,1.19nb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 09:19 AM
03-02-2009 09:19 AM
Re: Sort problem
> My understanding was the the -t option would cause sort to use the whole record as a fixed field when the -t specified a character not contained within the input record.
OK, I just figured out that that was why you kept specifying '-t#'. I guess I'm a bit dense today.
This doesn't work on HP-UX as logical as it would seem. It DOES WORK on AIX (and obviously Solaris) underscoring again that various UNIX dialects differ in the edge cases.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 10:07 AM
03-02-2009 10:07 AM
Re: Sort problem
you might try -t"#" or see if "stty -a" is using # for something...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 10:15 AM
03-02-2009 10:15 AM
Re: Sort problem
This looks like the right command except that -o. You need to use ">" or move it sooner:
sort -t# -k1.10n,1.19n file.txt > test.sorted
>I tried "sort -t'#' -k1.10n,1.19n" and the result was: Usage: ...
Do you still get that "Usage:" message?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 11:42 AM
03-02-2009 11:42 AM
Re: Sort problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 11:51 AM
03-02-2009 11:51 AM
Re: Sort problem
It's called COBOL. :-)
Except the numbers must be right justified in their fields. Of course the input procedure could fix them.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 11:52 AM
03-02-2009 11:52 AM
Re: Sort problem
sort -t# -k1.10n,1.19n file.text > test.sorted
should work, while:
sort -t# -k1.10n,1.19n -o test.sorted file.txt
*might* work...
what happens when you run either of those exactly as shown?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 11:58 AM
03-02-2009 11:58 AM
Re: Sort problem
1124688 Barrera
1998945 Warble
2327703 Rebholz
8 5032 Rebholz
Result for ( sort -t# -k1.10n,1.19n -o test.sorted ground.txt2 ):
1124688 Barrera
1998945 Warble
2327703 Rebholz
8 5032 Rebholz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2009 12:06 PM
03-02-2009 12:06 PM
Re: Sort problem
> Dennis: It's called COBOL. :-)
Except the numbers must be right justified in their fields. Of course the input procedure could fix them.
And its what COBOL would call an input procedure that I used when I manufactured the temporary fixed-position sort key. The output procedure stripped it.
The use of '-o output' file doesn't matter in HP-UX. In AIX, this simple form WORKS (even without the '-t#'):
# sort -k1.10n,1.19n file
Regards!
...JRF...