1753322 Members
5976 Online
108792 Solutions
New Discussion юеВ

Shell Scripting

 
SOLVED
Go to solution
Virumandi
Frequent Advisor

Shell Scripting

Hi

Any body help me in solving the issue
there are lot of files with .txt extension inside my folder.I want to create a script such that all the files ending with .txt needs to convert into .xls

The shell may be bash or any .NO PROBLEM

Anybody Help me pls

12 REPLIES 12
Yang Qin_1
Honored Contributor
Solution

Re: Shell Scripting

Hi,

I believe you want to rename those files instead of convert.
If all .txt files are in the same directory:

#!/usr/bin/ksh

for fn in *.txt
do
newfn=`echo $fn|sed 's/txt/xls/'`
mv $fn $newfn
done

exit

Regards,
Yang

Doug O'Leary
Honored Contributor

Re: Shell Scripting

That's funny!

I actually use this exact problem as one of my interview questions...

(correct answer, btw)

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Peter Nikitka
Honored Contributor

Re: Shell Scripting

Hi,

I'm soory but I must say that Yangs solution is not correct:

Having a file named
fn=allmytxtbooks.txt
newfn=`echo $fn|sed 's/txt/xls/'`

will lead to
allmyxlsbooks.txt

I suggest plain shell builtins, so no extra process is needed:

for fn in *.txt
do
mv $fn ${fn%*.txt}.xls
done

Error checking is left to the reader as an exercise :-)

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: Shell Scripting

If you are in the folder where the *.txt files are...

# ls -1 *.txt | awk -F. '{system("mv "$0" "$1".xls")}'
H.Merijn Brand (procura
Honored Contributor

Re: Shell Scripting

Wrong answer at an interview :)

The correct answer would be a new question:

Only at the current level, or recursive from the current folder?
And is the .txt extension case sensitive or not?
And are the .txt file containing CSV data, and do they have to be converted to real xls, or is a name change enough?
And should the new axtension match the casing of the old extension, or should it be all upper or lower case?
Are empty file to be renamed too? (empty .txt is valid text, empty .xls is not valid M$ Excel)
What is the host system, and should the script be portable (rename commands on VMS, M$Win, and *nix are quite different)

Too many questions to make a correct answer to this question.

$ perl -MFile::Find -MFile::Copy -e'find(sub{-s&&m/\.txt$/i or return;($n=$_)=~s/txt$/xls/i;move$_,$n},".")'

Addresses all above problems in one line.
Analysis is left an excercise to the reader.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: Shell Scripting

dead horse, but still...

System forks a process.
Find for a handful files, slow for thousands.
Personally I prefer the perl rename buildin:

For example, with the shell finding the candidate files:

perl -e 'while ($f = shift @ARGV) { $_ = $f; s/.tmp$/.xxx/; rename $f,$_ }' *.tmp

Other example with perl's glob finding the files....

perl -e 'while (<*.tmp>) { $old = $_; s/.tmp$/.xxx/; rename $old,$_ }'

Cheers,
Hein.
Doug O'Leary
Honored Contributor

Re: Shell Scripting

Holy crap! Some of you, procura especially, read *way* too much into a question.

It's the correct answer and gets the information I'm looking for in the intervew - specifically, can the person do a loop and does he or she know that you can't simply mv *.txt *.xls...

I did catch the fact that it'd catch xls in the middle of the filename; my usual way to do this is to use ${f%.*} just to strip off the extension. I would expect an admin that's working this problem to know his data; meaning he'd know if there was an xls in the middle of a set of filenames...

Since it's one question out of many, I'm sure I'll get whether or not the guy has the requisite attention to detail as well...

As for the rest of the answers, yet another proof that there's always multiple ways to solve problems in UNIX... Personally, I'd think using perl for the problem as stated would be akin to killing a fly with a small thermonuclear device, but if it works, have at it...

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Doug O'Leary
Honored Contributor

Re: Shell Scripting

Holy crap! Some of you, procura especially, read *way* too much into a question.

It's the correct answer and gets the information I'm looking for in the intervew - specifically, can the person do a loop and does he or she know that you can't simply mv *.txt *.xls...

I did catch the fact that it'd match xls in the middle of the filename; The better answer given is my usual way to do this; use ${f%.*} just to strip off the extension. It does, however, do what the OP requested... I would expect an admin that's working this problem to know his data; meaning he'd know if there was an xls in the middle of a set of filenames...

Since it's one question out of many, I'm sure I'll get whether or not the guy has the requisite attention to detail as well...

As for the rest of the answers, yet another proof that there's always multiple ways to solve problems in UNIX... Personally, I'd think using perl for the problem as stated would be akin to killing a fly with a small thermonuclear device, but if it works, have at it, although the awk solution is interesting... I never think to use awk that way..

Doug


------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Doug O'Leary
Honored Contributor

Re: Shell Scripting

Holy crap! Some of you, procura especially, read *way* too much into a question.

It's the correct answer and gets the information I'm looking for in the intervew - specifically, can the person do a loop and does he or she know that you can't simply mv *.txt *.xls...

I did catch the fact that it'd match xls in the middle of the filename; The better answer given is my usual way to do this; use ${f%.*} just to strip off the extension. It does, however, do what the OP requested... I would expect an admin that's working this problem to know his data; meaning he'd know if there was an xls in the middle of a set of filenames...

Since it's one question out of many, I'm sure I'll get whether or not the guy has the requisite attention to detail as well...

As for the rest of the answers, yet another proof that there's always multiple ways to solve problems in UNIX... Personally, I'd think using perl for the problem as stated would be akin to killing a fly with a small thermonuclear device, but if it works, have at it (although the awk solution is interesting).

Doug


------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html