Wildcard search in if loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wildcard search in if loop
# 1  
Old 10-21-2013
Wildcard search in if loop

Practice folder contains many files and im interested in extracting file which starts with abc* ghi* xyz* . I need to do variety of operations for different files. if file starts with xyz* then i need to move to some destination otherwise some other destination. I am not able to make wildcard search working in if loop. can someone help me to fix this.
Code:
#!/bin/sh
cd /home/practice
for file in abc* ghi* xyz*
 do
  
   if [$file = xyz*];
  then
  #echo $file;
   thetime=`date +%d%M`;
   name=${file%\.*}  
   #echo $name"_"$thetime"."xml
   #mv  ${file}  /test/dest1/$name"_"$thetime"."xml
  else
  #mv  ${file}  /test/dest2/$name"_"$thetime
  
              fi;
 done

# 2  
Old 10-21-2013
can you change the line as follows:
Code:
if [[ $file == xyz* ]];

instead of
Code:
if [$file = xyz*];


Last edited by greet_sed; 10-21-2013 at 07:05 AM.. Reason: updated pattern
# 3  
Old 10-21-2013
In Bourne shell /bin/sh you will need to extract the heading of the file to test against. The if will only accept a literal value to compare to. You might consider the following:-
Code:
#!/bin/sh
cd /home/practice
for file in abc* ghi* xyz*
 do
   testval="${file%${file##???}}"  
   if [ $file = "$testval" ];
  then
:
:

It might look a bit scary, but what the extra line does is to slice up the string. You could do this with cut but that spawns another process, so it can cost a lot of time if this is in a loop for lots of files.

The % and # markers are then followed by descriptions of how to split the line. The second part ${file##???} cuts the first three characters. It seems odd, but given that is what you want, it is the fed back in to the first part which then excludes everything from the string apart from what was worked out in part 2.

So, for a file name of xyz12345678, part 2 evaluates to 12345678, so part 1 the removes this from the end of the whole file name and you are left with xyz You can then use this in your if statement.



I hope that this helps,
Robin
Liverpool/Blackburn
UK
# 4  
Old 10-21-2013
Thanks for your reply but its not working . I think i have not explained the question well or did not understand your explanation. example for file in
/home/practice catalog
Code:
abc12_0001_0001
abc13_0001_0002
abc14_0001_0003
ghi12_0001_0001
ghi13_0001_0002
ghi14_0001_0003
xyz12_0001_0001.xml
xyz13_0001_0002.xml
xyz14_0001_0003.xml

Now i want to extract all the files which starting with xyx and move it to some destination. with the code you have given last time
Code:
if [ $file = "$testval" ] becomes xyz12_0001_0001=xyz

which is false and it executes the else code. Help me to fix this issue. for now im having a dumb solution
Code:
for file in xyz*
 do
   #if [$file] ; 
   thetime=`date +%d%M`;
   name=${file%\.*}
   #echo $name
   #echo $f
   echo $name"_"$thetime"."xml
   mv  ${file}  /test/dest1/$name"_"$thetime"."xml
              #fi;
  done
for file in abc* ghi*
do
  thet=`date +%d%M`;
              echo $file"_"$thet
  #mv  ${file}  /test/dest2/$name"_"$thet
done

thanks for your help in advance
# 5  
Old 10-21-2013
Tested code and it works .

Code:
 ls -a

Code:
.  ..  abc12_0001_0001  abc13_0001_0002  abc14_0001_0003  ghi12_0001_0001  ghi13_0001_0002  ghi14_0001_0003  xyz12_0001_0001.xml  xyz13_0001_0002.xml  xyz14_0001_0003.xml

Code:
code:
#!/bin/bash
mkdir A1 A2
for file in abc* ghi* xyz*
do
if [[ $file == abc* ]];  then
echo "Matched file is: $file";
mv $file "A1"
else
echo "Matching other than abc*"
mv $file "A2"
fi;
done

