Replacement of static sub-strings in SQL Strings with constant date Strings - shell

I have got a piece of software which has in-built code processor and support the shell scripting.I have multiple SQL strings in a file which will be processed through the software one-by-one and values of certain sub-string constants will be overridden by that software without changing the original file which contains the original SQLs.you can pass the replacement parameters at runtime to that software.
Below is the example sed for one of the SQLs:
echo " select * from TABLE where DATE(TSP_DATE) between (CURRENT DATE -1 DAY) AND (CURRENT DATE)" | sed -e 's/CURRENT DATE - 1 DAY/'08/30/2018'/; s CURRENT DATE/'08/31/2018'/
I want to replace sub-strings
CURRENT DATE - 1 DAY
with '08/30/2018' and
CURRENT DATE
with '08/31/2018' using one command. Date Strings include the quotes.
When I ran the above script,I got below error :
sed: -e expression #1,char 29: unknown option to 's'
Regards,
Ankit

Not sure, if you want to implement a solution, that's more complicated than necessary.
Whatever DBMS you're using, I'm pretty sure it has a function for the current date. So you don't have to do this in your shell script.
For example, when you use MySQL, you can simply use this query:
select * from TABLE where DATE(TSP_DATE) between CURDATE() - INTERVAL 1 DAY AND CURDATE();
When you want to do it with sed anyway, you can do it like this:
echo "select * from TABLE where DATE(TSP_DATE) between (CURRENT DATE -1 DAY) AND (CURRENT DATE)" | sed "s#CURRENT DATE -1 DAY#'08/30/2018'#;s#CURRENT DATE#'08/31/2018'#"
As delimiter you can choose whatever you want. When you choose something other than / you don't have to escape it all the time, when you use it in your replacement string.

Related

How to get date and string separately in a given file name using shell script

Hi I am trying to get the date and string separately from the given file name but not getting exact idea how to do it.
This is the file name "95FILRDF01PUBLI20170823XEURC0V41000.XML"
I want to extract date "20170823" and string "XEUR" from this file name.
I was going through lots of posts in Stackexchange/Stackoverflow, but didn't understand the regular expression they are using.
https://unix.stackexchange.com/questions/182563/how-to-extract-a-part-of-file-name-in-unix-linux-shell-script
Extract part of filename
To extract date and name:
$ name="95FILRDF01PUBLI20170823XEURC0V41000.XML"
$ echo "$name" | sed -E 's/.*([[:digit:]]{8})([[:alpha:]]{4}).*/date=\1 name=\2/'
date=20170823 name=XEUR
The key part of the regex is ([[:digit:]]{8})([[:alpha:]]{4}). The first part of that, ([[:digit:]]{8}) matches 8 digits and saves them as group 1. The second part of that, ([[:alpha:]]{4}) matches four letters that follow the date and saves them as group 2.
The key part is surrounded by .* before and .* after which matches whatever is left over.
The replacement text is date=\1 name=\2 which formats the output.

Pass parameter from shell script to SQL script

I have ksh script which is calling a SQL script called report.sql.
$ORACLE_HOME/bin/sqlplus swb/swb4gdeprod#gdeprod #$reportHome/report.sql
Currently this SQL script is fetching data for sysdate from the database.
I want to pass a date as input to the SQL script so that it picks data corresponding to that date instead of sysdate.
How can I pass a date from ksh to SQL*Plus?
With substitution variables; start is equivalent to # here.
You haven't shown the SQL your report contains, but assuming at some point you have something like:
where some_col > trunc(sysdate)
you would change that to:
where some_col > to_date('&1', 'YYYY-MM-DD')
and then pass a date string in the same format on the command line:
$ORACLE_HOME/bin/sqlplus swb/swb4gdeprod#gdeprod #$reportHome/report.sql 2016-07-27
The value you pass in, e.g. 2016-07-27, is the first positional parameter so it can be referenced as a substitution variable using &1. As it's a string that has to be enclosed in single quotes when you reference it in SQL. And because it's a string you have to convert it to a date. The format of the string you pass in has to match the format you specify with the to_date() function.
Also be wary of passing in month names as you may hit language issues at some point, and ideally you'd use a format that is unambiguous. I've used the ISO format YYYY-MM-DD, and if you stick with that you could also use a date literal as the substitution will be done by SQL*Plus before it is interpreted by the SQL engine:
where some_col > date '&1'

