![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
What are your criteria for "end part"?
Code:
case $file in *_\{arc\}_*) mv "$file" "${file%%_*}" ;; esac
Last edited by era; 05-29-2008 at 05:38 AM. Reason: Suggestion to try with echo |
|
|||
|
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 |
|
|||
|
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).
|
|
|||
|
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> |
|
|||
|
The ls is Useless, you can simply say
Code:
for test in *1* |
|||
| Google The UNIX and Linux Forums |