Two temp dirs are created by the script and after execution , i can see the abc*files in A1 and rest of thefiles in A2.

Last edited by greet_sed; 10-21-2013 at 08:16 AM.. Reason: update creation of dir by script
This User Gave Thanks to greet_sed For This Post:
# 6  
Old 10-21-2013
Thanks for your response. Im getting this following error when im running your code
Code:
 
`==' is not expected.

# 7  
Old 10-21-2013
I didn't get any error as mentioned earlier it is tested code.
can you post the exact sample data you are trying and code you have used ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loop with Perl (string search)

I am using a perl script to reverse and complement sequences if a string is found. The script works as expected as standalone but I would like to use it in my bash file. However, I am not getting my expected result. My test.txt file >Sample_72... (8 Replies)
Discussion started by: Xterra
8 Replies

2. Shell Programming and Scripting

Grep and BzGrep with Wildcard in Search Pattern

Hello All, I hope this is the right area. If not, Kindly let me know and I will report in the appropriate spot. I am needing to find a search pattern that will make the * act as Wildcard in the search pattern instead of being literal. The example I am using is bzgrep "to=<*@domain.com>"... (5 Replies)
Discussion started by: mancountry
5 Replies

3. Emergency UNIX and Linux Support

Waiting for wildcard filename to exists in while loop

Hi Experts, We are developing a script which will wait for the trigger file(with datetime in the trigger file name). But the problem is when I use 'while' loop to wait for the file, it waits for the filename with wilcard in it that is wait for 'Trigger*.done' file. :eek: Below is the script ... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

4. Shell Programming and Scripting

Search a wildcard text in a file

Hi, I have a file(report.txt) that contains : 0 1 chk_uncov_data_assert 776 chk_uncov_data_assert : assert property (chk_uncov_data) 1 0 chk_data_assert 772 chk_data_assert : assert property (chk_data) 1 0 chk_data_cover ... (8 Replies)
Discussion started by: Anamika08
8 Replies

5. Shell Programming and Scripting

Speeding up search and replace in a for loop

Hello, I am using sed in a for loop to replace text in a 100MB file. I have about 55,000 entries to convert in a csv file with two entries per line. The following script works to search file.txt for the first field from conversion.csv and then replace it with the second field. While it works fine,... (15 Replies)
Discussion started by: pbluescript
15 Replies

6. Shell Programming and Scripting

Grep Wildcard search

How can i grep for a pattern with wildcard using grep? I want to identify all the lines that start with SAM and end in .PIPE IN.TXT SAM_HEADER.PIPE SAM_DETAIL.PIPE SAM_INVOICE.PIPE Can i do something like grep SAM*.PIPE IN.TXT (2 Replies)
Discussion started by: venky338
2 Replies

7. Shell Programming and Scripting

ksh(!93) for loop with wildcard and empty directory

I'm sure this is by design, but using something like for f in dir/* do echo $f done produces unexpected (to me) results if run against an empty directory. I'd have expected it to not execute the loop, but it actually calls it with f set to 'dir/*'. Now I know that I'm trying to protect... (2 Replies)
Discussion started by: spr00t
2 Replies

8. UNIX for Dummies Questions & Answers

how to search and list file with wildcard character

hi, I want to search all files in the current working direcotry and to print in comma (,) seperated output. But I have two patterns to search for. Files will be in ABC20100508.DAT format. Search should happen on the format (ABC????????.DAT) along with date(20100508). I can do a ls... (2 Replies)
Discussion started by: anandapani
2 Replies

9. Shell Programming and Scripting

use loop to search

Hi, I need to search through a file to find the last one occurence of my data. File1 is like this: sososo abc123 ssssss abc456 ssssss abc345 I need to loop through to identify the last occurence of "abc". So "abc345" is the record that I need. Can someone give me some ideas? (9 Replies)
Discussion started by: whatisthis
9 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