The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-18-2007
inditopgun inditopgun is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 17
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!
  #2 (permalink)  
Old 07-18-2007
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
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
  #3 (permalink)  
Old 07-18-2007
inditopgun inditopgun is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 17
thanks!!

thanks Unix-god, the solution you gave works like a charm! though I couldn't figure out some of the intricate regexp embedded inside ;-))
  #4 (permalink)  
Old 07-20-2007
inditopgun inditopgun is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 17
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0