SQL Loader INFILE with file in a different path - sh

I have a SQL Loader Control File in the directory /opt/TEST/app/ITT/script/ctl/.
The input file is in the directory /opt/TEST/app/ITT/in/.
The SQL Loader Control File can't stay in the same directory of the input file, so I must take it putting the right directory.
Is it correct to write this code in the control file?
LOAD DATA
INFILE '/opt/TEST/app/ITT/in/FILE1.ready'
BADFILE '/opt/TEST/app/ITT/in/FILE1.bad'
DISCARDFILE '/opt/TEST/app/ITT/in/FILE1.dsc'
I would prefer to do something easier without writing the whole directory, but just to got back from /ctl/ to /ITT/ and then to enter in the /in/ directory but I don't know how to do it.

find /u01/*/*/tmp/*_OCI/*.csv | while read INFILE
do
echo load data > /u01/tmp/temp_ctl.ctl
echo infile "'$INFILE'" >> /u01/tmp/temp_ctl.ctl
echo append into table cons_sqldata >> /u01/tmp/temp_ctl.ctl
echo fields terminated by "','" >> /u01/tmp/temp_ctl.ctl
echo '('db_name, >> /u01/tmp/temp_ctl.ctl
echo cons_grp, >> /u01/tmp/temp_ctl.ctl
echo sql_count, >> /u01/tmp/temp_ctl.ctl
echo sql_execs')' >> /u01/tmp/temp_ctl.ctl
chmod 755 /u01/tmp/temp_ctl.ctl
echo
echo invoking the sql*loader
echo using table $INFILE
sqlldr userid=test/test#123#test_f control=/u01/tmp/temp_ctl.ctl
echo "****************************************************************"
done

According to this documentation, the INFILE path has to be surrounded by single qotes:
https://docs.oracle.com/cd/B10501_01/server.920/a96652/ch05.htm#1008018
Example:
INFILE 'c:/topdir/subdir/datafile.dat'

Related

Is there a way to make a powershell script from this cmd example?

I am currently using the below as part of a batch script to backup files over FTP. I am working on making a powershell version of this and am having trouble finding the right commands to take the place of this section. Does anyone have any suggestions? Ive been working on this for a while now with no luck.
ping %IP%
echo open %IP%>> temp.txt
echo >> temp.text
echo >> temp.text
echo bin>> temp.txt
echo prompt>> temp.txt
echo mget *.*>> temp.txt
echo bye>> temp.txt
ftp -n -s:temp.txt
:del temp.txt
What is this actually supposed to do ? Just test if PC is up, then transfer a file via FTP ?

SH - Save command with parameter in variable

Im trying make an script which can find and store specific type files in a folder.
When I run myScript.sh:
$ ./myScript.sh *.txt
It should save in files.txt all files with .txt extension but doesn't work for me. Only save first file.
myScript.sh:
var=`find $1`
for file in $var
do
echo $var >> files.txt
done
That's an example for practice
Do it like this:
for file in $#
do
echo "$file" >> files.txt
done
Using $#, you can get all the arguments in the script that you pass.
You don't need a loop at all. Just redirect the output of find to your file.
find . "$1" >> files.txt
Finally, i fix this issue passing .txt instead *.txt and changing my script like this:
$ ./myScript.sh *.txt
var=`find *$1`
for file in $var
do
echo $var >> files.txt
done
It works for me.
Thank you all for answer!
I don't understand why you are passing arguments to the script at all. If you want all the files in the current directory whose name ends in ".txt", you could do either:
ls *.txt > files.txt
or
find . -name '*.txt' > files.txt # descends the directory tree
or (not standard)
find . -maxdepth 1 -name '*.txt' > files.txt
None of these will handle files with embedded newlines particularly well, but that use case is not handled well when storing names in a file.

Import CSV to PostgreSQL programmatically

I have a csv with 90 columns that I need to import as a table to my pgsql database (and there are several more csv files with large numbers of columns that I would like to apply this method to). My aim is to avoid manually designating 90 separate columns with a CREATE TABLE query.
Column headers in the table should remain the same as in the csv and every column should be imported as a numeric data type with a precision of 2 decimal points.
So far, the only program that I've come across that does this is pgfutter which I have installed successfully. However, the database that I am connecting to is a remote one on AWS and it is unclear where to input the connection details. Also, after installing, I get an error when requesting help info:
$ ./pgfutter --help
-bash: ./pgfutter: Permission denied
Could anyone suggest a workaround in pgfutter or another method to import a csv file with straightforward numeric columns automatically to PostgreSQL ?
It is simple to write a shell script that constructs a CREATE TABLE statement from the first line of a CSV file.
Here is my solution:
#!/bin/bash
# makes a CREATE TABLE statement out of the first line of a CSV file
# usage: mktab <tabname> <CSV file>
if [ -z "$2" -o -n "$3" ]; then
echo "usage: mktab <tabname> <CSV file>" 1>&2
exit 1
fi
IFS=,
first=1
echo -n "CREATE TABLE \"$1\" ("
for col in $(head -1 "$2"); do
if [ $first -eq 1 ]; then
first=0
else
echo -n ', '
fi
echo -n "\"$col\" numeric(10,2)"
done
echo ');'
exit 0

Enclosing the content of a text file in element tags and writing the output to an XML file

How can I prepend a start-tag to the content of a text file, append it with an end-tag and redirect the output to an XML file in command line?
For DOS:
echo "<tag>" > file.xml
type file.txt >> file.xml
echo "</tag>" >> file.xml
> will create the file, or truncate it if it exists, and >> will append to it.

Bourne Shell Script

I'm attempting to write a script in the Bourne shell that will do the following:
Read in a filename
If the file does not exist in the target directory, it will display a message to the user stating such
If the file exists in the target directory, it will be moved to a /trash folder
If the file exists in the target directory, but a file of the same name is in the /trash folder, it will still move the file to the /trash directory, but will attach a _bak extention to the file.
My use of the Bourne shell is minimal, so here's what I have so far. Any pointers or tips would be greatly appreciated, thanks!
#!/bin/sh
#Scriptname: Trash Utility
source_dir=~/p6_tmp
target_dir=~/trash
echo "Please enter the filename you wish to trash:"
read filename
if [ -f $source_dir $filename]
then mv "$filename" "$target_dir"
else
echo "$filename does not exist"
fi
You cannot use ~ to refer to $HOME in a sh script. Switch to $HOME (or change the shebang to a shell which supports this, such as #!/bin/bash).
To refer to a file in a directory, join them with a slash:
if [ -f "$source_dir/$filename" ]
Notice also the required space before the terminating ] token.
To actually move the file you tested for, use the same expression for the source argument to mv:
mv "$source_dir/$filename" "$target_dir"
As a general design, a script which takes a command-line parameter is much easier to integrate into future scripts than one wich does interactive prompting. Most modern shells offer file name completion and history mechanisms, so a noninteractive script also tends to be more usable (you practically never need to transcribe a file name manually).
A Bash Solution:
#!/bin/bash
source_dir="~/p6_tmp"
target_dir="~/trash"
echo "Please enter the filename you wish to trash:"
read filename
if [ -f ${source_dir}/${filename} ]
then
if [ -f ${target_dir}/${filename} ]
then
mv "${source_dir}/${filename}" "${target_dir}/${filename}_bak"
else
mv "${source_dir}/${filename}" "$target_dir"
fi
else
echo "The file ${source_dir}/${filename} does not exist"
fi
Here's the completed script. Thanks again to all who helped!
#!/bin/sh
#Scriptname: Trash Utility
#Description: This script will allow the user to enter a filename they wish to send to the trash folder.
source_dir=~/p6_tmp
target_dir=~/trash
echo "Please enter the file you wish to trash:"
read filename
if [ -f "$source_dir/$filename" ]
then
if [ -f "$target_dir/$filename" ]
then mv "$source_dir/$filename" "$target_dir/$(basename "$filename")_bak"
date "+%Y-%m-%d %T - Trash renamed ~/$(basename "$source_dir")/$filename to ~/$(basename "/$target_dir")/$(basename "$filename")_bak" >> .trashlog
else mv "$source_dir/$filename" "$target_dir"
date "+%Y-%m-%d %T - Trash moved ~/$(basename "/$source_dir")/$filename to ~/$(basename "/$target_dir")/$filename" >> .trashlog
fi
else
date "+%Y-%m-%d %T - Trash of ~/$(basename "/$source_dir")/$filename does not exist" >> .trashlog
fi