Unix: Automate replacing specific words in all the files
The script mentioned below is a shell script intended to automate the process of modifying control files and loading data into an Oracle database using SQL*Loader. Let's break down the script step by step:
Do you need further help? Contact us
Script Explanation
1. Korn Shell Line:
#!/bin/ksh
- Specifies that the script should be executed using the Korn shell (`ksh`).
2. List and Process Control Files:
```bash
ls -ltr *.ctl | awk -F" " '{print $9}'|awk -F"." '{print $1}' >> load_files
```
- `ls -ltr *.ctl`: Lists all `.ctl` files (control files used by SQL*Loader) in the current directory, sorted by modification time.
- `awk -F" " '{print $9}'`: Uses `awk` to extract the ninth field (the filename), assuming fields are separated by spaces.
- `awk -F"." '{print $1}'`: Further processes each line to extract the filename without the `.ctl` extension.
- `>> load_files`: Appends the filenames to a file named `load_files`.
3. Read Each Filename and Process:
```bash
while read a
do
...
done < load_files
```
- Reads each line from the `load_files` file and processes it in the loop.
4. Define Filenames:
b=$a."ctl"
c=$a"1"."ctl"
- Constructs the original control filename (`b`) and a new filename (`c`) by appending `1` before the `.ctl` extension.
5. Modify and Create New Control File:
sed 's/C:\\Users\\Documents\\Projects\\DB_DUMP\\Pipeline_DB\\//g' $b >> $c
- Uses `sed` to remove a specific path from the content of the original control file (`b`) and writes the modified content into a new control file (`c`).
6. Print SQL*Loader Command:
printf "sqlldr integrate/password control=$c \n"
- Prints the SQL*Loader command that will be executed, useful for debugging or logging.
7. Execute SQL*Loader:
sqlldr integrate/password@PINDB control=$c;
- Executes SQL*Loader using the newly created control file (`c`). Assumes `integrate` as the username, `password` as the password, and `PINDB` as the database SID or service name.
8. Print Newlines for Formatting:
printf "\n \n\n \n"
- Prints a series of newline characters for better readability in the output.
Summary
The script automates the process of:
- Finding control files in the current directory.
- Creating modified versions of these files by removing a hardcoded file path.
- Loading data into an Oracle database using the modified control files with SQL*Loader.
This script is particularly useful when you need to repeatedly load data from multiple control files into a database, but first need to adjust the file paths within those control files. Remember to replace `integrate/password@PINDB` with your actual database credentials and connection string.
- #!/bin/ksh
- ls -ltr *.ctl | awk -F" " '{print $9}'|awk -F"." '{print $1}' >> load_files
- while read a
- do
- b=$a."ctl"
- c=$a"1"."ctl"
- sed 's/C:\\Users\\mmgude\\Documents\\Projects\\DB_DUMP\\Pipeline_DB\\//g' $b >> $c
- printf "sqlldr integrate/password control=$c \n"
- sqlldr integrate/password@PINDB control=$c;
- printf "\n \n\n \n"
- done < load_files
Comments
Post a Comment