Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2003
    Posts
    4

    Unanswered: Sorting By Date Recursively

    Hi gurus, I know this is a piece of cake for you guys but I'm just a novice and I'm trying to figure out how to read a list of files contained within a text file a the list them by date.
    The files are in diferent directories and what I need is to order then compared by date to each other even if they are in diferent directories.

    This is It:
    cat list.txt
    dir1/file1
    dir1/file2
    dir2/file3
    dir2/file4

    ls -l /dir1
    -rw-r--r-- 1 usr grp 649 Oct 11 15:40 file1
    -rw-r--r-- 1 usr grp 11781 Oct 13 15:40 file2

    ls -l /dir2
    -rw-r--r-- 1 usr grp 222 Oct 12 15:40 file3
    -rw-r--r-- 1 usr grp 345 Oct 16 15:40 file4

    what I need is to read list.txt, list the files and order them by date like this:
    -rw-r--r-- 1 usr grp 649 Oct 11 15:40 file1
    -rw-r--r-- 1 usr grp 222 Oct 12 15:40 file3
    -rw-r--r-- 1 usr grp 11781 Oct 13 15:40 file2
    -rw-r--r-- 1 usr grp 345 Oct 16 15:40 file4


    I've been reading and I think I have to use "while read" or awk's getline to read the file line by line.

    Any help will be really appreciated.
    Thanks a lot Guys.

    By the way, xargs ls -ltr <list.txt won't work. There are more than 2k files in the list.txt file.

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Lightbulb

    Try this:

    #!/bin/ksh
    cat /dev/null >/tmp/list.tmp
    for f in $(cat list.txt)
    do
    ls -l $f|awk '{m=int(index("JanFebMarAprMayJunJulAugSepOctNovDe c",$6)/3)+1;
    if (index($8,":") > 0) {y=2003; h=$8;} else {y=2002; h="00:00";}
    printf "%4s%3s-%2s-%s %s\n",y,m,$7,h,$0;}' >>/tmp/list.tmp
    done
    sort -o newList.txt /tmp/list.tmp

    Last edited by LKBrwn_DBA; 10-17-03 at 19:26.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Oct 2003
    Posts
    4
    Originally posted by LKBrwn_DBA
    Try this:

    #!/bin/ksh
    cat /dev/null >/tmp/list.tmp
    for f in $(cat list.txt)
    do
    ls -l $f|awk '{m=int(index("JanFebMarAprMayJunJulAugSepOctNovDe c",$6)/3)+1;
    if (index($8,":") > 0) {y=2003; h=$8;} else {y=2002; h="00:00";}
    printf "%4s%3s-%2s-%s %s\n",y,m,$7,h,$0;}' >>/tmp/list.tmp
    done
    sort -o newList.txt /tmp/list.tmp

    Thanks a lot man, it isnt working but its a good start point. Ill try to modify it and work on it.

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Question

    But it does work, look at this:

    $ cat m0
    #!/bin/ksh
    cat /dev/null >/tmp/list.tmp
    for f in $(cat list.txt)
    do
    ls -l $f
    ls -l $f|awk '{m=int(index("JanFebMarAprMayJunJulAugSepOctNovDe c",$6)/3)+1;
    if (index($8,":") > 0) {y=2003; h=$8;} else {y=$8; h="00:00";}
    printf "%4s%3s-%2s-%s %s\n",y,m,$7,h,fn;}' fn=$f >>/tmp/list.tmp
    done
    sort -o newList.txt /tmp/list.tmp
    cat newList.txt
    $#---EXECUTION:
    $ m0
    -rw-r--r-- 1 oracle dba 2664 Oct 20 05:30 /usr/production/mktg/tmp/s
    ynch_1_3_ALL.tmp
    -rw-r--r-- 1 oracle dba 800213 Jun 18 12:03 /usr/production/mktg/tmp/s
    ol.lst
    -rw-r--r-- 1 oracle dba 152 Oct 2 07:03 /usr/production/mktg/tmp/e
    d_email_1050520018315.msg
    -rw-r--r-- 1 oracle dba 343 Oct 15 18:25 /usr/production/mktg/dat/O
    rigFile.txt
    -rw-r--r-- 1 oracle dba 42761 Oct 4 2001 /usr/production/mktg/dat/a
    rp_011002.csv
    -rw-r--r-- 1 oracle dba 54514 Mar 28 2002 /usr/production/mktg/dat/A
    RPS_FY03.csv
    -rw-r--r-- 1 oracle dba 1095967 Oct 29 2002 /usr/production/mktg/dat/p
    rism_codes.dat
    -rw-r--r-- 1 oracle dba 122160128 Sep 15 11:22 /usr/production/mktg/dat
    /expDM030915.dmp
    #--- SORTED OUTPUT:
    2001 10- 4-00:00 /usr/production/mktg/dat/arp_011002.csv
    2002 3-28-00:00 /usr/production/mktg/dat/ARPS_FY03.csv
    2002 10-29-00:00 /usr/production/mktg/dat/prism_codes.dat

    2003 6-18-12:03 /usr/production/mktg/tmp/sol.lst
    2003 9-15-11:22 /usr/production/mktg/dat/expDM030915.dmp
    2003 10- 2-07:03 /usr/production/mktg/tmp/ed_email_1050520018315.msg
    2003 10-15-18:25 /usr/production/mktg/dat/OrigFile.txt
    2003 10-20-05:30 /usr/production/mktg/tmp/synch_1_3_ALL.tmp
    $

    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  5. #5
    Join Date
    Oct 2003
    Posts
    4
    This is what I'm getting:
    more newList.txt
    2002 1- 6-00:00 -rw-rw-r-- 1 nobody user 302844 Jan 6 2003 /data1/kmz2000/hzbf04341.w3
    2002 1- 6-00:00 -rw-rw-r-- 1 nobody user 350572 Jan 6 2003 /data1/kmz2000/hzbf04341.w1
    2002 1-10-00:00 -rw-rw-r-- 1 nobody user 457772 Jan 10 2003 /data1/kmz2000/hzbf05634.w1
    2002 1-10-00:00 -rw-rw-r-- 1 nobody user 627236 Jan 10 2003 /data1/kmz2000/hzbf05634.w3
    2002 1-14-00:00 -rwxrwxrwx 1 mjcorrea user 31080 Jan 14 2003 /data1/kmz2000/copicceb.dts
    2002 1-14-00:00 -rw-rw-r-- 1 nobody user 1831214 Jan 14 2003 /data1/kmz2000/FALLAS_FINALES_KMZ

    The first problem is with those files from 2001 and before, so I need to figure out how to fix that.

    more list.tmp
    2002 5-30-00:00 -rw-rw-r-- 1 ow03 user 470912 May 30 2001 /data1/bacab3d/BTPKS.dts
    2002 5-30-00:00 -rw-rw-r-- 1 ow03 user 24100 May 30 2001 /data1/bacab3d/BTPKS.mcf
    2002 5-30-00:00 -rw-rw-r-- 1 ow03 user 3080632 May 30 2001 /data1/bacab3d/BTPKSMTS.dts

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Exclamation

    The bug has been corrected in the second script with this statement:

    ... else {y=$8; h="00:00";}


    Try it again!



    PS: Unfortunately for previous years you loose the 'hours' timestamp.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  7. #7
    Join Date
    Oct 2003
    Posts
    4

    Thumbs up

    Ok, now it works fine. You r good with awk. Thank You Very Much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •