search pattern in a string and rename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers search pattern in a string and rename
# 1  
Old 05-29-2008
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  
Old 05-29-2008
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 09:38 AM.. Reason: Suggestion to try with echo
# 3  
Old 05-29-2008
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  
Old 05-29-2008
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  
Old 05-30-2008
The below has worked:

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



Thanks Mate
D
# 6  
Old 05-31-2008
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>
# 7  
Old 06-02-2008
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

2. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

3. Shell Programming and Scripting

Search for Pattern as output between the matched String

Hello, I have a file which has the below contents : VG_name LV_name LV_size in MB LV_option LV_mountpoint owner group y testdg rahul2lv 10 "-A y -L" /home/abc2 ... (6 Replies)
Discussion started by: rahul2662
6 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

Batch rename files after internal search pattern

Hello all I am trying to do a script that would allow me to mass rename files after a grep search within them. They are XML files and the pattern I would like to append to the file name is easy to find (e.g. <filename>foo</filename>), but I can't for the life of me find out how to do it and... (2 Replies)
Discussion started by: aavv
2 Replies

6. Shell Programming and Scripting

Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth". *th=something If the delimited field fulfills this condition, eg. mth=value I would like to print out both key and value for some number comparison. eg. if value > "12"... (5 Replies)
Discussion started by: alienated
5 Replies

7. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies

8. UNIX for Dummies Questions & Answers

How to search for a pattern a string?

Hi all, I'm new in UNIX , so how to check if stringA is present within stringB ? Something similar to INSTR function in pl sql... Thanks a lot. (12 Replies)
Discussion started by: Leo_NN
12 Replies

9. Shell Programming and Scripting

Search Mulitiple String pattern in a file

Hi, I need to search for a multiple string pattern(5 key words) in a file(similar to a flat file) ,and i need to store the output in a another file . In that file we may have mutiple occurrences of the key words.and i need only the unique. kindly help out. Thanks, Mohana Krishnan (2 Replies)
Discussion started by: krishnan_6015@y
2 Replies

10. Shell Programming and Scripting

Advance string pattern search Please

Here is my problem.. 1. I want to search all those files with file name starting AJ128**** (in all the sub directories also) 2. I want to search for the follwoing type of string line beging with string - 'AK*any_1_char*any_2_char*510' 3. I need to display list of file names... (2 Replies)
Discussion started by: sainj
2 Replies
Login or Register to Ask a Question