dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

12-hour ls listing - shell

This is a discussion on 12-hour ls listing - shell ; Greetings, I've written the following script to make an even more humanly readable "ls -htr" command by displaying the 12-hour time instead of military time. ls12: #!/usr/local/bin/bash touch /tmp/input.sed # First get a list of all the files which were ...


Home > Database Forum > Operating Systems > shell > 12-hour ls listing

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 11-13-2008, 02:46 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default 12-hour ls listing

Greetings,

I've written the following script to make an even more humanly
readable "ls -htr" command by displaying the 12-hour time instead of
military time.

ls12:
#!/usr/local/bin/bash
touch /tmp/input.sed
# First get a list of all the files which were modified after noon
ls -lhtr | awk 'substr($8,1,2)>12 {print substr($8,1,2)}'|sort -u > /
tmp/search
# Then replace them with their 12-hour counterparts
while read searchterm; do
(( replaceterm = searchterm - 12 ))
echo "s/$searchterm:\([0-9][0-9]\)/$replaceterm:\1P/" >> /tmp/
input.sed
done < /tmp/search
ls -lhtr | sed -f /tmp/input.sed
# cleanup
rm /tmp/input.sed /tmp/search

It works okay, however, the problem is that it relies on temp files
which I would prefer to avoid. Also, I'm sure that someone who knows
sed or awk better than I would be able to make it more efficient.
It's likely that these lines can be combined in a one-liner sed or awk
script.

Any ideas or suggestions would be much appreciated.

TIA.
Reply With Quote
  #2  
Old 11-13-2008, 03:07 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: 12-hour ls listing


wrote in message
news:4eaff73b-f040-401b-a470-617c27542720@40g2000prx.googlegroups.com...

> I've written the following script to make an even more humanly
> readable "ls -htr" command by displaying the 12-hour time instead of
> military time.


Did you say "military"? You must mean a 24-hour clock. We humans use a
24-hour clock to make sense of time displays and remove the am/pm ambiguity
that is the scourge of the lay-person's need for a 12-hour clock which
inevitably leads to problems with date math and communications..

You're wasting energy solving a problem that doesn't even exist. We always
make sure that all clocks on all instrumentation are set to use 24-hour
format.


Reply With Quote
  #3  
Old 11-13-2008, 03:16 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: 12-hour ls listing

On Thursday 13 November 2008 19:46, jaredsubman@yahoo.com wrote:

> Greetings,
>
> I've written the following script to make an even more humanly
> readable "ls -htr" command by displaying the 12-hour time instead of
> military time.
>
> ls12:
> #!/usr/local/bin/bash
> touch /tmp/input.sed
> # First get a list of all the files which were modified after noon
> ls -lhtr | awk 'substr($8,1,2)>12 {print substr($8,1,2)}'|sort -u > /
> tmp/search
> # Then replace them with their 12-hour counterparts
> while read searchterm; do
> (( replaceterm = searchterm - 12 ))
> echo "s/$searchterm:\([0-9][0-9]\)/$replaceterm:\1P/" >> /tmp/
> input.sed
> done < /tmp/search
> ls -lhtr | sed -f /tmp/input.sed
> # cleanup
> rm /tmp/input.sed /tmp/search
>
> It works okay, however, the problem is that it relies on temp files
> which I would prefer to avoid. Also, I'm sure that someone who knows
> sed or awk better than I would be able to make it more efficient.
> It's likely that these lines can be combined in a one-liner sed or awk
> script.


The output of ls -l is locale-dependent (compare eg LC_ALL=C ls -l and
LC_ALL=en_US ls -l). Can you paste an example output of ls -l that you see
on your system, to get an idea of the input data the script has to operate
on?
Reply With Quote
  #4  
Old 11-13-2008, 03:39 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: 12-hour ls listing

On Nov 13, 1:16*pm, pk wrote:
> On Thursday 13 November 2008 19:46, jaredsub...@yahoo.com wrote:
>
>
>
> > Greetings,

>
> > I've written the following script to make an even more humanly
> > readable "ls -htr" command by displaying the 12-hour time instead of
> > military time.

>
> > ls12:
> > #!/usr/local/bin/bash
> > touch /tmp/input.sed
> > # *First get a list of all the files which were modified after noon
> > ls -lhtr | awk 'substr($8,1,2)>12 {print substr($8,1,2)}'|sort -u > /
> > tmp/search
> > # *Then replace them with their 12-hour counterparts
> > while read searchterm; do
> > * * * * (( replaceterm = searchterm - 12 ))
> > * * * * echo "s/$searchterm:\([0-9][0-9]\)/$replaceterm:\1P/" >> /tmp/
> > input.sed
> > done < /tmp/search
> > ls -lhtr | sed -f /tmp/input.sed
> > # *cleanup
> > rm /tmp/input.sed /tmp/search

