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:
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
I didn't understand completely, can you try replacing those ? with . (period) if echo "$z" | grep -q "R1......R1";
1 Like
Grep uses regular expressions (regex), not pattern matching. It is a different syntax.
The equivalent regex of the pattern
R1??????R1
is
^R1......R1$
if [[ "$z" == R1??????R1 ]]
then
...
fi
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
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.
I tried that, but could not get it to work.
if echo "$z" | grep -q "^R1......R1$";
I may be doing something wrong here?
Perhaps I misread, if the file names have names that exactly match R1??????R2
, for example R1abcdefR2
then you would need ^R1......R1$
.
If the file names contain more characters than these 10 then you would need a pattern like *R1??????R2*
which would correspond to the regular expression R1......R1
.
Thank you.!
if echo "$z" | grep -q *R1??????R2*
works fine, as well!
To clarify: this string is just a part of the filename. There is stuff before and after.
jahn
In that case you could use patterns like this:
case $z in
(*R1??????R1*) echo "$z"
esac
or
if [[ "$z" == *R1??????R1* ]]
then
...
fi
or if you want to use grep with regex like in post #2:
if echo "$z" | grep -q "R1......R1";
could? must!
grep -q *R1??????R2*
takes additional args as files to search in, and ignores stdin.