Sponsored Content
Top Forums Shell Programming and Scripting How do i sort lines lexigraphical in bash? Post 302987989 by kidi on Monday 19th of December 2016 02:41:31 PM
Old 12-19-2016
Thanks for the response.. I guess i might have added a detail, on how i check if the sorting is done correctly.

I have a function which goes through, and if that function deem it ok, it would be sorted correctly. Of what i think looks it checks lexigraphically

the function is

Code:
function check_sorted_and_uniq {
  ! awk '{print $1}' $1 | sort | uniq | cmp -s - <(awk '{print $1}' $1) && \
    echo "$0: file $1 is not in sorted order or has duplicates" && exit 1;
}

---------- Post updated at 02:41 PM ---------- Previous update was at 10:33 AM ----------

Quote:
Originally Posted by drl
Hi.

In situations like this, I use msort. It is in many ways a work-alike for the standard sort, but it has a number of extra features, including the one we use here: a hybrid key, which is composed of alphabetic and numeric. Note that there is just the single command msort that does the work, after the setup and the verification of correctness:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate "hybrid" key ordering, msort.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort pass-fail

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
msort -j -q --line --position 1,1 --comparison-type hybrid $FILE |
tee f1

pl " Verify results if possible:"
paste f1 expected-output.txt
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
bash GNU bash 4.3.30
msort 8.53
pass-fail (local) 1.9

-----
 Input data file data1:
fkdo-b-cen6 fkdo
fkdo-b-cen7 fkdo
fkdo-b-cen8 fkdo
flrp-b-an2121 flrp
flrp-b-an21 flrp
flrp-b-an22 flrp
flrp-b-an23 flrp
flrp-b-an24 flrp
flrp-b-an25 flrp
flrp-b-cen1 flrp

-----
 Results:
fkdo-b-cen6 fkdo
fkdo-b-cen7 fkdo
fkdo-b-cen8 fkdo
flrp-b-an21 flrp
flrp-b-an22 flrp
flrp-b-an23 flrp
flrp-b-an24 flrp
flrp-b-an25 flrp
flrp-b-an2121 flrp
flrp-b-cen1 flrp

-----
 Verify results if possible:
fkdo-b-cen6 fkdo        fkdo-b-cen6 fkdo
fkdo-b-cen7 fkdo        fkdo-b-cen7 fkdo
fkdo-b-cen8 fkdo        fkdo-b-cen8 fkdo
flrp-b-an21 flrp        flrp-b-an21 flrp
flrp-b-an22 flrp        flrp-b-an22 flrp
flrp-b-an23 flrp        flrp-b-an23 flrp
flrp-b-an24 flrp        flrp-b-an24 flrp
flrp-b-an25 flrp        flrp-b-an25 flrp
flrp-b-an2121 flrp      flrp-b-an2121 flrp
flrp-b-cen1 flrp        flrp-b-cen1 flrp

-----
 Comparison of 10 created lines with 10 lines of desired results:
 Succeeded -- files (computed) f1 and (standard) expected-output.txt have same content.

There is a price to pay for the features -- msort is slower than the standard sort.

The command msort can be found in many repositories, or, as noted in the details below, be also found at the msort home site:
Code:
msort   sort records in complex ways (man)
Path    : /usr/bin/msort
Version : 8.53
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help    : probably available with -h,--help
Home    : http://billposer.org/Software/msort.html

Best wishes ... cheers, drl

I am not sure i understand how i should make it sort my input text?.. How slow is it?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort (bash command)

I did a search on this, and found lots on SORT but no answer to my question. I have a C program that fetches all of our users from Netware, and I have that it makes a file that I later include in a html as a select tag drop-down menu. Here is what 1 line looks like: <option... (5 Replies)
Discussion started by: booboo
5 Replies

2. Shell Programming and Scripting

How to sort decimal values in bash

Hi, I have a list of values from associative array from 0,..till 1.0000. I tried various sort options; sort -g, sort -nr but it still couldnt work. In other words, the numbers are not sorted accordingly. Please help. Thanks. (1 Reply)
Discussion started by: ahjiefreak
1 Replies

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

4. Shell Programming and Scripting

BASH: Sort four lines based on first line

I am in the process of sorting an AutoHotkey script's contents so as to make it easier for me to find and view its nearly 200 buzzwords (when I forget which one corresponds with what phrase, which I do now and then). About half to two-thirds of the script's key phrases correspond to locations... (7 Replies)
Discussion started by: SilversleevesX
7 Replies

5. Shell Programming and Scripting

grep from 3 lines and sort

Pseudo name=hdiskpower54 Symmetrix ID=000190101757 Logical device ID=0601 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ============================================================================== ---------------- Host --------------- - Stor - -- I/O Path - -- Stats --- ### HW... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

6. Shell Programming and Scripting

Bash - remove duplicates without sort

I need to use bash to remove duplicates without using sort first. I can not use: cat file | sort | uniq But when I use only cat file | uniq some duplicates are not removed. (4 Replies)
Discussion started by: locoroco
4 Replies

7. UNIX for Dummies Questions & Answers

Bash script to sort files

I've got a disorganized list of items and quantities for each. I've been using a combination of grep and sort to find out how much to buy of each item. I'm tired of having to constantly using these commands so I've been trying to write a shell script to make it easier, but I can't figure out how... (3 Replies)
Discussion started by: PTcharger
3 Replies

8. Shell Programming and Scripting

How to sort lines according words?

Hello I greped some lines from an xml file and generated a new file. but some entries are missing my table is unsorted. e.g. NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" NUMBER="2" POSTCODE="33333"... (5 Replies)
Discussion started by: witchblade
5 Replies

9. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

10. UNIX for Beginners Questions & Answers

How to sort file with certain criteria (bash)?

I am running a command that is part of a script and this is what I am getting when it is sorted by the command: command: ls /tmp/test/*NDMP*.z /tmp/test/CARS-GOLD-NET_CHROMJOB-01-XZ-ARCHIVE-NDMP.z /tmp/test/CARS-GOLD-NET_CHROMJOB-01-XZ-NDMP.z... (2 Replies)
Discussion started by: newbie2010
2 Replies
All times are GMT -4. The time now is 04:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy