Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitin file names

Status
Not open for further replies.

jamsman

Technical User
Jul 22, 2005
38
DE
i have a list of file names that i need to split on the "." and put the resulting two parts in to two variables

any ideas thank you

im a complete newbe to unix scripting so simple exmaples would be good thank you again :)
 
Hi

You wrote list, so I supposed that is in a file.
Code:
IFS=$' \t\n.'
while read name ext; do
  echo $name - $ext
done < list

[gray]# or[/gray]

while read str; do
  echo ${str%\.*} - ${str##*\.}
done < list

[gray]# or[/gray]

while read str; do
  name=`echo $str | cut -d. -f1`
  ext=`echo $str | cut -d. -f2`
  echo $name - $ext
done < list

Feherke.
 
Try this:
Code:
# Assumming variable $TheFile contain the file name, then:
${TheFile%%.*} #<= This gives the file name
${TheFile##*.} #<= This give the suffix.
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
is this of any use as a starting point?

Code:
[b]#!/usr/bin/perl[/b]

@filenames = `ls -1`;

foreach (@filenames) {
  ($name, $extension) = split(/\./);
  print "$name | $extension\n";
}


Kind Regards
Duncan
 
ok sorry the "list" i said about is the result of:
for file `ls $dir`
do

done

style loop i would like to put the split function in the loop can you use arrays in shell scripts??
 
Jamsman,
The question is really, why do you want to do this?
If you explain why then we may be able to help you more or indeed solve the complete problem for you.


Trojan.
 
the reaons i wish to split the file name is to the insert an id into the filename after the name but before the extension then mv the original file to the newly created filename :)

thanks
 
Where do you get the id from?
Is it the same id for each file or is it filename depenant or a counter?


Trojan.
 
the id is the same for each record the id is supplied by the user
 
Hi

I mention it just because it is the most simple solution. Will not work well on files with multiple dots in the name.
Code:
rename "." "$id." "$dir/"*
But is very possible, to not have [tt]rename[/tt], or to have other variant with different syntax.

Feherke.
 
Code:
ls *.txt | perl -e '$a=shift;while(<>){chomp;$x=$_;s/^(.*)(\.[^.]+)$/$1$a$2/;`mv $x $_`;}' ID
That should move all *.txt files to *ID.txt (I assume that's what you want).
Please test carefully first in a dummy directory with dummy files.


Trojan.
 
LOL!
Old habits dies hard I guess.
And I could probably have done it just as easily in shell.
Point taken though, thanks.


Trojan.
 

Try this:
Code:
for file in `ls $dir`
do
  mv $file ${file%%.*}${ID}${file##*.}
done

[2thumbsup]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

LKBrwnDBA, there is a little problem. [tt]ls[/tt] returns the filenames without the path, so if [tt]$dir[/tt] is not pointing to the current directory, [tt]mv[/tt] will fail.
Code:
for file in `ls $dir`
do
  mv [red]$dir/[/red]$file [red]$dir/[/red]${file%%.*}${ID}${file##*.}
done

Feherke.
 
Also, if there is more than one dot in the name, part of the name will be lost. That is, "[tt]this.is.my.file.txt[/tt]" would be changed to "[tt]this${ID}txt[/tt]".

It also loses the dots.

This will fix both of those...
Code:
for file in `ls $dir`
do
  mv $dir/$file $dir/${file%.*}.${ID}.${file##*.}
done
The dots being inserted can be added or removed as needed. BUT, with this, if [tt]ID=123[/tt], and the file name is "[tt]file.txt[/tt]", it will be renamed to "[tt]file.123.txt[/tt]".

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top