Display filenames longer than 'n' characters?

Display filenames longer than 'n' characters?

Post by Seymour Dup » Sun, 14 Nov 1999 04:00:00



I would like to display filenames longer than 'n' characters.

Tech support blew me off by saying, "Use AWK."  I came up with:

ls -l | awk _______

but I can't figure out the "_________".  Any ideas, or should I use
something else?

John

--
  If You Always Do the Things You've Done,
  You'll Always Have the Things You Got.

 
 
 

Display filenames longer than 'n' characters?

Post by Drew Bea » Sun, 14 Nov 1999 04:00:00


No, you shouldn't use something else.  Awk is perfect for your task.
But, before I answer your question, I think it would be a good idea for
you to learn awk, since you do use unix (or a variant thereof).

Here are some good places to start:

http://www.gnu.org/manual/gawk/html_chapter/gawk_toc.html
http://www.faqs.org/faqs/computer-lang/awk/faq/

Any questions you can't find the answer to should be directed to
comp.lang.awk.

And here's the answer to your qusetion:

ls -l | awk '{ if (length($NF) > n) { print $NF } }'


> I would like to display filenames longer than 'n' characters.

> Tech support blew me off by saying, "Use AWK."  I came up with:

> ls -l | awk _______

> but I can't figure out the "_________".  Any ideas, or should I use
> something else?

> John

> --
>   If You Always Do the Things You've Done,
>   You'll Always Have the Things You Got.

--


 
 
 

Display filenames longer than 'n' characters?

Post by Charles Dem » Sun, 14 Nov 1999 04:00:00




Quote:

>I would like to display filenames longer than 'n' characters.

>Tech support blew me off by saying, "Use AWK."  I came up with:

>ls -l | awk _______

>but I can't figure out the "_________".  Any ideas, or should I use
>something else?

if your filenames have no spaces, tabs, or newlines in them,

ls -l | awk 'length($NF) > 7 {print}'

would print the lines of ls -l that have the last field longer than
7 characters.  Change the "7" to whatever n you want.

this cam be written shorter as:

ls -l | awk 'length($NF) > 7'

but you could have gotten just the filenames using grep too.
These get filenames (even with spaces or tabs) greater than 7
characters.

ls | grep '\{8,\}'
ls | grep -v '\{1,7\}'
ls | awk 'length($0) >7'
ls | awk 'length > 7'

note that this doesn't use ls -l

BTW, you could have done this with just ls too

ls -l ????????

will output files with 8 characters or more, but it will do
subdirectories too, if they have 8 or more characters.

HTH,

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

Display filenames longer than 'n' characters?

Post by Charles Dem » Sun, 14 Nov 1999 04:00:00




Quote:>No, you shouldn't use something else.  Awk is perfect for your task.
>But, before I answer your question, I think it would be a good idea for
>you to learn awk, since you do use unix (or a variant thereof).

>Here are some good places to start:

>http://www.gnu.org/manual/gawk/html_chapter/gawk_toc.html
>http://www.faqs.org/faqs/computer-lang/awk/faq/

>Any questions you can't find the answer to should be directed to
>comp.lang.awk.

>And here's the answer to your qusetion:

>ls -l | awk '{ if (length($NF) > n) { print $NF } }'

overkill if you only want the filenames, this will do that:

ls | awk 'length > n'

and what you wrote is too much anyway, this is better awk form:

ls -l | awk 'length($NF) > n {print $NF}'

and to print the whole line:

ls -l | awk 'length($NF) > n'

Your references were good, but you might have also mentioned
the comp.lang.awk newsgroup.  :-)

Chuck Demas
Needham, Mass.


>> I would like to display filenames longer than 'n' characters.

>> Tech support blew me off by saying, "Use AWK."  I came up with:

>> ls -l | awk _______

>> but I can't figure out the "_________".  Any ideas, or should I use
>> something else?

>> John

>> --
>>   If You Always Do the Things You've Done,
>>   You'll Always Have the Things You Got.

>--


--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

Display filenames longer than 'n' characters?

Post by Craig Peterei » Sun, 14 Nov 1999 04:00:00



> I would like to display filenames longer than 'n' characters.

Try the following, and replace 4 with the number of characters:

bash$ /bin/ls | grep '^.\{4\}'

bash$ /bin/ls | grep '^.\{4\}' | xargs ls -dlF
                                           ^^^ +/- any other options

The -d is necessary so you don't display the contents of directories.

HTH,

--
Craig Peterein

 
 
 

Display filenames longer than 'n' characters?

Post by Martin Rams » Sun, 14 Nov 1999 04:00:00



Quote:> but you could have gotten just the filenames using grep too.
> These get filenames (even with spaces or tabs) greater than 7
> characters.

> ls | grep '\{8,\}'
> ls | grep -v '\{1,7\}'

These regular expressions miss the an important dot:
  ls | grep '.\{8,\}'

The second one also misses the anchors for start and end:
  ls | grep -v '^.\{1,7\}$'

Or do you have some other flavour of grep command?  I'm using the
plain grep of Solaris 2.6.

Using a long list, one could use:

  ls -Ll | grep '[^ ]\{8,\}$'
  ls -Ll | grep -v ' [^ ]\{1,7\}$'

Option -L is to avoid the display of symbolic links.  These commands
only work as intended, if there are no spaces within filenames ...

Quote:> BTW, you could have done this with just ls too

> ls -l ????????

> will output files with 8 characters or more, but it will do
> subdirectories too, if they have 8 or more characters.

This is missing the trailing asterisk!  And the option -d will avoid
listing of subdirectories:

  ls -dl ????????*

Regards,
  Martin
--

PGP KeyID=0xE8EF4F75 FiPr=5244 5EF3 B0B1 3826  E4EC 8058 7B31 3AD7

 
 
 

Display filenames longer than 'n' characters?

Post by Charles Dem » Sun, 14 Nov 1999 04:00:00





>> but you could have gotten just the filenames using grep too.
>> These get filenames (even with spaces or tabs) greater than 7
>> characters.

>> ls | grep '\{8,\}'
>> ls | grep -v '\{1,7\}'

>These regular expressions miss the an important dot:
>  ls | grep '.\{8,\}'

>The second one also misses the anchors for start and end:
>  ls | grep -v '^.\{1,7\}$'

>Or do you have some other flavour of grep command?  I'm using the
>plain grep of Solaris 2.6.

No, I was just sloppy.  I would have caught it if I were actually
doing it, but I didn't test them.

- Show quoted text -

Quote:

>Using a long list, one could use:

>  ls -Ll | grep '[^ ]\{8,\}$'
>  ls -Ll | grep -v ' [^ ]\{1,7\}$'

>Option -L is to avoid the display of symbolic links.  These commands
>only work as intended, if there are no spaces within filenames ...

>> BTW, you could have done this with just ls too

>> ls -l ????????

>> will output files with 8 characters or more, but it will do
>> subdirectories too, if they have 8 or more characters.

>This is missing the trailing asterisk!  And the option -d will avoid
>listing of subdirectories:

>  ls -dl ????????*

Again I must apologize for the sloppiness.  Today doesn't seem to be my
day.  At least I had the concepts right, if not the exact execution. :-)

Chuck Demas
Needham, Mass.

>Regards,
>  Martin
>--

>PGP KeyID=0xE8EF4F75 FiPr=5244 5EF3 B0B1 3826  E4EC 8058 7B31 3AD7

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

Display filenames longer than 'n' characters?

Post by Martin Rams » Sun, 14 Nov 1999 04:00:00



Quote:> Again I must apologize for the sloppiness.  Today doesn't seem to be my
> day.  At least I had the concepts right, if not the exact execution. :-)

:-)  I know these days quite well. :-)
Sorry, if my answers were a little bit brief.

Regards,
  Martin
--

