- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell Scripting
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2006 11:07 PM
тАО09-28-2006 11:07 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2006 11:20 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-29-2006 01:32 AM
тАО09-29-2006 01:32 AM
Re: Shell Scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-30-2006 10:35 PM
тАО09-30-2006 10:35 PM
Re: Shell Scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-01-2006 08:40 AM
тАО10-01-2006 08:40 AM
Re: Shell Scripting
# ls -1 *.txt | awk -F. '{system("mv "$0" "$1".xls")}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-01-2006 09:16 AM
тАО10-01-2006 09:16 AM
Re: Shell Scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-01-2006 09:21 AM
тАО10-01-2006 09:21 AM
Re: Shell Scripting
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-02-2006 07:39 AM
тАО10-02-2006 07:39 AM
Re: Shell Scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-02-2006 07:53 AM
тАО10-02-2006 07:53 AM
Re: Shell Scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-02-2006 07:55 AM
тАО10-02-2006 07:55 AM
Re: Shell Scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-02-2006 08:06 AM
тАО10-02-2006 08:06 AM
Re: Shell Scripting
Doug
------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-02-2006 09:04 AM
тАО10-02-2006 09:04 AM
Re: Shell Scripting
My environment arises the *need* to be portable across OS's (HP-UX, AIX, Linux, Cygwin, Win32), and accross databases (Oracle, Unify, postgres, Mysql), so you learn to program in a very defensive style, and never use system calls if either modules or builtins are available that enable you to do the same in a portable way.
My job is to be alert to any of these hidden questions and problems in questions from customers. "Can you please do A for me?" And they mean "B", but we should have known, as they always do so :) (wink wink)
If I interview someone on unix knowledge, I most of the time do not expect a `real' answer. I expect either the questions I asked, or a question like "what does the company prefer? bash, awk, sed, perl, csh, python?"
As Hein said: It's a dead horse. There are a zillion options to `fix' it or deal with it. It's just the question of how to do it reliable and the way no new errors arise.
Happy hacking!
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2006 04:43 PM
тАО10-05-2006 04:43 PM
Re: Shell Scripting
The query has been resolved a little Earlier..
But I didnt updated the Ticket.But however I found that it became one of the interesting topic for the people to Talk..
Good...
Regards
Suseendran .A