I wanted get the output in assending order (i.e high value lines to low value lines) of a file.
file:-
1|21
2|12
3|0
4|2
5|1
6|3
7|13
8|32
9|123
10|1234
Expected Output :-
10|1234
9|123
8|32
1|21
7|13
2|12
6|3
4|2
5|1
3|0
I tried below command but not getting expected output
cmd:- sort -t "|" -k 2 12.txt
You seem to want to sort numbers in reverse order.
sort -rn -t "|" -k 2 12.txt
In bash shell scripting, I have users.txt file.
sort -t: -k 3n users.txt gives me
gg:g#g.g:
ff:f#f.f:5
gdr:d#sd.ds:5
g:w#f.com:8
r:w#d.com:85
qaws:aaa#d.com:789
qaz:a#s.com:789
vcf:d#c.com:855
op:popo#ppoo.po:6465
zx:a#c.com:7845
az:a#aa.com:7894
w:a#a.com:7894
h:g#g.com:7899
lp:l#l.com:8888
t:s#s.com:85474
dssdfsdf:fssdf#mbhkj.sdf:654654
u:f#g.comn:6546546
a:a#a.com:7894561
tt:t#t.t:1234567890
This is ok. But I need 3rd column which contain only phone number in reverse order i.e. descending order. How to achieve that?
I need like
tt:t#t.t:1234567890
a:a#a.com:7894561
u:f#g.comn:6546546
dssdfsdf:fssdf#mbhkj.sdf:654654
t:s#s.com:85474
lp:l#l.com:8888
and so on...
You could add an r (short for "reverse") to the order column specification:
$ sort -t: -k 3nr users.txt
# Here ---------^
a Linux question: I have the CSV file data.csv with the following fields and values
KEY,LEVEL,DATA
2.456,2,aaa
2.456,1,zzz
0.867,2,bbb
9.775,4,ddd
0.867,1,ccc
2.456,0,ttt
...
The field KEY is a float value, while LEVEL is an integer. I know that the first field can have repeated values, as well as the second one, but if you take them together you have a unique couple.
What I would like to do is to sort the file according to the column KEY and then for each unique value under KEY keep only the row having the higher value under LEVEL.
Sorting is not a problem:
$> sort -t, -k1,2 data.csv # fields: KEY,LEVEL,DATA
0.867,1,ccc
0.867,2,bbb
2.456,0,ttt
2.456,1,zzz
2.456,2,aaa
9.775,4,ddd
...
but then how can I filter the rows so that I get what I want, which is:
0.867,2,bbb
2.456,2,aaa
9.775,4,ddd
...
Is there a way to do it using command line tools like sort, uniq, awk and so on? Thanks in advance
try this line:
your sort...|awk -F, 'k&&k!=$1{print p}{p=$0;k=$1}END{print p}'
output:
kent$ echo "0.867,1,bbb
0.867,2,ccc
2.456,0,ttt
2.456,1,zzz
2.456,2,aaa
9.775,4,ddd"|awk -F, 'k&&k!=$1{print p}{p=$0;k=$1}END{print p}'
0.867,2,ccc
2.456,2,aaa
9.775,4,ddd
The idea is, because your file is already sorted, just go through the file/input from top, if the first column (KEY) changed, print the last line, which is the highest value of LEVEL of last KEY
try with your real data, it should work.
also the whole logic (with your sort) could be done by awk in single process.
Use:
$> sort -r data.csv | uniq -w 5 | sort
given your floats are formatted "0.000"-"9.999"
Perl solution:
perl -aF, -ne '$h{$F[0]} = [#F[1,2]] if $F[1] > $h{$F[0]}[0]
}{
print join ",", $_, #{$h{$_}} for sort {$a<=>$b} keys %h' data.csv
Note that the result is different from the one you requested, the first line contains bbb, not ccc.
I have a file, with contents like:
onelab2.warsaw.rd.tp.pl 5
onelab3.warsaw.rd.tp.pl 5
lefthand.eecs.harvard.edu 7
righthand.eecs.harvard.edu 7
planetlab2.netlab.uky.edu 8
planet1.scs.cs.nyu.edu 9
planetx.scs.cs.nyu.edu 9
so for each line, there is a number I want the 1st line for each number so for
the content above, I want to get:
onelab2.warsaw.rd.tp.pl 5
lefthand.eecs.harvard.edu 7
planetlab2.netlab.uky.edu 8
planet1.scs.cs.nyu.edu 9
How can I achieve this? I hope for shell scripts, with awk, sed, etc.
This might work for you (GNU sort):
sort -nsuk2 file
Sort the -k2 second field -n numerically keeping the -s original order and -u remove duplicates.
Use the awk command for that:
awk '{if(!a[$2]){a[$2]=1; print}}' file.dat
Explanation:
{
# 'a' is a lookup table (array) which will contain all numbers
# that have been printed so far. It will be initialized as an empty
# array on its first usage by awk. So you don't have to care about.
# $2 is the second 'column' in the line -> the number
if(!a[$2])
{
# set index in the lookup table. This way the if statement will
# fail for the next line with the same number at the end
a[$2]=1;
# print the whole current line
print
}
}
With sort and uniq:
sort -n -k2 input | uniq -f1
perl -ane 'print unless $a{$F[1]}++' file
Example file.txt:
100 foo
2 bar
300 tuu
When using sort -k 1,1 file.txt, the order of lines will not change, though we are expecting :
2 bar
100 foo
300 tuu
How to sort a field consisting of numbers based on the absolute numerical value?
Take a peek at the man page for sort...
-n, --numeric-sort
compare according to string numerical value
So here is an example...
sort -n filename
If you are sorting strings that are mixed text & numbers, for example filenames of rolling logs then sorting with sort -n doesn't work as expected:
$ ls |sort -n
output.log.1
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.2
output.log.20
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9
In that case option -V does the trick:
$ ls |sort -V
output.log.1
output.log.2
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.20
from man page:
-V, --version-sort
natural sort of (version) numbers within text
Well, most other answers here refer to
sort -n
However, I'm not sure this works for negative numbers. Here are the results I get with sort version 6.10 on Fedora 9.
Input file:
-0.907928466796875
-0.61614990234375
1.135406494140625
0.48614501953125
-0.4140167236328125
Output:
-0.4140167236328125
0.48614501953125
-0.61614990234375
-0.907928466796875
1.135406494140625
Which is obviously not ordered by numeric value.
Then, I guess that a more precise answer would be to use sort -n but only if all the values are positive.
P.S.: Using sort -g returns just the same results for this example
Edit:
Looks like the locale settings affect how the minus sign affects the order (see here). In order to get proper results I just did:
LC_ALL=C sort -n filename.txt
You have to use the numeric sort option:
sort -n -k 1,1 File.txt
Use sort -n or sort --numeric-sort.
You must do the following command:
sort -n -k1 filename
That should do it :)
Use sort -nr for sorting in descending order. Refer
Sort
Refer the above Man page for further reference
echo " Enter any values to sorting: "
read n
i=0;
t=0;
echo " Enter the n value: "
for(( i=0;i<n;i++ ))
do
read s[$i]
done
for(( i=0;i<n;i++ ))
do
for(( j=i+1;j<n;j++ ))
do
if [ ${s[$i]} -gt ${s[$j]} ]
then
t=${s[$i]}
s[$i]=${s[$j]}
s[$j]=$t
fi
done
done
for(( i=0;i<n;i++ ))
do
echo " ${s[$i]} "
done