Issue
I have a file in Linux
. This file contains table names in it.
Now I want to check this file and create files based on condition.
table=${}
validateTable=$(hive --database "${hivedb}" -e "SHOW TABLES LIKE '$table'")
if [[ -z $validateTable ]]; then
Add to file New_imports
else
Add to file Already exists
fi
For example:
The file contains
table1
table2
table3
table4
In the above tables table1
and table2
are already existing.
So I want two files
1) New_imports for tables that don't exist 2) Already exists for tables that exist
new_imports
table3
table4
already exists
table1
table2
How can I achieve my result
#!/bin/bash while read table ; do table=${1:1:-1} validateTable=$(hive --database "${hivedb}" -e "SHOW TABLES LIKE '$table'") if [[ -z $validateTable ]]; then echo "$table" >> New_imports else echo "$table" >> Already_exists fi done < tableFile
Solution
#!/bin/bash
while read table ; do
table=$(echo "$table" | sed 's/[][]//g;s/'"'"'//g')
validateTable=$(hive --database "${hivedb}" -e "SHOW TABLES LIKE '$table'")
if [[ -z $validateTable ]]; then
echo "$table" >> New_imports
else
echo "$table" >> Already_exists
fi
done < tableFile
Should get your started. Making this bullet-proof and so it can accept arguments will take some more doing.
Managing the output files may take some doing. Note that with using >>
means append, so each time you run this, you either need to delete those files, or manage them. I'd recommend keeping them with date-time stamps embedded in the file name like
echo "$table" >> new_imports.$(/bin/date +%Y-%m-%d.%H:%M)
Also, I don't have anyway to test this with hive
, so this is a general solution.
Answered By - shellter Answer Checked By - Candace Johnson (WPSolving Volunteer)