SIFA
March 18, 2010, 1:19am
1
Hi,
I need help in printing out the dates with the largest value in front of it using awk.
436 28/Feb/2008
436 27/Feb/2008
436 20/Feb/2008
422 13/Feb/2008
420 23/Feb/2008
409 21/Feb/2008
402 26/Feb/2008
381 22/Feb/2008
374 24/Feb/2008
360 18/Feb/2008
357 15/Feb/2008
356 19/Feb/2008
352 06/Feb/2008
350 11/Feb/2008
346 25/Feb/2008
327 16/Feb/2008
327 01/Feb/2008
323 08/Feb/2008
321 12/Feb/2008
317 14/Feb/2008
eg, so here, the output should be
28/Feb/2008
27/Feb/2008
20/Feb/2008
Can anyone help me with this?
Using sed
sed 's/.* //' input_filename
Here the input_file will contain the data as
436 28/Feb/2008
436 27/Feb/2008
436 20/Feb/2008
422 13/Feb/2008
420 23/Feb/2008
409 21/Feb/2008
402 26/Feb/2008
381 22/Feb/2008
374 24/Feb/2008
360 18/Feb/2008
357 15/Feb/2008
356 19/Feb/2008
352 06/Feb/2008
350 11/Feb/2008
346 25/Feb/2008
327 16/Feb/2008
327 01/Feb/2008
323 08/Feb/2008
321 12/Feb/2008
317 14/Feb/2008
Now the output after executing I got is
28/Feb/2008
27/Feb/2008
20/Feb/2008
13/Feb/2008
23/Feb/2008
21/Feb/2008
26/Feb/2008
22/Feb/2008
24/Feb/2008
18/Feb/2008
15/Feb/2008
19/Feb/2008
06/Feb/2008
11/Feb/2008
25/Feb/2008
16/Feb/2008
01/Feb/2008
08/Feb/2008
12/Feb/2008
14/Feb/2008
Are you expecting like this? or according to the large value of the date it should be ordered?
Hi SIFA,you try the following script to do your task.
i=0
while read line
do
arr[$i]=`echo $line | cut -d ' ' -f 1`
let i+=1
done < input
val=0
for((j=0;j<$i;j++))
do
if [[ ${arr[$j]} -gt $val ]];then
val=${arr[$j]}
fi
done
sed -rn "s/${val} (.*)/\1/p" input
In my code,input is the input file which contains the input as you described in your question.
Hello Thillai,I think you misunderstood the requirement.Kindly refer it and change your answer.
Try this
grep "`awk '{print $1}' filename | sort -nr | uniq | head -1`" filename | awk '{print $2}'
Sorry Now only I got the requirement. The value in front of the date should be the greatest value means we need to print that date. Am I correct?
---------- Post updated at 11:20 AM ---------- Previous update was at 11:12 AM ----------
Sorry for misunderstanding. Another simple way
max=`sort -k 1 input | cut -d' ' -f 1 | sort -u | tail -n 1`
sed -rn "s/${max} (.*)/\1/p" input
frans
March 18, 2010, 3:20am
6
try this :
uniq -Dw4 infile | cut -d' ' -f2
This is not giving the desired output.
Here the dates
28/Feb/2008
27/Feb/2008
20/Feb/2008
only having the greatest value in front of it(which is 436).
So dates
16/Feb/2008
01/Feb/2008
should not be get displayed. Please correct it
frans
March 18, 2010, 3:27am
8
Apologizes :
MAX=$(sort -r infile | head -n1 | cut -d' ' -f1)
grep "$MAX" infile | cut -d' ' -f2