Help with awk/shell script needed!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk/shell script needed!
# 1  
Old 07-18-2007
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  
Old 07-18-2007
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  
Old 07-18-2007
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  
Old 07-20-2007
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script - help needed

I want to take out the Z1 value from the lscfg outpu and use the below command to get it lscfg -vl hdisk0 | grep "Device Specific.(Z1)" | awk -F. '{print $NF}' # lscfg -vpl hdisk0 . . Device Specific.(Z0)........0000063268181002 Device Specific.(Z1)........020064a . And it works,... (2 Replies)
Discussion started by: moorthikv
2 Replies

2. Shell Programming and Scripting

Help Needed with Shell Script

I need help writing a Unix shell script that does the following: 1) Searches all of the files in a given directory that have the .acl file extension 2) Redirects the output of the search to a column in a .xls or .cvs file (Note: The column name in the spreadsheet file needs to be named... (2 Replies)
Discussion started by: ijmoore
2 Replies

3. Shell Programming and Scripting

Needed shell script for the following

hi all, i want the script for the following my input is date mm-dd-yyyy i.e 1-4-2012(it doesn't have zeros in the input ,in output it should append zeros and should print the output as defined below) my output should be 2012-04-01(yyyy-dd-mm) if my input is 11-14-2012 then my should be... (7 Replies)
Discussion started by: hemanthsaikumar
7 Replies

4. UNIX for Dummies Questions & Answers

Help needed with Shell script

Hi I want a script which should basically do 1. If the size of the file is 0kb, send email to some list od ppl 2. if the size of the file is other than 0kb send email to someother list... Pls help (2 Replies)
Discussion started by: win4luv
2 Replies

5. Shell Programming and Scripting

Shell script help is needed

I have a file test.txt and i need to grep pattern "A.17" from that file. I know cat test.txt | grep A.17 will return the pattern, but it is returing like # VERSION=A.17 How can i take only A.17 from this if A.17 is found, ... do something if not found ... do something Please... (11 Replies)
Discussion started by: Renjesh
11 Replies

6. Shell Programming and Scripting

Help with Shell Script Needed

Hi all, I'm a newbie. I'm thinking of making a shell script which will list a directory for *.csv files and merge all the files into a single file called data.csv. However, i have no idea how to. What i do currently is to manually issue the following command to merge multiple CSV files into... (1 Reply)
Discussion started by: infinity2030
1 Replies

7. Shell Programming and Scripting

help needed for a shell script

i need to search the starting line example we have -sh shl-js-gd i need to search only starting -sh not the other i have used cmd cat filename | grep '-' but it will check for complete - in the file please help me to search only starting - thank u revenna (0 Replies)
Discussion started by: revenna
0 Replies

8. Shell Programming and Scripting

script help needed --- awk?

hello and thanks in advance. I need to consolidate 2 records of data into 1 record. here is a sample of the input: ------------------------------------------------------------------------------------------ totalcount fred thomas 99999.00 88888:00 total 77777.00... (7 Replies)
Discussion started by: scuderiaf1
7 Replies

9. UNIX for Dummies Questions & Answers

shell script Help Needed!!

Hi guys-- Here is the interesting problem.. I have a folder, which has a couple of xml files like ( could be anywhere under the sub folders) ABCD_NA.xml XAYSGD_SC.xml CBV_CA.xml etc I need to peek in each of these xml's and see that the pattern before the _ and after the _ are there... (0 Replies)
Discussion started by: smallu
0 Replies

10. Shell Programming and Scripting

shell script help needed

I am trying to query a table having 3 columns, the third column is a field of varchar(1024) with a SQL string in it. I am using cut command to split out the three fields into three variables. I do a db2 command to extract the data into a file. My problem is with the third field having the SQL... (3 Replies)
Discussion started by: fastgoon
3 Replies
Login or Register to Ask a Question