MOHAMED
MOHAMED

Reputation: 43558

How to sort lines based on a sub string in the line

I have the following output:

aaa=12
bbb=124
cccc=1
dddd=15

I want to sort the above lines based on the value.s so the output should look like this:

$ cat file | awk_or_sed_or_any_command
cccc=1
aaa=12
dddd=15
bbb=124

UPDATE

I tried the following commands:

$ cat file | awk -F '=' '{print $2"="$1}' | sort |  awk -F '=' '{print $2"="$1}'

But it's too long.

Are there another suggestion better than the above one?

Note: my linux use sort from busybox that support only the following options:

$ sort --help
BusyBox v1.19.4 (2014-04-04 18:50:39 CEST) multi-call binary.

Usage: sort [-nru] [FILE]...

Sort lines of text

        -n      Sort numbers
        -r      Reverse sort order
        -u      Suppress duplicate lines

Upvotes: 3

Views: 7873

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed -e 's/=\(.*\)$/=000000\1_\1/;s/=0*\([0-9]\{7\}\)_/=\1_/' YourFile | sort | sed -e 's/=[0-9]_/=/'

prepare for a basic sort not numeric nor taking column than put back in original form

Upvotes: 0

user3159253
user3159253

Reputation: 17455

Use the following command

sort -n -t = -k 2 your_file

gives me

alex@rhyme ~ $ ash
$ cat <<EOF | sort -n -t = -k 2
> aaa=12
> bbb=124
> cccc=1
> dddd=15
> EOF 
cccc=1
aaa=12
dddd=15
bbb=124
$ which sort
/usr/bin/sort
$ LANG=C sort --version
sort (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.

Check the sort manpage for other sort options

Upvotes: 7

Related Questions