How to change timestamp format

File has the following data and want to remove the 'T' and '-07:00' in the Timestamp field
407358186|2014-05-16T08:14:00-07:00|993827047
407358186|2014-05-15T08:58:00-07:00|993335621
407358186|2014-05-13T06:13:00-07:00|992181538
407358186|2014-05-11T19:58:00-07:00|991523532
Expected output result
407358186|2014-05-16 08:14:00|993827047
407358186|2014-05-15 08:58:00|993335621
407358186|2014-05-13 06:13:00|992181538
407358186|2014-05-11 19:58:00|991523532
You can use sed.
cat file | sed 's/T/\ /g' | sed 's/-07:00//g'
The first pipe simply replaces all T's with spaces, and the second pipe eliminates all instances of the string -07:00.
I'm assuming since you're removing the -07:00 that you are not interested in any timezone sensitive data. If you were, you would not remove it, and hence I am able to justify hardcoding '-07:00' into the answer.
Changing timestamps from other timezones would require code which is a bit more involved.

Retrieving File name for bash/shell programing

I need to access two files in my shell script. The only issue is , I am not sure what the file name is going to be as it is system generated.A part of the file name is always constant , but the rest of it is system generated , hence may vary. I am not sure how to access these files?
Sample File Names
Type 1
MyFile1.yyyy-mm-dd_xx:yy:zz.log
In this case , I know MyFile1 portion is a constant for all the files, the other portion varies based on date and time. I can use date +%Y-%m-%d to get till MyFile1.yyyy-mm-dd_ but I am not sure how to select the correct file. Please note each day will have just one file of the kind. In unix the below command gives me the correct file .
unix> ls MyFile1.yyyy-mm-dd*
Type 2
MyFile2.yyyymmddxxyyxx.RandomText.SomeNumber.txt
In this file , as you can see Myfile2 portion is common,I can user Date +%Y%m%d to get till (current date) MyFile2.yyyymmdd, again not very clear how to go on from there .In unix the below command gives me the correct file .Also I need to have previous date in the dd column for File 2.
unix> ls MyFile2.yyyymmdd*
basically looking for the following line in my shell script
#!/bin/ksh
timeA=$(date +%Y-%m-%d)
timeB=$(date +%Y%m)
sysD=$(date +%d)
sysD=$((sysD-1))
filename1=($Home/folder/MyFile1.$timeA*)
filename2=($Home/folder/MyFile2.$timeB$sysD*)
Just not sure how to get the RHS for these two files.
The result when running the above scripts is as below
Script.ksh[8]: syntax error at line 8 : `(' unexpected
Perhaps this
$ file=(MyFile1.yyyy-mm-dd*)
$ echo $file
MyFile1.yyyy-mm-dd_xx:yy:zz.log
It should be noted that you must declare variables in this manner
foo=123
NOT
foo = 123
Notice carefully, bad
filename1=$($HOME/folder/MyFile1.$timeA*)
good
filename1=($HOME/folder/MyFile1.$timeA*)

Cshell Script date issue

Is there a way to add date in the name of the file... we can add current date in this manner date '+%Y%m%d' but i want to add "filename_date_1-2-2011_thru_31-2-2011.txt" Is it possible to do that??????????
If you have a sufficiently advanced version of the date command and you know a Unix timestamp for the start and end dates, then you can use:
(MacOS X) date -r 1234567890 "+%d-%m-%Y" to obtain 13-02-2009.
(GNU) date -d 2/13/2009 "+%d-%m-%Y" to obtain 13-02-2009 again.
If you don't want the leading zeroes on the day of month, then you need to use '%e` instead of '%d' on Linux (but that puts a space in place of the zero). It is not clear that there's a format specifier for day-of-month without a leading zero on MacOS X; nor is it clear that there's a way to format month of year as a single-digit number for January to September on either platform.
You get the format into your C shell script using back-ticks around the date commands.
Consider reading Csh Programming Considered Harmful and heeding its advice.

Resources