1835446 Members
2931 Online
110078 Solutions
New Discussion

Advanced ksh shell help.

 
RAC_1
Honored Contributor

Advanced ksh shell help.

I have files as follows and I want to list files that do not have "." as a character in them. Only with ksh/sh

root.old
xync
xdr.cdb
port.ory
port
abc

I tried following.

ll [a-zA-Z]*[!\.][a-zA-Z]*
ll *[!\.]*

and fe other combinations.

Thank you in advance.
There is no substitute to HARDWORK
18 REPLIES 18
Biswajit Tripathy
Honored Contributor

Re: Advanced ksh shell help.

Ofcourse, the easiest answer would be

ll | grep -v '\.'

I would have to think a little more about the
regexp solution.

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: Advanced ksh shell help.

Not sure (needs testing), but the following
seems to work:

ll [a-zA-Z][!\.]

- Biswajit
:-)
RAC_1
Honored Contributor

Re: Advanced ksh shell help.

Bisawajit,

That does not work.
There is no substitute to HARDWORK
Dietmar Konermann
Honored Contributor

Re: Advanced ksh shell help.

This works:

# ll | grep -v '\.'

You need to take care to protect the \. from the shell, e.g. using ''.

Or simply use fgrep:

# ls -l | fgrep -v .
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Cem Tugrul
Esteemed Contributor

Re: Advanced ksh shell help.

RAC,
As Dietmar's advise would be solution.
ls -l | fgrep -v .
good luck,
Our greatest duty in this life is to help others. And please, if you can't
Muthukumar_5
Honored Contributor

Re: Advanced ksh shell help.

ls is using pattern matching instead of regular expression support.

* is the problem in using with ls command.

ll [a-zA-Z]*

it is not meaning that,

only a to z A to Z -- 0 or more times.

It is meaning as,

one a to z or A to Z in the beginning. Anything after that. It is including . 0-9 - + etc in that.

Example:
# ls -l [a-z]
-rw-rw-rw- 1 root sys 0 Nov 23 23:54 a
# ls -l [a-z]*
-rw-rw-rw- 1 root sys 0 Nov 23 23:54 a
-rw-rw-rw- 1 root sys 0 Nov 23 23:35 bye
-rw-rw-rw- 1 root sys 0 Nov 23 23:35 file1.
-rw-rw-rw- 1 root sys 0 Nov 23 23:35 ok.ksh
-rw-rw-rw- 1 root sys 0 Nov 24 01:38 ok_bye
-rw-rw-rw- 1 root sys 0 Nov 23 23:35 test.sh

To remove . then we must not use *. We have to handle it specifically as,

ls -l [a-z][a-z][a-z][!\.]*
ls -l [a-z][a-z][a-z][a-z][!\.]*

To avoid this you can with grep or awk as,

ls -l | grep -v '\.'
ls -l | awk '!/\./ { print; }'

hth.



Easy to suggest when don't know about the problem!
john korterman
Honored Contributor

Re: Advanced ksh shell help.

Hi,

# ls -l | grep -v "[.]"

should also work.

regards,
John K.
it would be nice if you always got a second chance
RAC_1
Honored Contributor

Re: Advanced ksh shell help.

So this can not be explicitely done with [a-zA-Z]*[!.]* or similar pattern matching.

While grep -v will what I want I wanted to use above pattern matching.
There is no substitute to HARDWORK
john korterman
Honored Contributor

Re: Advanced ksh shell help.

Hi again,

I think it will be quite difficult, as the pattern you want to specify should work on file name expansion directly.
This means that if you expand to a directory name and that directory holds a filename like the ones you specify, they are listed and not filtered out, as they would be if the above "grep" examples were used; these will work on the actual output of the commands.
I have tried with this terrible pattern:

# ls !([a-zA-Z0-9]*[.]*[a-za-Z0-9]*|[.]*{a-zA-Z0-9]|[a-zA-Z0-9]*[.]$)

