Globbling with multiple criteria (UNIX Shell)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Globbling with multiple criteria (UNIX Shell)
# 1  
Old 05-13-2013
Wrench Globbling with multiple criteria (UNIX Shell)

I am new to UNIX Shell. I want to list the files names in the current directory that are not start with 'AB' and have at least two characters. For example, say I have those files in the current directory: AB, AC, AD, AE, B, C. After executing the command, AC, AD, AE will be listed on the screen.
My strategy is to get those files that their names have more than one character and use pipeline to redirect to another echo for filtering those files names that do not start with AB. I have tried the following command:

Code:
echo [!?] | echo [!A][!B]*

It seems it works fine, however, I am not sure wether I am correct. Is there any other way to do this? BTW, I am getting confused about the the differences between

Code:
echo [!A][!B]* | echo [!?]

and

Code:
echo [!?] |echo [!A][!B]*

I know that the first one is pass the results that do not start with AB to the second echo to filter files names longer than one character, the second one does the reverse order, however, they gave me different answers. I thought they should perform exactly the same. Could anybody explain the differences? and why I get different answers? I appreciate that.

Last edited by Scrutinizer; 05-13-2013 at 05:35 PM.. Reason: code tags
# 2  
Old 05-13-2013
echo cannot accept stdin, so you cannot pipe output of another command into it. But how about just:
Code:
ls [!A][!B]*


Last edited by Scrutinizer; 05-13-2013 at 05:47 PM..
# 3  
Old 05-13-2013
Quote:
Originally Posted by Scrutinizer
echo cannot accept stdin, so you cannot pipe output of another command into it. But how about just:
Code:
ls [!A][!B]*

I want to retrieve files whose names have at least two characters but do not start with 'AB'. The command : ls [!A][!B]* just list files that do not start with 'AB', thus, file AC will not appear in the result, however, it should be.
# 4  
Old 05-13-2013
Ow yes, that will also not list files that start with "A" . This should work better:
Code:
ls [!A]?* A[!B]*


Last edited by Scrutinizer; 05-13-2013 at 06:24 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-14-2013
It seems it works perfectly fine. Can I understand the code in this way that [!A]?* filters those files not start with A, however, have at least two characters and A[!B]* catches those files start with A, however, not AB? Does ls command combine the results in terms of two criteria together? Thanks!Smilie
# 6  
Old 05-14-2013
Your understanding is correct for the first part, but
Code:
[!A]?* A[!B]*

Are two patterns that produce one field list, so it is not the command that glues them together. These fields are input to the command ls. They would need to be mutually exclusive.
You can see what is looks like if you first type:
Code:
set -x

and issue the command. Later you can switch it off with set +x


If for either pattern there is at least one file present, then you could for example also use:
Code:
printf "%s\n" [!A]?* [A[!B]*

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 05-14-2013
You could also use bash extend globbing:

Code:
bash$ shopt -s extglob
bash$ ls
AB  AC  AD  AE  B  C
bash$ ls !(AB*|?)
AC  AD  AE

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Export Oracle multiple tables to multiple csv files using UNIX shell scripting

Hello All, just wanted to export multiple tables from oracle sql using unix shell script to csv file and the below code is exporting only the first table. Can you please suggest why? or any better idea? export FILE="/abc/autom/file/geo_JOB.csv" Export= `sqlplus -s dev01/password@dEV3... (16 Replies)
Discussion started by: Hope
16 Replies

2. Shell Programming and Scripting

Finding the right file with multiple sort criteria

Hello, I have files in a directory with names like, ./f0/84.40_E1200_85.39_E1300_f0_r00_1300-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.40_E1200_85.83_E1200_f0_r00_1200-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.60_E1100_86.45_E1100_f0_r00_1100-ON-0.25_S7A_v4_47.19.1.out.txt... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. Shell Programming and Scripting

Help getting a UNIX shell script to look at multiple files.

Hey everyone! I made a shell script that would go through a file and replace any phrase or letter with another phrase or letter. Helps update variable names or values. The following code is this: #!/bin/sh Word1="$1" Replace1="$2" File1="$3" arg=$( echo "$Word1" | sed 's:\:\\&:g' )... (3 Replies)
Discussion started by: rebmonk
3 Replies

4. Shell Programming and Scripting

How to run multiple functions in Background in UNIX Shell Scripting?

Hi, I am using ksh , i have requirement to run 4 functions in background , 4 functions call are available in a case that case is also in function, i need to execute 1st function it should run in background and return to case and next i will call 2nd function it should run in background and... (8 Replies)
Discussion started by: karthikram
8 Replies

5. Shell Programming and Scripting

Globbling files in the direct subdirectory of the current directory

I want to list files that end with .c in the direct subdirectory of the current directory. I have tried the following command: find ./ -mindepth 2 -maxdepth 2 -name "*.c" Is that right? Or is there any easier way to handle that problem? Another problem is that I want to grep in a file to find... (5 Replies)
Discussion started by: Ray Sun
5 Replies

6. Shell Programming and Scripting

Extract error records based on specific criteria from Unix file

Hi, I look for a awk one liner for below issue. input file ABC 1234 abc 12345 ABC 4567 678 XYZ xyz ght 678 ABC 787 yyuu ABC 789 7890 777 zxr hyip hyu mno uii 678 776 ABC ty7 888 All lines should be started with ABC as first field. If a record has another value for 1st... (7 Replies)
Discussion started by: ratheesh2011
7 Replies

7. UNIX for Advanced & Expert Users

Multiple Instance of Unix Shell Script

Hi All, I have a problem mentioned below. I have a script which performs line by line operations on several files. I have a temp_file storing the list of names of the file to be validated. Right not in while loop i validate these files one by one. Is there anyway that i can modify... (1 Reply)
Discussion started by: amitaryans
1 Replies

8. Shell Programming and Scripting

Help with egrep or grep command to meet multiple criteria

Hello, I"m a newbie :). I hope I can learn from the scripting expert. I'm trying to use egrep and grep commands to get the total count by meeting both criteria. So far, I haven't been able to do it. if robot = TLD and barcode = AA, then final count should be 2 if robot = TLD and... (9 Replies)
Discussion started by: MinBee
9 Replies

9. Shell Programming and Scripting

Combine multiple string into 1 string group by certain criteria

Hi all, I am newbie in unix. Just have some doubts on how to join multiple lines into single line. I have 1 file with following contents. R96087641 HostName-kul480My This is no use any more %% E78343970 LocalPath-/app/usr/SG (Blank in this line) %% E73615740... (4 Replies)
Discussion started by: whchee
4 Replies

10. Shell Programming and Scripting

Searching for multiple criteria in log files?

I would like a simple shell script that will allow me to display to screen all unsuccessful su attempts in my sulog file, for the present date. I have been trying several different combinations of commands, but I can't quite get the syntax correct. The mess I have right now (don't laugh) is... (4 Replies)
Discussion started by: Relykk
4 Replies
Login or Register to Ask a Question