Ls help: ls not working with start character search (^) | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Ls help: ls not working with start character search (^)

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-26-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 8 September 2014, 10:33 AM EDT
Posts: 225
Thanks: 143
Thanked 13 Times in 12 Posts
Ls help: ls not working with start character search (^)

Experts,

How to list a file using ^ character, for all files started with character a. (os= hp-ux )


Code:
# ls -l 
-rw-------   1 useradm    users            0 Mar 26 14:30 abc
-rw-------   1 useradm    users            0 Mar 26 14:30 def
-rw-------   1 useradm    users            0 Mar 26 14:33 zyx
#



Code:
Trying but not working, any idea whats wrong, 
# ls -l ^abc
^abc not found

# ls -l ^a*
^a* not found

# ls ^a*
^a* not found



Thanks a lot.

---------- Post updated at 03:09 PM ---------- Previous update was at 02:46 PM ----------

Also can anyone please explain , how the ^ character not working with
Code:
ls

, for regex.




Did not work with ls: # Wanted to list which is not having first letter a .

Code:
Code:
# ls -l [^a]bc
-rw-------   1 useradm   users            0 Mar 26 14:30 abc


Worked with grep: # Wanted to list which is not having first letter a .

Code:
# ls -l | grep "[^a]bc"
-rw-------   1 useradm   users            0 Mar 26 14:48 bbc


Thanks a lot.
Sponsored Links
    #2  
Old 03-26-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 October 2014, 2:56 PM EDT
Location: Amsterdam
Posts: 9,538
Thanks: 284
Thanked 2,422 Times in 2,172 Posts
Try:

Code:
ls -l [!a]bc

The Following User Says Thank You to Scrutinizer For This Useful Post:
rveri (03-27-2013)
Sponsored Links
    #3  
Old 03-26-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 October 2014, 5:13 PM EDT
Location: Saskatchewan
Posts: 19,675
Thanks: 820
Thanked 3,346 Times in 3,134 Posts
Important to note, it is not ls that's doing the search here, it's the shell. If you do echo [!a]bc you'll see similar results.
The Following User Says Thank You to Corona688 For This Useful Post:
rveri (03-27-2013)
    #4  
Old 03-26-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
You can also use:

Code:
ls -l !(a)+(bc)

From KSH manual:

Code:
           ?(pattern-list)     Optionally matches any one of the given
                               patterns.

           *(pattern-list)     Matches zero or more occurrences of the given
                               patterns.

           +(pattern-list)     Matches one or more occurrences of the given
                               patterns.

           @(pattern-list)     Matches exactly one of the given patterns.

           !(pattern-list)     Matches anything, except one of the given
                               patterns.

The Following User Says Thank You to Yoda For This Useful Post:
rveri (03-27-2013)
Sponsored Links
    #5  
Old 03-26-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 October 2014, 4:58 PM EDT
Location: San Jose, CA, USA
Posts: 4,894
Thanks: 182
Thanked 1,644 Times in 1,395 Posts
The key point that rveri seems to be missing is that the shell uses filename pattern matching (not regular expressions) when expanding the arguments that will be passed to ls. Although there are some similarities between pattern matching and regular expression, many of the details are different.
The Following User Says Thank You to Don Cragun For This Useful Post:
rveri (03-27-2013)
Sponsored Links
    #6  
Old 03-27-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 8 September 2014, 10:33 AM EDT
Posts: 225
Thanks: 143
Thanked 13 Times in 12 Posts
Thanks,Scrutinizer, Corona, Yoda First 3 worked.. to list all files NOT starting with a :




# Worked well: Seems matching is passed to ls command: " ls -l [!a]bc "

Code:
# ls -l ab[c]
-rw-------   1 useradm    users            0 Mar 27 12:57 abc


# ls -l [!a]bc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbc

# Worked well: Seems matching is passed to shell.

Code:
# echo [!a]bc 
bbc

This is too worked fine, seems matching is done through shell.

Code:
# ls -l !(a)+(bc)
-rw-------   1 useradm    users            0 Mar 26 15:27 bbc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcabc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc
#


Yoda, thanks for the manual page,

Don thanks for pointing out the difference of use when "shell uses filename pattern matching" & when "Regular expression uses pattern matching".
Is there a way we can figure out which is shell doing, and which is doing by regular expressions.


For example with the below example: How to figure out which portion is shell or regex performing the operation:


Code:
# ls -l bbcbbc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc


# ls -l !([c-z])?[c][b][b][b-d]
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc


echo $SHELL
/usr/bin/ksh
#OS= hp-ux 11.23


Thanks..
Sponsored Links
    #7  
Old 03-27-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 October 2014, 4:58 PM EDT
Location: San Jose, CA, USA
Posts: 4,894
Thanks: 182
Thanked 1,644 Times in 1,395 Posts
Quote:
Originally Posted by rveri View Post
... ... ...

Don thanks for pointing out the difference of use when "shell uses filename pattern matching" & when "Regular expression uses pattern matching".
Is there a way we can figure out which is shell doing, and which is doing by regular expressions.


For example with the below example: How to figure out which portion is shell or regex performing the operation:


Code:
# ls -l bbcbbc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc


# ls -l !([c-z])?[c][b][b][b-d]
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc


echo $SHELL
/usr/bin/ksh
#OS= hp-ux 11.23


Thanks..
In general, the answer is to read the man pages. But as a high altitude first cut, any unquoted argument passed to the shell that is not a parameter expansion (i.e., $name or ${...name...} that is not part of a shell command lanuage construct (e.g., if , then , else , fi , case , esac , ...) will be subject to pathname expansion.
The Following User Says Thank You to Don Cragun For This Useful Post:
rveri (03-27-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Appending a character(#) with string search at the start of the line chandu123 UNIX for Dummies Questions & Answers 1 01-08-2013 07:26 PM
How to start working with GNU PLOT?? Naga06 Shell Programming and Scripting 7 01-26-2012 01:46 AM
pppoe-start not working editheraven Slackware 0 09-18-2011 11:26 AM
Solaris 10, start inetd in a zone not working click Solaris 6 08-09-2011 03:22 AM
checking whether line start with character or number Satyak Shell Programming and Scripting 4 10-14-2008 02:11 AM



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