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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-14-2008
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
Reply With Quote
Forum Sponsor
  #2  
Old 01-14-2008
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,636
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
Reply With Quote
  #3  
Old 01-14-2008
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
Reply With Quote
  #4  
Old 01-14-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
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
Reply With Quote
  #5  
Old 01-14-2008
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
Reply With Quote
  #6  
Old 01-14-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
show the code you have. this time with code tags.
Reply With Quote
  #7  
Old 01-14-2008
Registered User
 

Join Date: Oct 2007
Posts: 19
Script code

Hi ghostdog74,

Here is my code:

Code:
#!/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 -ptest statbackup<<EOFMYSQL

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

            *.99)
		
                         #LOAD the file into Test2 Database

                          mysql -uroot -ptest statbackup<<EOFMYSQL

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

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

    esac
done

#Remove all the files

rm -r /root/test/statbackup/*
After I execute with ./statbackup.ksh, it complaint "syntax error at line 20: '<<' unmatched". If I run with sh -x statbackup.ksh, then it complaint "line 48: syntax error: unexpected end of file". Really stuck with this error, dont have any idea what is happening. Please advise.

Thank you so much.

Br,
Shirley

Last edited by shirleyeow; 01-14-2008 at 08:41 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0