Wildcard in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wildcard in bash script
# 1  
Old 03-01-2014
Wildcard in bash script

Hi there,
I am pretty new to linux scripting so ..

I am writing a script to loop through all my directories of sequence files in order to do stuff with them (trimming, normalizing, stuff that one would do with sequence files).

Here I need to pick out files that match each other. The files containing
R1??????R1 should be matched with R1??????R2, R2??????R1 with R2??????R2, and so on. The ‘?’ being wildcards. There are always six of them (barcodes).

So, I try the following test to pick a file:


Code:
if echo "$z" | grep -q "R1??????R1";



which does not work, most likely because the ? are treated as the text part I am searching for and not a wildcard.


Suggestion to solve this one would be greatly appreciated.


best regards

Last edited by Scrutinizer; 03-01-2014 at 06:09 AM.. Reason: code tags
# 2  
Old 03-01-2014
I didn't understand completely, can you try replacing those ? with . (period) if echo "$z" | grep -q "R1......R1";
This User Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 03-01-2014
Grep uses regular expressions (regex), not pattern matching. It is a different syntax.

The equivalent regex of the pattern
Code:
R1??????R1

is
Code:
^R1......R1$


Last edited by Scrutinizer; 03-01-2014 at 06:18 AM..
# 4  
Old 03-01-2014
Code:
if [[ "$z" == R1??????R1 ]]
then
  ...
fi

# 5  
Old 03-02-2014
I used:
if echo "$z" | grep -q "R1......R1"; then

and it seem to work the way I want - pick files that contain a string with R1+sixletters+R1
in the filename.

Thank you for your replies.

jahn
# 6  
Old 03-02-2014
If you are using grep with regex as specified in post #2, you will need the opening ^ and the closing $ as well, otherwise unintended matches might occur.
# 7  
Old 03-02-2014
I tried that, but could not get it to work.
if echo "$z" | grep -q "^R1......R1$";

I may be doing something wrong here?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

3. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

4. Shell Programming and Scripting

Help using wildcard (*) within script

Hi. I'm using !/bin/ksh. I used to be decent at shell programming years ago, but now I am extremely rusty. HELP My team has a script that takes user input from screen. The user enters grep A0 /dir/dir1/dir2/*filename*. The code works if they enter a filename or directory. It doesn't work if... (5 Replies)
Discussion started by: katolb75
5 Replies

5. Shell Programming and Scripting

Problem when concatinating wildcard onto file location in bash script

I am having difficulty with the following script: #! /bin/bash filelist=~/data/${1}* ~/./convertFile $filelist ~/temp/outputEssentially, there are a large number of files in the directory ~/data, each with a four-letter code at the beginning (eg. aaaa001 aaaa002 bbbb001 bbbb002 etc). The... (11 Replies)
Discussion started by: Lears_Fool
11 Replies

6. Shell Programming and Scripting

wildcard in bash shell script

I tried to use the wildcard '*' in my bash script, but I can not get it work. Here is a simple example (list file names in current directory): ls ./* does not work in my bash script. But it works if I use ls ./ So is there any special syntax to use '*' wildcard in bash script (I tested the... (11 Replies)
Discussion started by: aerosols
11 Replies

7. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. UNIX for Dummies Questions & Answers

Bash Wildcard Question

Hi, I am writing a BASH script. In a directory I have a bunch of files of various filename structures. How do I list all the filenames that begin with either a capital or lowercase A or T. Is there one command that could replace the following 4: ls A* ls a* ls T* ls t* Thanks. Mike (3 Replies)
Discussion started by: msb65
3 Replies

9. Shell Programming and Scripting

globbing, $# is too high after wildcard expansion in bash script

How can I pass in an argument such as "*.k" to a bash script without having to double-quote *.k and not having *.k `glob` to match all files in the pattern? I tried using noglob in my script but this didn't work the way I thought it would.. expansion is still occuring, $# is higher than I... (3 Replies)
Discussion started by: zoo591
3 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question