>
> > It works okay, however, the problem is that it relies on temp files
> > which I would prefer to avoid. *Also, I'm sure that someone who knows
> > sed or awk better than I would be able to make it more efficient.
> > It's likely that these lines can be combined in a one-liner sed or awk
> > script.

>
> The output of ls -l is locale-dependent (compare eg LC_ALL=C ls -l and
> LC_ALL=en_US ls -l). Can you paste an example output of ls -l that you see
> on your system, to get an idea of the input data the script has to operate
> on?


Sure.

ls -l:

-rwxr-xr-x 1 root wheel 275 Sep 1 13:15 file1
-rwxr-xr-x 1 root wheel 395 Sep 3 13:44 file2
-rwxr-xr-x 1 root wheel 557 Sep 5 12:22 file3
-rwxr-xr-x 1 root wheel 224 Sep 9 10:08 file4
-rwxr-xr-x 1 root wheel 1137 Sep 9 14:14 file5
-rwxr-xr-x 1 root wheel 4342 Nov 5 14:17 file6
-rwxr-xr-x 1 root wheel 869 Nov 10 10:42 file7


Reply With Quote
  #5  
Old 11-13-2008, 03:39 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: 12-hour ls listing

At 2008-11-13 02:07PM, "ynotssor" wrote:
>
> wrote in message
> news:4eaff73b-f040-401b-a470-617c27542720@40g2000prx.googlegroups.com...
>
> > I've written the following script to make an even more humanly
> > readable "ls -htr" command by displaying the 12-hour time instead of
> > military time.

>
> Did you say "military"? You must mean a 24-hour clock. We humans use a
> 24-hour clock to make sense of time displays and remove the am/pm ambiguity
> that is the scourge of the lay-person's need for a 12-hour clock which
> inevitably leads to problems with date math and communications..
>
> You're wasting energy solving a problem that doesn't even exist. We always
> make sure that all clocks on all instrumentation are set to use 24-hour
> format.


I agree with ynotssor, but if you want to shoot yourself in the foot,
here's a gun:

ls -lhtr | perl -pe 's/\b(\d\d)\d\d)\b/
($ap,$hr) = $1>12 ? ("P",$1-12) : ("A",$1);
sprintf("%02d:%s%s",$hr,$2,$ap)
/e'

Note that, on solaris at least, if a file is more than 6 months old, the
year is printed instead of the time. In that case, and if your file
happens to match the pattern, then that perl command will actually
disguise the name of the file.

You're also throwing off the layout by introducing an extra character
into the output for some lines:

$ ls -lhtr | perl -pe 's/\b(\d\d)\d\d)\b/
> ($ap,$hr) = $1>12 ? ("P",$1-12) : ("A",$1);
> sprintf("%02d:%s%s",$hr,$2,$ap)
> /e'

total 201K
-rw-r--r-- 1 xx087 users 58K Jan 8 2008 htmltmpl.py
-rw-r--r-- 1 xx087 users 717 Jan 8 2008 _xkcd.tmpl
-rw-r--r-- 1 xx087 users 956 Mar 25 2008 comix.css
-rw-r--r-- 1 xx087 users 712 May 12 2008 comix.css~
-rw-r--r-- 1 xx087 users 997 May 12 2008 _index.tmpl~
-rw-r--r-- 1 xx087 users 35K Aug 29 10:18A fetch.py~
-rw-r--r-- 1 xx087 users 1.5K Aug 29 02:48P Makefile
-rw-r--r-- 1 xx087 users 36K Aug 29 02:49P fetch.py.20080829
-rw-r--r-- 1 xx087 users 2.4K Sep 24 02:28P _index.tmpl
-rw-r--r-- 1 xx087 users 23K Sep 24 02:34P comix.zip
-rw-r--r-- 1 xx087 users 39K Nov 10 09:49A fetch.py
drwxr-xr-x 2 xx087 users 512 Nov 10 09:49A CVS/


Mind you, I also don't like the -h flag. It takes more effort to read
that "35K" is larger that "997", while it's easy to see "35303" is much
bigger that "997". For a while, I used a script that would commify the
size column, but it was a bit fragile and kept mangling the month.

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 01:05 PM.