bash script to increment a digit in filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers bash script to increment a digit in filename
# 1  
Old 03-14-2010
bash script to increment a digit in filename

Hi guys,

Can someone help me out with this:

I have a directory with files like the following,
GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g
GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g
GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g
GHost++ 2010-03-14 04-13 DotaCash AR us_ca LC #8 (48m17s).w3g
GHost++ 2010-03-14 04-15 DotaCash RD us_ca LC #9 (45m17s).w3g

I would like to know how to add to the hour part of the filename for all the files in that direction. In this particular example, add 3 to 04, so it becomes 07.

end result would be something like this.
GHost++ 2010-03-14 07-01 DotaCash RD us_ca LC #7 (44m19s).w3g
GHost++ 2010-03-14 07-06 DotaCash AP us_ca LC #8 (42m24s).w3g
GHost++ 2010-03-14 07-07 DotaCash AR us_ca LC #10 (08m23s).w3g
GHost++ 2010-03-14 07-13 DotaCash AR us_ca LC #8 (48m17s).w3g
GHost++ 2010-03-14 07-15 DotaCash RD us_ca LC #9 (45m17s).w3g

Any help is greatly appreciated.

Thanks.,
# 2  
Old 03-14-2010
Code:
ls GHost++ 2010-03-14 04-* |
awk -F'[ -1]'  '{ old=$0; $5+=3; system( sprintf("mv %s %s",old, $0) ) } '

Try nawk on Solaris
# 3  
Old 03-14-2010
Hi thanks for the quick response.
It did not work...


Code:
 
ls GHost++\ `date +%Y-%m-%d`* | awk -F'[ -1]'  '{ old=$0; $5+=3; system( sprintf("mv %s %s",old, $0) ) } '
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory
mv: target `LC' is not a directory



---------- Post updated at 07:52 AM ---------- Previous update was at 07:27 AM ----------

I am not sure if this would make it easier, but basically I want to add 3 hours to the date stamp in the file name.
# 4  
Old 03-14-2010
rename 's/([^\s]+\s){2}(\d+)/$1. " " . ($2+3)/e' GHost++*

Code:
ls
GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g
GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g
GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g
GHost++ 2010-03-14 04-13 DotaCash AR us_ca LC #8 (48m17s).w3g
GHost++ 2010-03-14 04-15 DotaCash RD us_ca LC #9 (45m17s).w3g

19:55:12 : t :rename 's/([^\s]+\s){2}(\d+)/$1. " " . ($2+3)/e' GHost++*
19:55:34 : t :ls
2010-03-14  7-01 DotaCash RD us_ca LC #7 (44m19s).w3g
2010-03-14  7-06 DotaCash AP us_ca LC #8 (42m24s).w3g
2010-03-14  7-07 DotaCash AR us_ca LC #10 (08m23s).w3g
2010-03-14  7-13 DotaCash AR us_ca LC #8 (48m17s).w3g
2010-03-14  7-15 DotaCash RD us_ca LC #9 (45m17s).w3g

# 5  
Old 03-14-2010
Thanks.

Getting closer, however, the 04 was replaced with just 3, that will not work due to the change in format of filename
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing filename with sed in bash script

I need to cat two files with similar names. I am using the following script: #!/bin/bash if ] then file=$1 file2="${file%R1.fastq}R2.fastq" echo fetching data from R2 file ... sleep 3 cat $file $file2 > infile else echo "Input_file passed... (2 Replies)
Discussion started by: Xterra
2 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Bash: Getting first digit of a string

If i'm given a string like "abc-def-1.2.3", how would I return "1"? I'm new to scripting and got stumped on this problem. Thanks in advance! (7 Replies)
Discussion started by: atsim
7 Replies

4. Shell Programming and Scripting

How to Pass filename to AWK in bash script

I have written a script which works fine, to remove patterns contained in EXCLUDE.DAT from input.txt awk 'BEGIN {n=0;while (getline < "EXCLUDE.DAT" > 0){ex=$0;n++}} {for(var in ex){print var "-" ex $0 ;i++}}' input.txt The last problem I need to solve is how to pass the file... (3 Replies)
Discussion started by: nixie
3 Replies

5. Shell Programming and Scripting

Change filename - increment digit by 60

I have a bunch of files, they have spaces! I am working in windows 7, but I am hoping I could install and use cygwin or some other unix platform to rename my files (suggestions welcome). The files are named: Wind Attn Vol - 474.wrl Wind Attn Vol - 475.wrl Wind Attn Vol - 476.wrl etc.. I... (10 Replies)
Discussion started by: d_sai_kumar
10 Replies

6. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

7. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

8. Shell Programming and Scripting

increment counter as suffix starting with the rightmost digit

Hi, I would like to add a suffix to a file name but maintain the suffix length to 5 digits. For eg, output > 1st_file.00001, 2nd_file.00002...10th_file.00010....100th_file.00100 Can anyone please advise me on how to go about it? Platform: SunOS mps201a 5.9 Generic_118558-39 sun4u... (7 Replies)
Discussion started by: danish0909
7 Replies

9. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

10. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies
Login or Register to Ask a Question