If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > insert character in name file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-19-04, 11:07
sant sant is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
insert character in name file

Hello

We have x files in a directory, never they have the same name, and the length name is also different

ie.

pedi04011999999999.txt
salidft2001dfa100104011999999999.txt

I would like change the name insert a character "." , for every eight characters

ie

pedi0401.19999999.99.txt
salidft2.001dfa10.01040119.99999999.txt



hope someone can help.

thanks
Reply With Quote
  #2 (permalink)  
Old 01-19-04, 12:19
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
This assumes that the filenames will not currently contain more than one '.' (and that each has a file extension).

for fileName in *
do

newFileName=$(echo ${fileName%.*} | sed 's/.\{8\}/&\./g').${fileName#*.}
echo mv ${fileName} ${newFileName} #remove 'echo' when you're happy

done

Last edited by Damian Ibbotson; 01-19-04 at 12:21.
Reply With Quote
  #3 (permalink)  
Old 01-20-04, 03:00
sant sant is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
Smile

I happy now, thats work fine.

Thanks Damian
Regards
Reply With Quote
  #4 (permalink)  
Old 01-20-04, 17:53
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi,
there is a little mistake in Damians solution.
If the filename is 8,16,... long then you have two points between the
filename and the extension. Like this :
salidft2001dfa100104011999999999.txt ==> salidft2.001dfa10.01040119.99999999..txt
12345678.txt ==> 12345678..txt

I have another solution for you. It is a little bit larger but it works how you want it.

The same as Damian said :
This assumes that the filenames will not currently contain more than one '.' (and that each has a file extension).

for oldFile in `ls *.txt`
do
newFile=`echo $oldFile | awk ' BEGIN { FS="."}
{
lang=length($1)
x=int(lang / 8)
y=lang / 8
newFile=""
i=0
while (i < x)
{
newFile=newFile""substr($1,i*8+1,8)"."
i=i+1
}
if ( x == y )
{
newFile=newFile""substr($1,i*8+1)""$2
}
else
{
newFile=newFile""substr($1,i*8+1)"."$2
}
print newFile
}'`
#The same as Damian said :
echo mv $oldFile $newFile #remove 'echo' when you're happy
done
__________________
Greetings from germany
Peter F.

Last edited by fla5do; 01-20-04 at 18:51.
Reply With Quote
  #5 (permalink)  
Old 01-21-04, 04:04
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Good point Peter.

Piping the command through sed 's/\.$//' will also fix the double periods.
Reply With Quote
  #6 (permalink)  
Old 01-21-04, 06:37
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Just for the hell of it, you could do the substitution all in one sed command...

echo $fileName | sed '{
:insDot
/[^.]\{9\}/ {
s/\([^.]\{8\}\)\([^.]\)/\1.\2/
b insDot
}
}'

This basically says: if the fileName contains a 9 character string that does not contain a '.', place a '.' after the 8th character. Repeat until the 9 character string is not found.

It's not perfect as for example, the string "123.1234567812345678" would be transformed to "123.12345678.12345678". Sant's original requirements suggest that this should really be "123.1234.56781234.5678".

Damian
Reply With Quote
  #7 (permalink)  
Old 01-21-04, 13:56
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
I know gotos are considered harmful, but wow! There's a lot more to sed than I first reckoned ...
Reply With Quote
  #8 (permalink)  
Old 01-21-04, 14:08
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi Damian,
i am brim over with enthusiasm about your knowledge.
It is realy great.

Hi periods Chillies ;-),
excuse my bad englisch. Now I know that the point is a dot or a period.
Thanks for adjust me.
__________________
Greetings from germany
Peter F.
Reply With Quote
  #9 (permalink)  
Old 01-22-04, 04:38
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Sed is horrible! It does the job *really* quick but even when you write the stuff yourself, it can be a nightmare to figure out what you were trying to do.

Take a look at this (aaagh!)...

http://www.dbnet.ece.ntua.gr/~george/sed/OLD/dc.sed.txt
Reply With Quote
  #10 (permalink)  
Old 01-22-04, 05:16
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Ow! ow! ow!

I think Greg had too much time on his hands, waiting for processes to finish.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On