Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello UNIX Guru's,
I'm in need of your valuable help regarding the issues i'm having.
Brief Description:
-------------------I have put together a little script to count the files based on the date and display the result in the format of "Month Day Number of files".
Please find the below script which is working and the results are per my expectations.
SCRIPT NUMBER-1:
---------------
THIS IS WORKING:
user@servername $ ls -lt | grep "^-" | awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s\t%d\n", date, freq[date] }'
+ ls -lt
+ awk {Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s\t%d\n", date, freq[date] }
+ grep ^-
Dec 13 1
Feb 21 1
Dec 3 2
Feb 3 1
Dec 16 1
Feb 29 2
Feb 9 1
-----------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------SCRIPT NUMBER-2:
----------------
THIS IS WORKING AND THE RESULTS ARE SORTED ALPHABETICALLY, but the results below are not as expected as i tried different ways to sort the below results as per the MONTH's sequence [Like Jan, Feb, Mar, Apr, May ... Dec].
PLEASE HELP ME IN FINDING THE RESULT sorting the Months from Jan till Dec.$ ls -lt | grep "^-" | awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s\t%d\n", date, freq[date] }'|sort -n
Apr 14 3
Apr 16 1
Apr 25 2
Apr 28 1
Apr 29 1
Apr 8 50
Aug 11 1
Aug 12 1
Aug 13 1
Aug 19 3
Aug 20 1
Aug 28 1
Aug 4 1
Aug 7 1
Dec 13 1
Dec 16 1
Dec 3 2
Feb 21 1
-------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------SCRIPT NUMBER-3:
----------------
I have assigned a variable called 'a' to the above one-liner and executed it and echo'd the variable 'a', but i got the results as below which i don't want in a single line. I wanted to see the results line by line, i tried different ways but couldn't accomplish that. so i slightly modified my script as in SCRIPT NUMBER-4. Please find my modified script as below.$ a=`ls -lt | grep "^-" | awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s\t%d\n", date, freq[date] }'|sort -n`; echo $a
Apr 14 3 Apr 16 1 Apr 25 2 Apr 28 1 Apr 29 1 Apr 8 50 Aug 11 1 Aug 12 1 Aug 13 1 Aug 19 3 Aug 20 1 Aug 28 1 Aug 4 1 Aug 7 1 Dec 13 1 Dec 16 1 Dec 3 2 Feb 21 1 Feb 29 2 Feb 3 1 Feb 9 1 Jan 13 3 Jan 19 9 Jan 21 1 Jan 30 1 Jul 1 2 Jul 13 2 Jul 17 2 Jul 21 1 Jul 22 4 Jul 23 1 Jul 28 1 Jul 29 1 Jul 30 4 Jul 31 1
------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------SCRIPT NUMBER-4:
----------------
I have separated the results with 2 pipes and month and date referring to the number of files as [MONTH DATE -> NUMBER OF FILES]. I'm some what satisfied with this, but i still wanted this results sorted line by line which looks clean and easy to figure out the files. Since i'm not the only one loking into these results, i wanted this to be more user friendly by sorting the results line by line. Can you please help me with this.$ a=`ls -lt | grep "^-" | awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s -> %d file(s) ||\t", date, freq[date] }'`; echo $a
Dec 13 -> 1 file(s) || Feb 21 -> 1 file(s) || Dec 3 -> 2 file(s) || Feb 3 -> 1 file(s) || Dec 16 -> 1 file(s) || Feb 29 -> 2 file(s) || Feb 9 -> 1 file(s) || Jan 21 -> 1 file(s) || Jan 30 -> 1 file(s) || Jan 13 -> 3 file(s) ||
--------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------SCRIPT NUMBER-5:
----------------THE last and the final major issue i'm having is when i use ssh in the script to login to other box [where the ssh keys are set correctly and i have verified ssh with other scripts which is working perfectly with out any issues]
i'm seeing a syntax error. Can you also please help me in resolving this.$ $ a=`ssh -q user@servername.net "ls -lt | grep "^-" | awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s -> %d file(s) ||\t", date, freq[date] }'"`; echo $a
ksh: syntax error: `(' unexpected
--------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------

On script #2: the ending sort command should look like this:
sort -k 1,1M -k 2,2n
The above sorts by the first field where the M flag forces a sort by a 3 character month. Then sort by the second field forcing a numeric sort.

Thanks for your reply, i will try your sort command and will keep you posted regarding the updates.
Thanks
Shashi.

Hi Nails,
Wonderful. The sort command works like a charm.
I have solved the last script5 by using eval command and it works pretty good for me.
"a=`eval ssh -q user@servername \"ls -lt \~/core_files|grep -i "^-"|awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s -> %d file(s) ||\t", date, freq[date] }'\"`; echo $a". Can you plz let me know if you have any other alternate way of doing this?
$ D {for (date in freq) printf "%s\t%d\n", date, freq[date] }'|sort -k 1,1M -k 2,2n <
Jan 27 2
Jan 28 2
Mar 24 2
Apr 30 5
May 1 3
May 15 2
May 21 7
May 27 3
Jun 1 3
Jun 25 6
Aug 7 1
Thanks

I have modified my script and added some perl one liner and sed to parse the output line by line.
$ a=`ls -lt ~/core_files| grep "^-" | awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s -> %d files(s) || \n", date, freq[date] }'`; echo $a|perl -pi -e 's/\|\| /\n/g'|sed 's/\|\|//g'
May 21 -> 6 files(s)
May 27 -> 6 files(s)
Jun 1 -> 22 files(s)BUT,
if i use the perl and sed inside my backquotes, i'm not seeing the output line by line, any ideas how to achieve this?
Appreciate your quick response on this.
NOT WORKING AS EXPECTED:
------------------------------------------------
$ a=`ls -lt ~/core_files| grep "^-" | awk '{Date_count=$6" "$7; freq[Date_count]++}END {for (date in freq) printf "%s -> %d files(s) || \n", date, freq[date] }'|perl -pi -e 's/\|\| /\n/g'|sed 's/\|\|//g'`; echo $a
May 21 -> 6 files(s) May 27 -> 6 files(s) Jun 1 -> 22 files(s)
Thanks
Shashi.

First, I am not much of a perl programmer, but I would use the warning option:
perl -wpi -e .....
Second, try surrounding the a variable with double quotes during the echo:
echo "$a"
If you perform the - echo "$a" - in script 3, you can probably throw away that perl and sed stuff you are using.

Very good solution for the issue i'm having. I will never forget this.
Thanks Nail, you are really very helpful in resolving the issue i have been having.
Thanks
Shashi.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |