Trouble with test pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble with test pattern
# 1  
Old 06-15-2015
Trouble with test pattern

I can run the following command

Code:
IFS='\n';ls |grep "02\\-[0-9]\{2\}-2015" |while read file;do cp $file /tmp/feb

However, what I really want to do is
Code:
 if [[ $file =~ "02\\-[0-9]\{2\}-2015"  ]]; then cp $file /tmp/feb;fi

The problem is the regex is not picked up in this test condition. Can someone help? In addition, I need to make sure that the newline is not printed, the filenames have spaces in them, and that is why IFS='\n' is used.
# 2  
Old 06-15-2015
Would that work? Remove the echo if it does.
Code:
for file in *; do
    if [[ "$file" =~ 02\\-[0-9]\{2\}-2015 ]]; then
       echo cp "$file" /tmp/feb
   fi
done


Last edited by Aia; 06-15-2015 at 03:15 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 06-15-2015
Try removing the quotes, it may be taking them literally.
# 4  
Old 06-15-2015
I believe the following change to Aia's test should match the original grep criteria. Also =~ if for awk, in bash/ksh use = instead

Code:
for file in *; do
    if [[ "$file" = *02-[0-9][0-9]-2015* ]]
    then
       echo cp "$file" /tmp/feb
   fi
done

# 5  
Old 06-15-2015
Quote:
Originally Posted by Chubler_XL
I believe the following change to Aia's test should match the original grep criteria. Also =~ if for awk, in bash/ksh use = instead

Code:
for file in *; do
    if [[ "$file" = *02-[0-9][0-9]-2015* ]]
    then
       echo cp "$file" /tmp/feb
   fi
done

This is the line that the OP posted having problems with:
Code:
if [[ $file =~ "02\\-[0-9]\{2\}-2015"  ]];

The intention was to just show where the trouble originate from, thus, basically, the copy and paste.
Otherwise, I would have not even bother with a loop, a simple ls pattern | xargs cp would have done the job.

Quote:
Also =~ if for awk, in bash/ksh use = instead
Since version 3 bash obtained its own regex-match operator: =~.

Last edited by Aia; 06-15-2015 at 05:55 PM.. Reason: Explaining
This User Gave Thanks to Aia For This Post:
# 6  
Old 06-15-2015
Thanks Aia, never used the regex-match of bash, probably looked and it a while ago and ignored due to portability issues.

Yes the important details for the OP is to quote $file both in the cp command line and the test expression. Also don not escape or quote the RE:

Code:
if [[ "$file" =~ 02-[0-9]{2}-2015 ]]; then cp "$file" /tmp/feb;fi

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Expect Script - Pattern Matching Trouble

I am still learning expect, For the below script I always get This is True as the answer. Tried to debug and does not make sense to me. Not sure, where I am doing the mistake. Need Help! - Thanks #!/usr/bin/expect -f set timeout 10 spawn -noecho bash expect { *$* } send "test -d... (3 Replies)
Discussion started by: rmsagar
3 Replies

3. Shell Programming and Scripting

Prefixing test case methods with letter 'test'

Hi, I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
Discussion started by: royalibrahim
5 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. Shell Programming and Scripting

select some text from a test dependng on pattern

I have some absolute file location $INSTALL_BASEPATH/onereview-5.0/resources/commons-messages/commonmessages_default.properties $INSTALL_BASEPATH/onereview-5.0/orv-deploy/config-console.war/WEB-INF/classes/com/connectiva/configuration/console/resource/configurationBundle.properties I need to... (3 Replies)
Discussion started by: mnmonu
3 Replies
Login or Register to Ask a Question