SED command for completing SQL query file - bash

I've got a SQL query file to be filled with a variable (domainname) and two date values (start and end date).
The idea is to bash script the SED command to create SQL query files per month and domain.
Is it smart to search and replace the SQL query file with SED?
When I try so, I need to escape the characters from the date value.
The part of the SQL file looks as follows:
date < ''
AND
date >= ''
AND
to_domain = ''
The input of an example:
to_domain = testing.com date = 2017-04-01 date = 2017-04-02
Any ideas to do this smart?
I'm sorry but I don't have enough experience in SED.
I've Google'd my ass of with this one, but no luck so far.
Thanks in advance!

You can use environment variables in a template query file and generate the new file with envsubst to substitute env vars.
Given the following query-template.txt :
date < '$start_date'
AND
date >= '$end_date'
AND
to_domain = '$to_domain'
You can generate generate query.txt with envsubst and previously define your variables :
export start_date=2017-04-01
export end_date=2017-04-02
export to_domain=testing.com
envsubst < query-template.txt > query.txt
Or put all the variables export in a file vars.sh and source it before performing the substitution :
source vars.sh
envsubst < query-template.txt > query.txt

Related

bash: create a variable name containing another variable

I have to create multiple variable while reading the file in bash.
These variables need to have a dynamic name as per the content of file.
E.g:
File content:
abc: 20
1 apple a day
abc: 40
1 keeps the doctor away
now i have to create variables as:
day_20_id = 1
day_20_fruit = apple
away_40_id = 1
away_40_who = doctor
it would be like in all variable names, only the the value of $abc will be updated and the value of the variable will be as per the file content.
Can somebody help me out to figure out how to achieve this.
You can use the eval command to accomplish this as illustrated below
abc=20 # assuming you got this from the input file
val=1 # assuming you also got this from the input file
varName="day_${abc}_id"
command="${varName}=${val}"
eval $command
# now print to output file as you have stated in the comments
outputFile=output.txt # randomly-chosen name
command="echo $varName = $val > $outputFile"
eval $command

output a file with a variable name in shell

So I am trying to output a file with the name of like: lastlogin-"yyyymmdd" where it contains the current date.
I figured out the date should be : date +"%Y%m%d" and I tried to do a variable
now = date +"lastlogin-%Y%m%d.txt"
filename = ${now}
xxxxx > ${filename}
but nothing seems to work
Please help
Use command substitution:
lastlogin-"$(date '+%Y%m%d')".txt
To save in a variable:
filename="lastlogin-"$(date '+%Y%m%d')".txt"
and then do:
echo 'foobar' >"$filename"
You should use $() for command execution and storage of result:
now=$(date +"lastlogin-%Y%m%d.txt")

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').

Shell Script for Searching text with variable and replace it with content

im new to shell script, but i need something to search a file with any given variable, and if the file contains this variable replace it with the variables alias
the text would be some thing like:
74304050 = +4574304050#voip1.local
74304051 = +4574304051#voip1.local
20304050 = +4520304050#voip2.local
20304051 = +4520304051#voip2.local
so if i use call the shell script with 20304050 i get +4520304050#voip1.local
how can this be done, i need it for calling aliases and rewriting them in opensips config file?
It's a bit underspecified, but does this do what you want?
awk -V number=20304050 '$1 == $number { print $3 }' file

Bash script to convert a date and time column to unix timestamp in .csv

I am trying to create a script to convert two columns in a .csv file which are date and time into unix timestamps. So i need to get the date and time column from each row, convert it and insert it into an additional column at the end containing the timestamp.
Could anyone help me? So far i have discovered the unix command to convert any give time and date to unixstamp:
date -d "2011/11/25 10:00:00" "+%s"
1322215200
I have no experience with bash scripting could anyone get me started?
Examples of my columns and rows:
Columns: Date, Time,
Row 1: 25/10/2011, 10:54:36,
Row 2: 25/10/2011, 11:15:17,
Row 3: 26/10/2011, 01:04:39,
Thanks so much in advance!
You don't provide an exerpt from your csv-file, so I'm using this one:
[foo.csv]
2011/11/25;12:00:00
2010/11/25;13:00:00
2009/11/25;19:00:00
Here's one way to solve your problem:
$ cat foo.csv | while read line ; do echo $line\;$(date -d "${line//;/ }" "+%s") ; done
2011/11/25;12:00:00;1322218800
2010/11/25;13:00:00;1290686400
2009/11/25;19:00:00;1259172000
(EDIT: Removed an uneccessary variable.)
(EDIT2: Altered the date command so the script actually works.)
this should do the job:
awk 'BEGIN{FS=OFS=", "}{t=$1" "$2; "date -d \""t"\" +%s"|getline d; print $1,$2,d}' yourCSV.csv
note
you didn't give any example. and you mentioned csv, so I assume that the column separator in your file should be "comma".
test
kent$ echo "2011/11/25, 10:00:00"|awk 'BEGIN{FS=OFS=", "}{t=$1" "$2; "date -d \""t"\" +%s"|getline d; print $1,$2,d}'
2011/11/25, 10:00:00, 1322211600
Now two imporvements:
First: No need for cat foo.csv, just stream that via < foo.csv into the while loop.
Second: No need for echo & tr to create the date stringformat. Just use bash internal pattern and substitute and do it inplace
while read line ; do echo ${line}\;$(date -d "${line//;/ }" +'%s'); done < foo.csv

Resources