renaming files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers renaming files
# 1  
Old 08-29-2007
renaming files

Hello all-

I need to rename files by adding an embedded 0
e.g.
aaa_bbb_1234 needs to become aaa_bbb_01234

The aaa and 1234 will change but the bbb_ can be my anchor.

TIA
# 2  
Old 08-29-2007
ok so I made this _way_ to complicated....

I first tried ( with no luck on lots of permutations ):
for i in `ls *bbb_*`
do
a=`echo $i | sed -e 's/\(*bbb_\)\(*\)/\10\2/g'`
mv $i $a
done

The zero was problematic also the replay of \1 and \2 did not seem to work at all.

Then after playing with it I realized I was just making it too fancy. Knowing that I had an anchor made it simpler:

for i in `ls *bbb_*`
do
a=`echo $i | sed -e 's/bbb_/bbb_0/g'`
mv $i $a
done
# 3  
Old 08-29-2007
i tried this option--

cat file1
aaa_bbb_1234
ccc_bbb_2315
ddd_bbb_9856

sed 's/???_bbb_1234/???_bbb_0{????}/g' file1
sed 's/???_bbb_????/'\(???_bbb_0????\)'/g' file1
sed 's/???_bbb_???/???_bbb_0\(????\)/g' file1

but this option is not working still shwoing the same output.
Can anyon eexplain me why?

Thanks
Namish
# 4  
Old 08-29-2007
First move all the files with similar names to another file:


find . -name "*_bbb_*" > names.txt

Then use the sed command



sed 's/\(.*\)_\(.*\)_\(.*\)/\1_\2_0\3/g' names.txt
# 5  
Old 08-29-2007
Quote:
Originally Posted by ohagar
[...]
aaa_bbb_1234 needs to become aaa_bbb_01234
[...]
Depending on your shell you could:

Code:
zsh 4.3.4% touch aaa_bbb_1234 bbb_bbb_5678
zsh 4.3.4% autoload -U zmv
zsh 4.3.4% zmv -n '(*)' '${f/_bbb_/_bbb_0}'
mv -- aaa_bbb_1234 aaa_bbb_01234
mv -- bbb_bbb_5678 bbb_bbb_05678
zsh 4.3.4% # remove "-n" to execute
zsh 4.3.4% bash
bash 3.2.13(1)$ for f in *_bbb_*;do
> echo "mv $f ${f/_bbb_/_bbb_0}"
> done
mv aaa_bbb_1234 aaa_bbb_01234
mv bbb_bbb_5678 bbb_bbb_05678
bash 3.2.13(1)$ # remove the echo command to execute
bash 3.2.13(1)$ sh
$ for f in *_bbb_*;do
> echo "mv $f ${f%_bbb_*}_bbb_0${f#*_bbb_}"
> done
mv aaa_bbb_1234 aaa_bbb_01234
mv bbb_bbb_5678 bbb_bbb_05678
$ # remove the echo command to execute

# 6  
Old 08-29-2007
Code:
[/tmp]$ cat txt
aaa_bbb_1234
bbb_bbb_1345
ccc_bbb_1456
[/tmp]$ cat test.ksh
#! /bin/ksh
#
#

