The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 05:59 AM
Search for a pattern from the result of search boopathi_d Shell Programming and Scripting 3 12-05-2007 09:54 AM
Search Mulitiple String pattern in a file krishnan_6015@y Shell Programming and Scripting 2 11-23-2007 04:03 AM
Advance string pattern search Please sainj Shell Programming and Scripting 2 10-30-2007 10:56 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 03:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-29-2008
deepakgang deepakgang is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
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
  #2 (permalink)  
Old 05-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 08:38 AM.. Reason: Suggestion to try with echo
  #3 (permalink)  
Old 05-29-2008
deepakgang deepakgang is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
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
  #4 (permalink)  
Old 05-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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).
  #5 (permalink)  
Old 05-30-2008
deepakgang deepakgang is offline
Registered User
  
 

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

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



Thanks Mate
D
  #6 (permalink)  
Old 05-31-2008
snowline84 snowline84 is offline
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>
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0