1832615 Members
3244 Online
110043 Solutions
New Discussion

Re: sort

 
SOLVED
Go to solution
javedk
Advisor

sort

I am trying to sort the following lines with date filed. The issue I am facing is that some of the lines date is 5th field and some it's 6th. Could some one tell me how I can do a sort in this case.

rw-r--r-- cma dba 2217912320 10/29/05 10:39:10 Oct_29_2005_1010.tar
-rw-r--r-- cma dba 183522816 11/05/05 22:51:18 Nov_5_2005_2250.tar
-rw-r--r-- cma dba 0.553 Gb 06/22/05 23:34:35 Jun_22_2005_2330.tar
9 REPLIES 9
LiPEnS
Valued Contributor

Re: sort

Hi
try this:
#ll -t
:-)
or
|awk '{print $(NF-2)" "$(NF-1)" "$0}' |sort -n

Regards
Sยภเl Kย๓คг
Respected Contributor

Re: sort

Hi,
Use #ls -lrt
This will list the files with date and time sorted in ascending order
Regards,
Sunil
Your imagination is the preview of your life's coming attractions
Bill Hassell
Honored Contributor

Re: sort

The standard Unix ls -l will show the time in detail up to about a year and then it switches to a less detailed date. However, the -t option will sort the ls -l output correctly with no need to use sort. Use -r to reverse the sort direction from newst first to oldest first.


Bill Hassell, sysadmin
Raj D.
Honored Contributor

Re: sort

Hi Javedk,

You can use

# ll -rt , to get automatically sorted as per date/time.

Hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
javedk
Advisor

Re: sort

Hi,
This is not an ls -l output. This is an output from omnidb command.

My requirement is to sort the lines using date field and the date happens to be in field 5 in first two lines and field 6 in last line in the above sample becuase the size of the file is in GB.

The real file has lot of entries and the date appears only in feild 5 and field 6.
Muthukumar_5
Honored Contributor

Re: sort

Use like:

sort -nk 5,6 file

Example:

# cat file1
rw-r--r-- cma dba 2217912320 10/29/05 10:39:10 Oct_29_2005_1010.tar
-rw-r--r-- cma dba 183522816 11/05/05 22:51:18 Nov_5_2005_2250.tar
rw-r--r-- cma dba 2217912320 10/25/05 10:39:10 Oct_29_2005_1010.tar
-rw-r--r-- cma dba 0.553 Gb 06/22/05 23:34:35 Jun_22_2005_2330.tar
-rw-r--r-- cma dba 0.553 Gb 03/22/05 23:34:35 Jun_22_2005_2330.tar
# sort -nk 5,6 file1
-rw-r--r-- cma dba 0.553 Gb 03/22/05 23:34:35 Jun_22_2005_2330.tar
-rw-r--r-- cma dba 0.553 Gb 06/22/05 23:34:35 Jun_22_2005_2330.tar
rw-r--r-- cma dba 2217912320 10/25/05 10:39:10 Oct_29_2005_1010.tar
rw-r--r-- cma dba 2217912320 10/29/05 10:39:10 Oct_29_2005_1010.tar
-rw-r--r-- cma dba 183522816 11/05/05 22:51:18 Nov_5_2005_2250.tar

hth.
Easy to suggest when don't know about the problem!
LiPEnS
Valued Contributor
Solution

Re: sort

cat file1 |awk '{printf $1" "$2" "$3" "$4; if (NF == 7) print " "$5" "$6" "$7; else print $5" "$6" "$7" "$8}'|sort -k 5,6

Regards
javedk
Advisor

Re: sort

Thank LiPEns,

It works great.
javedk
Advisor

Re: sort

Thanks LiPEns,

It works great.