Replace array of string ( passed as argument to script ) replace those values in a HQL file using Bash shell script? - bash

I have a script which accepts 3 arguments $1 $2 $3
but $3 is an array like ("2018" "01")
so I am executing my script as :
sh script.sh Employee IT "2018 01"
and there an HQL file ( emp.hql) in which I want to replace my partition columns with the array passed like below :
***"select deptid , employee_name from {TBL_NM} where year={par_col[i]} and month={par_col[i]}"***
so below is the code I have tried :
**Table=$1
dept=$2
Par_cols=($3)
for i in "${par_cols[#]}" ;do
sed -i "/${par_col[i]}/${par_col[i]}/g" /home/hk/emp.hql**
done
Error :
*sed: -e experssion #1 , char 0: no previous regular expression*
*sed: -e experssion #2 , char 0: no previous regular expression*
But I think logic to replace partition columns is wrong , could you please help me in this?
Desired Output in HQL file :
select deptid ,employee_name from employee where year=2018 and month=01
Little bit related to below like :
Shell script to find, search and replace array of strings in a file

Related

how to read contents of a file coming as a single argument to shell script

I want to pass reference file as an argument to shell script
ref.file contains
no=1
desc="query 0 "
src_txt="select count(1) from source"
trg_txt="select count(1) from target"
flag=c
no=2
desc="query 1 "
src_txt="select count(1) from source1"
trg_txt="select count(1) from target1"
flag=c
no=3
desc="query 2 "
src_txt="select count(1) from source2"
trg_txt="select count(1) from target2"
flag=c
and so on...
shell comamnd - sh generic.sh $(cat ref.txt )
inside shell script I am trying to read the contains of ref.txt
echo "ref-- " $1
It prints only first line i.e. no=1
if i use $2 and so on then it will print only few words not the whole text
How to read the entire file in a variable including new lines and iterate over other queries in ref file in a single go

How to manipulate a rows in a file using bash script?

I have a file which has records from a table.
It has this format. Each column is separated by tabs
UUID number name
ac500f63-630d-4048-90cf-74bc85c1101c 1 Kane
47493ed9-008b-4dd6-88dc-d91fa64225b3 3 NULL
What I want to do is : columns need to be comma separated. and UUID and name columns need to be wrapped in single quotes and not enclose in single quotes when name is NULL. Even a row needs to be comma separated.
the sample output for the above is
'ac500f63-630d-4048-90cf-74bc85c1101c', 1, 'Kane'
'47493ed9-008b-4dd6-88dc-d91fa64225b3', 3 NULL
I will need these values for an INSERT query. Is there a way to achieve this with sed or awk commands ?
cat l.txt
UUID number name
ac500f63-630d-4048-90cf-74bc85c1101c 1 Kane
47493ed9-008b-4dd6-88dc-d91fa64225b3 3 NULL
cat p.sh
#!/bin/bash
awk '
NF == 3 && NR >= 2 { m=$3=="NULL"?32:39;printf("%c%s%c,%s, %c%s%c\n",39,$1,39,$2,m,$3,m);}
' $1
./p.sh l.txt
'ac500f63-630d-4048-90cf-74bc85c1101c',1,'Kane'
'47493ed9-008b-4dd6-88dc-d91fa64225b3',3, NULL
I hope it will good for you.

Merge all the data within the (.....) in one line in shell script

I am new to shell script i need some help i have one SQL file like
SELECT DISTINCT F1.COL1,
F1.COL5 ADDRESS ,
COALESCE(COL1,
COL2,
COL3,
COL4),
F1.COL7
FROM TABLE1 F1
I need to print this in one line like
SELECT DISTINCT F1.COL1,
F1.COL5 ADDRESS ,
COALESCE(COL1,COL2,COL3,COL4),
F1.COL7
FROM TABLE1 F1
Thanks
With sed :
sed '/(/{:a;N;s/^ *//;s/\n *//;/)/!{ba}}' file
To edit file in place, add the -i option :
sed -i '/(/{:a;N;s/^ *//;s/\n *//;/)/!{ba}}' file
All lines starting with ( are joined until next line containing ).

How to extract the sybase sql query output in a shell script

I am trying to execute a SQL query on SYBASE database using shell script.
A simple query to count the number of rows in a table.
#!/bin/sh
[ -f /etc/bash.bashrc.local ] && . /etc/bash.bashrc.local
. /gi/base_environ
. /usr/gi/bin/environ
. /usr/gi/bin/path
ISQL="isql <username> guest"
count() {
VAL=$( ${ISQL} <<EOSQL
set nocount on
go
set chained off
go
select count(*) from table_name
go
EOSQL
)
echo "VAL : $VAL"
echo $VAL | while read line
do
echo "line : $line"
done
}
count
The above code gives the output as follows
VAL : Password:
-----------
35
line : Password: ----------- 35
Is there a way to get only the value '35'. What I am missing here? Thanks in advance.
The "select count(*)" prints a result set as output, i.e. a column header (here, that's blank), a line of dashes for each column, and the column value for every row. Here you have only 1 column and 1 row.
If you want to get rid of the dashes, you can do various things:
select the count(*) into a variable and just PRINT the variable. This will remove the dashes from the output
perform some additional filtering with things like grep and awk on the $VAL variable before using it
As for the 'Password:' line: you are not specifying a password in the 'isql' command, so 'isql' will prompt for it (since it works, it looks like there is no password). Best specify a password flag to avoid this prompt -- or filter out that part as mentioned above.
Incidentally, it looks like you may be using the 'isql' from the Unix/Linux ODBC installation, rather than the 'isql' utility that comes with Sybase. Best use the latter (check with 'which isql').

get the table names from file using UNIX Script

I am having a sample file as given below. This is an SQL Loader control file:
LOAD DATA
APPEND
INTO TABLE XXWIN_TMP_LOADER_TAB
( seq POSITION(1:10) INTEGER EXTERNAL
,h_record POSITION(11:20) CHAR
,h_file_name POSITION(21:55) CHAR
)
APPEND
INTO TABLE XXWIN_SQL_LOADER_TAB
( seq POSITION(1:10) INTEGER EXTERNAL
,h_record POSITION(11:20) CHAR
,h_file_name POSITION(21:55) CHAR
)
APPEND
INTO TABLE XXWIN_SQL_LOADER_TAB
( seq POSITION(1:10) INTEGER EXTERNAL
,h_record POSITION(11:20) CHAR
,h_file_name POSITION(21:55) CHAR
)
I would like to select any number of table names occurring in the file which are starting with 'XX_' and ending with '_TAB' and store it into an array using an UNIX script.
Please advice.
Thanks,
Arun
If the file syntax is not changing (the table names start with XX, not XX_):
tnames=`grep -o "TABLE XX[^ ].*_TAB" <file_name> | sed 's/TABLE //g'`
for tn in $tnames; do echo $tn; done
Change the <file_name> to the name of the file.
You don't say which shell, but since sh doesn't support arrays I'm assuming Bash.
tables=($(sed -n '/TABLE /s/TABLE \(XX[^ ]*TAB\) *$/\1/p' inputfile))
for table in ${tables[#]}
do
echo "$table"
done

Resources