The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SED Search Pattern and Replace with the Pattern racbern Shell Programming and Scripting 4 03-15-2008 01:59 AM
Search for a pattern from the result of search boopathi_d Shell Programming and Scripting 3 12-05-2007 05:54 AM
Search Mulitiple String pattern in a file krishnan_6015@y Shell Programming and Scripting 2 11-23-2007 12:03 AM
Advance string pattern search Please sainj Shell Programming and Scripting 2 10-30-2007 06:56 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 11:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-29-2008
Registered User
 

Join Date: Oct 2007
Posts: 26
search pattern in a string and rename

Hi All,

I have a string assigned to a variable. the string will be a filename. Something like below:

file=testfile.dat.20009080_{arc}_2004809090

I need to check if the filename has a pattern like "_{arc}_" and if so I have to rename the file like below

mv testfile.dat.20009080_{arc}_2004809090 testfile.dat.20009080


Could anyone tell me how to check if the string contains the pattern and to generate the new filename by removing the end part.

Thanks,
D
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
What are your criteria for "end part"?

Code:
case $file in *_\{arc\}_*) mv "$file" "${file%%_*}" ;; esac
Assuming your shell supports the %% variable interpolation, this will remove everything after the first underscore. (Maybe try it with echo instead of mv to see if it works for you.)

Last edited by era; 05-29-2008 at 05:38 AM. Reason: Suggestion to try with echo
Reply With Quote
  #3 (permalink)  
Old 05-29-2008
Registered User
 

Join Date: Oct 2007
Posts: 26
the criteria is not actually anything aftet the first underscore, but its from the underscore just starting before {arc}. So for example the file :

test_file.dat.20009080_{arc}_2004809090

Should be renamed to "test_file.dat.20009080"


Thanks
D
Reply With Quote
  #4 (permalink)  
Old 05-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
The change is very minor, just use ${file%_{arc}_*} -- that should also make it work even with more shells (in theory any Bourne shell, although I can imagine some old and/or buggy ones might require quoting of the literal curly braces, and/or simply freak out on them).
Reply With Quote
  #5 (permalink)  
Old 05-30-2008
Registered User
 

Join Date: Oct 2007
Posts: 26
The below has worked:

case $file in *_\{arc\}_*) mv "$file" "${file%%_\{arc\}_*}" ;; esac



Thanks Mate
D
Reply With Quote
  #6 (permalink)  
Old 05-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 6
I think this can also help you

anlbilih:stenv4> ll
total 0
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 a1a
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 b1b
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 c1c
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 d1dd
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 ee1e
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 f2f
anlbilih:stenv4> for test in `ls -1 *1*`
> do
> mv $test `echo $test | sed s/1/2/`
> done
anlbilih:stenv4> ll
total 0
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 a2a
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 b2b
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 c2c
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 d2dd
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 ee2e
-rw-rw-r-- 1 stenv4 test 0 May 31 14:43 f2f
anlbilih:stenv4>
Reply With Quote
  #7 (permalink)  
Old 06-01-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
The ls is Useless, you can simply say

Code:
for test in *1*
and actually it avoids some bugs in corner cases where the ls in backticks will not work right.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0