The UNIX and Linux Forums  


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
How do i check whether a file has extension? sunday8 Shell Programming and Scripting 2 08-29-2008 10:58 PM
how to unzip File which has no extension thepurple SUN Solaris 13 11-29-2007 05:46 AM
Stripping out the extension of a file name ramky79 Shell Programming and Scripting 2 12-27-2006 02:25 PM
Check file extension mahalakshmi Shell Programming and Scripting 6 12-27-2006 01:15 PM
default extension of file rujupriya UNIX for Dummies Questions & Answers 2 05-17-2006 11:49 AM

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 01-14-2008
shirleyeow shirleyeow is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 19
How to get file extension

Hi all,

I am writing a script to do some matching for the filename. Let say there are multiple files in a directory, the filename is like below

test.88
test2.88
test.99
test2.99
test3.99

For different file extension, different action will be taken. For example test.88 and test2.88 files will be load into one database, whereas test.99, test2.99 and test3.99 will be load into another database.

I tried to use the nawk to get the file extension like below but is not working.

for filename in `ls -l`
do
nawk 'BEGIN { FS="." }; { print $2 }' $filename
done


The print $2 didn't print the 88 or 99 extension. It just return me an empty value. Does anyone know what is going wrong here. Please advise.

Thank you so much.

BR,
Shirley
  #2 (permalink)  
Old 01-14-2008
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,245
That is not a good way to process files. However the problem is that you used ls -l and assumed to always find 2 fields, on the first line of ls -l you will only have one field.

One better way would be:

Code:
for file in * ; do
    case $file in 
        *.88)
               do something;;
        *.99)
               do something else;;
    esac
done

  #3 (permalink)  
Old 01-14-2008
shirleyeow shirleyeow is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 19
Unexpected end of file

Hi Reborg,

Here is my complete script.

#!/bin/ksh

DIR="/root/test/statbackup"

# Checking whether the directory exist or not

if [ -d "$DIR" ]; then
cd $DIR
else
print "ERROR: Directory doest not exist!"
fi

for file in * ; do
case $file in
*.88)

#LOAD the file into Test1 Database

mysql -uroot -ptecnomen teststat<<EOFMYSQL

LOAD DATA LOCAL INFILE '$DIR/$file' INTO TABLE test1 FIELDS TERMINATED BY '|'
IGNORE 1 LINES;
;;

*.99)

#LOAD the file into Test2 Database

mysql -uroot -ptecnomen teststat<<EOFMYSQL

LOAD DATA LOCAL INFILE '$DIR/$file' INTO TABLE test2 FIELDS TERMINATED BY '|'
IGNORE 1 LINES;
;;

*)
print "Usage: $file is not in correct format"
;;

esac
done

#Remove all the statbackup file

rm -r /root/test/statbackup/*


After I execute the script, I get error "syntax error:unexpected end of file".
I tried go into the script and the line that script complaint actually is not occur, meaning one line more than the maximum script line. I really dont know how to solve on this. Please advise.

Thank you so much.

BR,
Shirley
  #4 (permalink)  
Old 01-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
Quote:
Originally Posted by shirleyeow View Post
Hi Reborg,

Here is my complete script.

#!/bin/ksh

DIR="/root/test/statbackup"

# Checking whether the directory exist or not

if [ -d "$DIR" ]; then
cd $DIR
else
print "ERROR: Directory doest not exist!"
fi

for file in * ; do
case $file in
*.88)

#LOAD the file into Test1 Database

mysql -uroot -ptecnomen teststat<<EOFMYSQL

LOAD DATA LOCAL INFILE '$DIR/$file' INTO TABLE test1 FIELDS TERMINATED BY '|'
IGNORE 1 LINES;
;;

*.99)

#LOAD the file into Test2 Database

mysql -uroot -ptecnomen teststat<<EOFMYSQL

LOAD DATA LOCAL INFILE '$DIR/$file' INTO TABLE test2 FIELDS TERMINATED BY '|'
IGNORE 1 LINES;
;;

*)
print "Usage: $file is not in correct format"
;;

esac
done

#Remove all the statbackup file

rm -r /root/test/statbackup/*


After I execute the script, I get error "syntax error:unexpected end of file".
I tried go into the script and the line that script complaint actually is not occur, meaning one line more than the maximum script line. I really dont know how to solve on this. Please advise.

Thank you so much.

BR,
Shirley

Put your code in code tags, and if possible, indent your code for readability.
back to your script, you used HERE document, << EOFMYSQL, but you left out the closing pair.

Code:
case .....
...
     LOAD DATABASE ... <<EOFMYSQL
....
...
EOFMYSQL
..
;;
...
esac

  #5 (permalink)  
Old 01-14-2008
shirleyeow shirleyeow is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 19
Same error still prompt out

Hi ghostdog74,

Thank you so much for your advise.

The code become so messy and difficult to read after I copy it into here.
So sorry about it.

I added in the closing pair for EOFMYSQL and tried to execute the script. The same error still prompt out.

Is it ok to load the data into databse by using Case statement?

Tried to execute the script by sh -x statbackup.ksh, but it didnt show where is the error, just prompt "unexpected end of file". Please advise.

Thank you so much.

Br,
Shirley
  #6 (permalink)  
Old 01-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
show the code you have. this time with code tags.
Closed Thread

Bookmarks

Tags
mtime

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 09:25 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