PGP KeyID=0xE8EF4F75 FiPr=5244 5EF3 B0B1 3826  E4EC 8058 7B31 3AD7

 
 
 

Display filenames longer than 'n' characters?

Post by Seymour Dup » Mon, 15 Nov 1999 04:00:00


Thanks to all who responded.  I now have *several* ways of doing it.
And also to Drew Beal who showed me where to learn awk - an example of,
"Give a man a fish, feed him for a day,
 Teach hin how to fish, feed him for life."

John


> I would like to display filenames longer than 'n' characters.
> Tech support blew me off by saying, "Use AWK."  I came up with:
> ls -l | awk _______
> but I can't figure out the "_________".  Any ideas, or should I use
> something else?
> John
> --
>   If You Always Do the Things You've Done,
>   You'll Always Have the Things You Got.

--
  If You Always Do the Things You've Done,
  You'll Always Have the Things You Got.
 
 
 

Display filenames longer than 'n' characters?

Post by Matthew Land » Tue, 16 Nov 1999 04:00:00


--
_______________________________________________________________________

      Comments, views, and opinions are mine alone, not IBM's.



> > I would like to display filenames longer than 'n' characters.

> Try the following, and replace 4 with the number of characters:

> bash$ /bin/ls | grep '^.\{4\}'

> bash$ /bin/ls | grep '^.\{4\}' | xargs ls -dlF
>                                            ^^^ +/- any other options

> The -d is necessary so you don't display the contents of directories.

> HTH,

> --
> Craig Peterein


Whup, and an added shell version with only ls as an external command:

ls |while read line
do
  [[ ${#line} -gt 7 ]] && print $line
done

 
 
 

Display filenames longer than 'n' characters?

Post by Ken Pizzi » Wed, 17 Nov 1999 04:00:00



>for filename in *
>do
>    case "$filename" in
>        ???????*) echo $filename ;;
>        *) ;;
>    esac
>done

>Which, can of course be expressed more simply as:

>    echo ???????*

Except that the output is different in the two cases; how about:
 for filename in ???????*; do echo $filename; done

                --Ken Pizzini

 
 
 

Display filenames longer than 'n' characters?

Post by Dennis Ester » Mon, 29 Nov 1999 04:00:00



> Or, for those of us who think that using ksh for scripting is just
> as bad as using csh,

I only script with borne shell but just out of habit. Why would
I not want to use ksh or csh?

Dennis.

.sig: Not Found

 
 
 

Display filenames longer than 'n' characters?

Post by Christopher J. Matter » Mon, 29 Nov 1999 04:00:00




>> Or, for those of us who think that using ksh for scripting is just
>> as bad as using csh,
> I only script with borne shell but just out of habit. Why would
> I not want to use ksh or csh?

csh: several reasons, including frustrating inability to quote
things correctly and severely limited ability to manipulate
file descriptors and failure to properly shortcircuit boolean
expressions and...

ksh: y'got me.  I love programming in ksh; it's my favorite shell.

                            Chris Mattern

 
 
 

Display filenames longer than 'n' characters?

Post by Walter Smit » Tue, 30 Nov 1999 04:00:00


Amen!!

When I started, I also had to make a choice. I tried some things in
each and learned to steer clear of csh. The ksh is essentially a
super-set of Borne shell and for scripting, thats good!

"...the Korn Shell includes nearly all the features of the Bourne
Shell. In addition, the Korn Shell provides report formatting
capabilities, built in arithmentic, data types, control flow, and other
features that greatly improve its usefulness as a programming
language." Mark G. Sobell, Unix System V: A Practical Guide, 1985.

HOWEVER: the Unix utility "awk" has an easier programming language and
can do many things - and executes LOTS faster - I have rewritten some
things that were once in ksh and now run very fast in awk.

AND thats not all - Perl is essentially a super set of awk and also
runs very fast.

If I were doing it over - I would learn the basics of Korn Shell -
because sometimes you just need a shell script! - then move on to Perl
ASAP, bypassing awk.

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!