while read file
do
    post=${file##*bbb_}
    pre=${file%%$post}
    echo "old file=[$file]"
    echo "new file=[${pre}0${post}]"
    # mv "${file}" "${pre}0${post}"
done < txt
[/tmp]$ ./test.ksh 
old file=[aaa_bbb_1234]
new file=[aaa_bbb_01234]
old file=[bbb_bbb_1345]
new file=[bbb_bbb_01345]
old file=[ccc_bbb_1456]
new file=[ccc_bbb_01456]
[/tmp]$

# 7  
Old 08-29-2007
Quote:
Originally Posted by namishtiwari
i tried this option--

cat file1
aaa_bbb_1234
ccc_bbb_2315
ddd_bbb_9856

sed 's/???_bbb_1234/???_bbb_0{????}/g' file1
sed 's/???_bbb_????/'\(???_bbb_0????\)'/g' file1
sed 's/???_bbb_???/???_bbb_0\(????\)/g' file1

but this option is not working still shwoing the same output.
Can anyon eexplain me why?

Thanks
Namish
Sed actually tries to match the question marks you provided in the regex. Instead of the question mark you should use a . (dot) to match any character. Also in the sed world, a question mark '\?' is used to match zero or one occurrence of the preceding character. From info sed

Code:
`\?'
     As `*', but only matches zero or one.  It is a GNU extension.

Examples:
`a\?b'
     Matches `b' or `ab'.

Using a . would translate to either of

Code:
sed -e "s/\(...\)_bbb_1234/\1_bbb_01234/g" file1
sed -e "s/\(...\)_bbb_\(....\)/\1_bbb_0\2/g" file1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

2. Shell Programming and Scripting

Renaming files

Hello, I am looking for a command line that will rename name files : f700_abc_o_t_MASTERID_AS_AE_20130323.csv like this f700_abc_o_t_MASTERID_AS_AE_20130324.csv The great idea could be to get the date stamp 20130323 and change any part of it, instead of just change the... (4 Replies)
Discussion started by: Aswex
4 Replies

3. Shell Programming and Scripting

renaming files

Hi, I have a list of files in a folder with the same name ending (over 1000 files) joe.jpy.jpeg joe1.jpy.jpeg joe2.jpy.jpeg jon3.jpy.jpeg jor5.jpy.jpeg .....jpy.jpeg etc. I want to change jpy to hhk So the output will be: joe.hhk.jpeg joe1.hhk.jpeg joe2.hhk.jpeg jon3.hhk.jpeg... (3 Replies)
Discussion started by: kylle345
3 Replies

4. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

5. Shell Programming and Scripting

renaming files

Hi all, using a utility image file was named starting with blank space and a blank space in between. I want to rename the files. file names are in the format " sb 12.tif"," sb 13.tif"," sb 14.tif" the files are in thousands. i want to rename as 12.tif, 13.tif, 14.tif.... thanks. (3 Replies)
Discussion started by: ahkverma
3 Replies

6. UNIX for Dummies Questions & Answers

renaming files

I have a list of files named ab_*.csv I would like to remane them all by removing the ab_ and have *.csv I did the following but I am surely missing something. /* wrong script */ for i in `ls -1 ab_*`; do mv ab_$i $i; done Thanks in advance. (1 Reply)
Discussion started by: jxh461
1 Replies

7. UNIX for Dummies Questions & Answers

Renaming files

Hello! I am not familiar with UNIX and I have this problem: I need to move files from a UNIX machine to a PC. UNIX file names contain ":" as special character which is not recognized in a PC. How can I change ":" for "_" in the name of a bunch of files in UNIX? Thanks for your help. (7 Replies)
Discussion started by: Tygoon
7 Replies

8. UNIX for Dummies Questions & Answers

renaming files

directory name = /usr/tom/1997 files - ABC_1997_ST1_BCD.SQL BCD_1997_ST1_EFG_SAB.SQL TTT_EBC_1997_ST1_A.SQL sub directory - /usr/tom/1997/jan a) I want to just rename the all files ending with '.SQL' and also its contents in the 1997 directory(excluding subdirectories eg... (3 Replies)
Discussion started by: systemsb
3 Replies

9. UNIX for Dummies Questions & Answers

renaming the files

Hi All, Today I got a small problem while handling zipped files in PROD support. There are files in this format and I had to grep them reading some contents A.B.gz.C.D where A,B,C and D stand for variables (like FIRST.NAME.gz.MIDDLE.LAST). I know that these files are zipped files and If I... (1 Reply)
Discussion started by: adurga
1 Replies

10. UNIX for Dummies Questions & Answers

renaming files

i have a set of *.lst files. now i want to change the names from "lst" to "dat". how to do it? ex.: -rw-r--r-- 1 rram group 22 Sep 21 13:10 a.lst -rw-r--r-- 1 rram group 22 Sep 21 13:09 b.lst -rw-r--r-- 1 rram group 22 Sep 21 13:10 c.lst... (4 Replies)
Discussion started by: raguramtgr
4 Replies
Login or Register to Ask a Question