[Cialug] Display file contents either with less or cat based on number of lines

Tim Wilson tim_linux at wilson-home.com
Tue Oct 25 11:58:29 CDT 2011


Maybe I'm missing the point of the original post, but it says "Display file
contents ...".  If what you want to display is already a file, why copy the
contents of the file to a temporary file (especially using awk to do the
copy), just to run wc on it, and either cat or less on it?  This works just
as well, and you don't have to redirect stdin:
out() { if [ $(wc -l $1 | awk '{print $1}') -gt $(tput lines) ]; then less
$1; else cat $1; fi; }

If what's desired is run cat or less on the output of some command, then
this command covers both files and pipes:

out() { if [ $# -gt 0 ];then tmp=$*; else tmp=$(mktemp /tmp/XXXXXX); cat
>|$tmp; fi; if [ $(wc -l $tmp | awk '{print $1}') -gt $(tput lines) ]; then
less $tmp; else cat $tmp; fi; if [ $# -eq 0 ];then rm -fr $tmp; fi; }

Then both of these will work:
$ out foo.txt
$ grep 'some string' foo.txt | out

I added a template to the mktemp command because some older linuxes expect a
template.  I also added a '|' on the redirect because I have my bash set
such that it won't overwrite an existing file on redirect (which is really
handy if you meant to type "<foo.txt" but instead typed ">foo.txt").


On Tue, Oct 25, 2011 at 9:30 AM, Paul Gray <gray at cs.uni.edu> wrote:

> On 10/25/2011 09:21 AM, Scott Prader wrote:
>
>> Could probably write a wrapper for cat (maybe call it dog) and have it
>> reference /etc/mine.types and/or the file command so that once it
>> understands the 'scent' of the file, it will know how to process the
>> file based off of the preferred application for it.  For instance, if
>> you ran the command:
>>
>> dog kitty.jpg
>>
>> , it should know to figure out that it's an image file (file kitty.jpg),
>> check the environment (is ${DISPLAY} set?), figure out which application
>> to use (cross reference with mime.types depending on the environment),
>> and run the appropriate command to display the image.
>>
>
> On a Mac, isn't this just the "open" command?
>
> --
> Paul Gray                                         -o)
> 314 East Gym, Dept. of Computer Science           /\\
> University of Northern Iowa                      _\_V
>  Message void if penguin violated ...  Don't mess with the penguin
>  No one says, "Hey, I can't read that ASCII attachment ya sent me."
>
> ______________________________**_________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/**listinfo/cialug<http://cialug.org/mailman/listinfo/cialug>
>



-- 
Tim
Required reading: http://bccplease.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cialug.org/pipermail/cialug/attachments/20111025/e3e77f0e/attachment.html>


More information about the Cialug mailing list