Filename manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filename manipulation
# 1  
Old 10-18-2012
Filename manipulation

Hi all,

I need to convert some files with the following name format:
Code:
<Base File Name>_YYYYMMDD_HHMISS_NNNNN.*

to:
Code:
NNNNNN_<Base File Name>_YYYYMMDD_HHMISS.*

Example:
Code:
MY_FILE_NAME_20120912_123443_12345.data

will be converted to
Code:
012345_MY_FILE_NAME_20120912_123443.data

Notice that the file name contains underscore as well so I can't use it as string separator. Also the 5 digits number in the original name needs to be converted to a 6-digit one so a zero is padded in front of it.

So essentially, it's just moving the 5 digit number at the end of the file name to the front and make it a 6-digit number.

Any suggestions what's the best way to do this?

Thanks heaps in advance.

Regards,

Ivan

Last edited by Franklin52; 10-18-2012 at 03:52 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-18-2012
Hi Ivan

I'd probably use Perl for this, but I'd probably use Perl for everything Smilie
Code:
 perl -e '$_="MY_FILE_NAME_20120912_123443_12345.data";/(.+?)_(\d+)(\..+)/&& printf"%06d_%s%s\n",$2,$1,$3;'

012345_MY_FILE_NAME_20120912_123443.data

# 3  
Old 10-18-2012
Hi,
check if this will help
Code:
ls |sed 's/\(............................\)\(.\)\(.....\)/0\3_\1/'

# 4  
Old 10-18-2012
Another perl (with some assumptions regarding the file-name structure) which will actually rename your files:
Code:
perl -e 'chdir "/path/to/files" or die "Cannot chdir ($!)\n";
@targetfiles=glob "*_????????_??????_?????.*";
for (@targetfiles) {
 if(-f && /(.*_[0-9]{8}_[0-9]{6})_([0-9]{5})(.*)/s) {
  $new_name = sprintf "%06d_%s%s",$2,$1,$3;
  print "[$_] will be renamed to [$new_name]\n";
  #print "Renaming [$_] to [$new_name] unsuccessful ($!)\n" unless rename $_ => $new_name;
 }
}'

Change the path (first line) suitably.
Uncomment the rename line if the printed lines seem OK.
# 5  
Old 10-18-2012
Code:
 
$ for i in *.data; do echo $i; done
MY_FILE_NAME_20120912_123443_12345.data
 
$ for i in *.data; do echo mv $i ${i%_*}.${i##*.}; done
mv MY_FILE_NAME_20120912_123443_12345.data MY_FILE_NAME_20120912_123443.data

if you are happy with the command printed, then remove the echo
# 6  
Old 10-18-2012
@itkamaraj:
Quote:
Originally Posted by schweinshaxe
Example:
Code:
MY_FILE_NAME_20120912_123443_12345.data

will be converted to
Code:
012345_MY_FILE_NAME_20120912_123443.data

# 7  
Old 10-18-2012
Quote:
Originally Posted by elixir_sinari
@itkamaraj:
ooops, i didnt notice the first word (012345_) Smilie

thanks for notifying.

---------- Post updated at 02:17 PM ---------- Previous update was at 02:13 PM ----------

Code:
 
$ for i in *.data; do a=${i%.*};echo mv $i 0${a##*_}_${i%_*}.${i##*.}; done                                      
mv MY_FILE_NAME_20120912_123443_12345.data 012345_MY_FILE_NAME_20120912_123443.data

---------- Post updated at 02:21 PM ---------- Previous update was at 02:17 PM ----------

Code:
 
$ for i in *.data; do a=${i%.*};b="00000"${a##*_};echo mv $i ${b:(-6)}_${i%_*}.${i##*.}; done
mv MY_FILE_NAME_20120912_123443_12.data 000012_MY_FILE_NAME_20120912_123443.data
mv MY_FILE_NAME_20120912_123443_12345.data 012345_MY_FILE_NAME_20120912_123443.data

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

filename manipulation using "sed"

Dear all, I need to manipulate some filenames (dump.1, dump.2, etc.) and feed them to another command. For this purpose I am using sed and because my last COMMAND needs to receive files one-by-one I am using xargs: >> ls dump.* | xargs sed -n 's/expression1/expression2/' | COMMAND The... (4 Replies)
Discussion started by: bigboss010
4 Replies

4. Shell Programming and Scripting

difference between sh filename.sh and . filename

Hi, Gurus, I feel sorry to ask this easy question, but to be honest, I really don't know. I have a question about execute shell script. I have script named as filename.sh with -rwxr-xr-x permission. when I execute it with . filename.sh It works properly. when I exec it with sh... (7 Replies)
Discussion started by: ken002
7 Replies

5. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

6. UNIX for Dummies Questions & Answers

Filename Manipulation in AWK

I have created a nawk program and passing a file as an input parameter. The filename will be /home/dir/ksh/test.out I need to extract the filename alone. Is there anyway to get this ? Input : /home/dir/ksh/test.out Output -1: test.out Output -2 : t Input :... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

7. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies

8. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

9. Shell Programming and Scripting

mv Filename variable to another filename

Anyone who can assist : I am trying to pass the group vairiable to a filename: rpt_tsavegrp=/export/legato/scripts/$group_savegrp_rpt.$dat It will not pass to variable. Anyone have any ideas what I am doing wrong here. Thanks # This script sends email that save group completed.... (3 Replies)
Discussion started by: gzs553
3 Replies

10. UNIX for Dummies Questions & Answers

shortcut for tar cvf - [filename] | gzip > [filename].tar.gz

i'd like to have an alias (or something similar) where i can type a command like "archive" and a filename and have it tar and gzip the file, so... $ archive filename results in filename.tar.gz...do i have to write a script to do this? (4 Replies)
Discussion started by: bcamp1973
4 Replies
Login or Register to Ask a Question