which I hardly understand any longer myself, but if it matches a dir holding one of your files, the file will of course be listed.

regards,
John K.
it would be nice if you always got a second chance
Ivan Ferreira
Honored Contributor

Re: Advanced ksh shell help.

I don't know what do you mean with ksh only, but this will work:

for FILE in `ls`; do echo $FILE | awk -F "." 'NF==1 {print $0}';done


You can then customize the first ls to use the patters that you want.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Muthukumar_5
Honored Contributor

Re: Advanced ksh shell help.

John,

# ls !([a-zA-Z0-9]*[.]*[a-za-Z0-9]*|[.]*{a-zA-Z0-9]|[a-zA-Z0-9]*[.]$)

solution will list all files (including . dotted files).

If we use * with ls it will everything. It is not like [a-z]* means to take a to z 0 to more times. It is taking as one a to z and anything other.

We must not use *. We have to go with specific thing as in my previos reply.

hth.

Easy to suggest when don't know about the problem!
john korterman
Honored Contributor

Re: Advanced ksh shell help.

Hi Muthukumar,

sorry, it should be:

# ls !([a-zA-Z0-9]*[.]*[a-za-Z0-9]*|[.]*{a-zA-Z0-9]|[a-zA-Z0-9]*[.])

and I think that it does not list the names of files with dots in the file name in the *current* directory.
Hoever, as the command also lists directory names, the files under those will be listed as they are and not filtered by grep.
Does the command list dot filenames in the "current directory" on your system?

regards,
John K.
it would be nice if you always got a second chance
RAC_1
Honored Contributor

Re: Advanced ksh shell help.

John,

It does not work. I think 8 makes all difference.

ll !([a-zA-Z0-9]*[.]*[a-za-Z0-9]*|[.]*[a-zA-Z0-9]|[a-zA-Z0-9]*[.])
-r-------- 1 root users 749 Nov 4 10:18 capplan
-r-------- 1 capplan users 398 Jul 9 2004 capplan.old
-r-------- 1 root dba 6303 Nov 22 13:50 oracle
-r-------- 1 oracle dba 4833 Aug 27 2004 oracle.old
-r-------- 1 root dba 0 Apr 12 2005 precise
-r-------- 1 root sys 3819 Jul 23 01:14 root
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: Advanced ksh shell help.

RAC,

We may not get . negated files using ls -l . We have to go to *ls* developers to get those possibility of negating . files without piping to some other utility. May be a classical problem?

If you want to negate only . dotted files then,

# alias lsn='ls -l | grep -v "\."'
# lsn

that is all. ;)

hth.





Easy to suggest when don't know about the problem!
john korterman
Honored Contributor

Re: Advanced ksh shell help.

Hi again,

my example is for the ls command - not ll.

regards,
John K.
it would be nice if you always got a second chance
Muthukumar_5
Honored Contributor

Re: Advanced ksh shell help.

ok.

See this:

# ls
a file1 file1.sh file2 file_ok test.tar
#
#
# ls !([a-zA-Z0-9]*[.]*[a-za-Z0-9]*|[.]*{a-zA-Z0-9]|[a-zA-Z0-9]*[.])
a file1 file1.sh file2 file_ok test.tar
#

Can you clarify here.

hth.
Easy to suggest when don't know about the problem!
john korterman
Honored Contributor

Re: Advanced ksh shell help.

Hi Muthukumar,

maybe I should have said that I executed
# sh
first, and then:

# ls
a file1 file1.sh file.2 file_ok test.tar


# ls !([a-zA-Z0-9]*[.]*[a-za-Z0-9]*|[.]*{a-zA-Z0-0-9]|[a-zA-Z0-9]*[.])
a file1 file_ok

regards,
John K.
it would be nice if you always got a second chance
Peter Nikitka
Honored Contributor

Re: Advanced ksh shell help.

Hi,

how about this:
ls
abc port port.ory root.old xdr.cdb xync

ls -d !(*.*)
abc port xync


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"