Replacing part of filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing part of filename
# 8  
Old 01-29-2014
Quote:
Originally Posted by TimmyTiz
Thank you very much Don. But there is still one little detail.
If I use your code it is also replacing the second number but I just want to get rid of the first one. Probably because I have more than 2 underscores like:

mar10lan_all_1000_ct1210111172602.alf_gts_mar10.list
So, if that is your filename, what is the new name supposed to be:
Code:
mar10lan_1000_ct1210111172602.alf_gts_mar10.list

or
Code:
marlan_all_1000_ct1210111172602.alf_gts_mar10.list

or
Code:
mar10lan_1000_ct1210111172602.alf_gts_mar10.list

From you statements so far, I'm not sure if trying to get rid of the first string between underscore characters, the first string of digits between underscore characters, or the first contiguous string of one or more digits without caring about underscore characters???
# 9  
Old 01-29-2014
Yes, sorry. I want

mar10lan_all_1000_ct1210111172602.alf_gts_mar10.list

to be

mar10lan_all_ct1210111172602.alf_gts_mar10.list

so the numbers between the second and the third underscore should vanish
so I want to replace "_1000_" by just "_".
# 10  
Old 01-29-2014
Quote:
Originally Posted by TimmyTiz
Yes, sorry. I want

mar10lan_all_1000_ct1210111172602.alf_gts_mar10.list

to be

mar10lan_all_ct1210111172602.alf_gts_mar10.list

so the numbers between the second and the third underscore should vanish
so I want to replace "_1000_" by just "_".
You have made the requirements ambiguous again!
Do you want to:
  1. replace a string of digits surrounded by underscores with a single underscore only if the string of digits follows the 2nd underscore in the file's name,
  2. replace the first occurrence of a string of digits surrounded by underscores in the file's name with a single underscore,
  3. delete any digits between the 2nd and 3rd underscores (even if there are other characters between the 2nd and 3rd underscores) in the file's name, and delete one of the underscores if there weren't any other characters between the underscores,
  4. replace every string of digits surrounded by underscores in a file's name with a single underscore, or
  5. something else?
# 11  
Old 01-29-2014
second
# 12  
Old 01-29-2014
the heavy way ...
Code:
for i in *_ct*.list
do 
n=$(echo $i | sed 's/_1000_/_/')
mv "$i" "$n"
done

To macth other numeric pattern than 1000 :
Code:
 n=$(echo $i | sed 's/_[0-9]*_/_/')

# 13  
Old 01-29-2014
Yes, that worked perfectly.

Thank you guys Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing .dat.gz to .txt.gz in filename

Hi, I tried below method; mv -v /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz \ $(echo /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz | tr 'dat' 'txt'); nfame variable has the string "dat" . I need to rename files like below; ASIS: 20140902103700_100319.dat.gz... (8 Replies)
Discussion started by: snr_silencer
8 Replies

2. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

3. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. Shell Programming and Scripting

Getting part of a filename

Hi All, I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be: prefix<partwewant>suffix.extension the prefix and suffix are always 3... (4 Replies)
Discussion started by: imonkey
4 Replies

5. Shell Programming and Scripting

Replacing white spaces in filename

Hi; In following code find LOG_DIR -type f | while read filename; do echo $filename; done I want to precede each white space encountered in filename with \ so that when i use $filename for running some commands in do...done,it wont give me an error. will appreciate ur help in this.... (1 Reply)
Discussion started by: ajaypadvi
1 Replies

6. Shell Programming and Scripting

Replacing some part of file

Hello, I have two files, consider that as file1 and file2. Here file1 is the master file. file1 will contain data like GS*RA*071000013*102562451P*091130*0520*334052023*X*003050 ST*820*334052023 BPR*C*509.77*C*ACH*CTX*01*071000013*DA*5529085*9000002008**01*071000013*DA*5529085*091130... (8 Replies)
Discussion started by: atlantis
8 Replies

7. Shell Programming and Scripting

Replacing the part of file name?

Hi All One of my script generate following files. These files has static TIMESTAMP 20080227. AccAdd_20080227_1000.dat AccBal_20080227_1000.dat Acc_20080227_1000.dat AccGrpMem_20080227_1000.dat AccToCust_20080227_1000.dat What i need to do is, once the file has been generated, it... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

8. UNIX for Dummies Questions & Answers

Replacing string with filename

Hi All, I've recently run a script that inserts the filename into all files of my active directory. Now I want to move the filename string and have it replace text a few lines down. In other words, here's what I'm trying to do. Here is a file called 'goodtimes': " goodtimes Hi, Welcome... (1 Reply)
Discussion started by: calrog
1 Replies

9. Shell Programming and Scripting

Need help regarding replacing a part of string

Hi all suppose i have a string "abacus sabre", i need to replace occurences 'ab' with 'cd' and i need to store this result into same string and i need to return this result from script to the calling function, where as the string is passed from calling function. i tried like this ... (1 Reply)
Discussion started by: veerapureddy
1 Replies

10. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies
Login or Register to Ask a Question