![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help needed for Shell Script..... | pulkit | Shell Programming and Scripting | 0 | 02-22-2008 07:25 AM |
| shell script Help Needed!! | smallu | UNIX for Dummies Questions & Answers | 0 | 01-15-2008 08:40 PM |
| Help needed in Shell Script | nvuradi | Shell Programming and Scripting | 1 | 10-24-2007 05:56 AM |
| shell script help needed | fastgoon | Shell Programming and Scripting | 3 | 07-17-2006 04:24 PM |
| Shell Script needed urgently | vas_dba | Shell Programming and Scripting | 2 | 03-11-2005 09:00 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Help with awk/shell script needed!
I have a shell script which reads a file named filelist.txt, containing a list of fully qualified filenames (with complete path), and then does some processing based on the file extension. In order to do the processing, I have to know the file extension, the filename without extension and just the filename_with_extension(excluding the full path)
some sample lines in the filelist.txt looks like this /home/johndoe/test/a.java /home/rwilliams/test2/b.xml My script looks like below (basically I am using awk and relying on "." to give me the extension and filename_without_extension #!/bin/sh for cfilename in `cat filelist.txt` do file_extension=`echo $cfilename | awk -F\/ '{print $NF}' | awk -F. '{print $NF}'` export file_extension filename_wo_ext=`echo $cfilename | awk -F\/ '{print $NF}' | awk -F. '{print $1}'` export filename_wo_ext filename_with_ext=$filename.$file_extension export filename_with_ext <processing logic> done The problem is this script works fine for normal files with names like /home/johndoe/test/a.java however whenever I encounter files with names like /home/john.doe/test/abcd.efgh.ijkl.mnop.qrst.xml the script throws up as it relies heavily on the "." character embedded in the name to be the separator between the extension and filename. Any help will be really appreciated! |
|
||||
|
1) there are no such things as extensions in UNIX. a period is just part of the filename.
2) This might give you some ideas on how to handle: for F in /home/johndoe/test/a.java /home/rwilliams/test2/b.xml /home/johndoe/test/a.java /home/john.doe/test/abcd.efgh.ijkl.mnop.qrst.xml do echo $F echo $(basename $F) echo $(dirname $F) echo $(basename $F | awk -F. -v OFS=. '{$NF=""; sub("[.]$", ""); print}') echo $(basename $F | awk -F. '{print $NF}') done ----------------------------- OUTPUT ----------------------------- /home/johndoe/test/a.java a.java /home/johndoe/test a java /home/rwilliams/test2/b.xml b.xml /home/rwilliams/test2 b xml /home/johndoe/test/a.java a.java /home/johndoe/test a java /home/john.doe/test/abcd.efgh.ijkl.mnop.qrst.xml abcd.efgh.ijkl.mnop.qrst.xml /home/john.doe/test abcd.efgh.ijkl.mnop.qrst xml |
|
||||
|
still need help with the awk script
The solution proposed above returns file extension and filename excluding the full path and the extension, ONLY if the format of the filename is something like /home/john.doe/test.java or /home/john.doe/test.test1.test2.test3.java;
However unfortunately whenever it encounters a filename without an extension e.g. /home/john.doe/runjava then the script returns runjava as the file extension instead of returning null as file extension and the filename excluding the full path and the extension is returned as null instead. (kinda it's doing a complete switcheroo) Is there a way to handle this? because I just realized that there are quite a few executables I have in my filelist.txt without any extensions as such (like /bin/gzip, /usr/local/bin/perl etc.) and my script just craps out when it encounters them. Any help will be really appreciated! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|