Performing a calculation on string to be replaced


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Performing a calculation on string to be replaced
# 1  
Old 12-08-2010
Performing a calculation on string to be replaced

Hi,

I have a file with occurances of the string "TO_DATE(<number here>,'J')" at random places. I need minus 2400000 from the number and replace the string with "convert(date, dateadd(dd, <new number here>,'16 Nov 1858')".

I'm finding this difficult as the string isn't necessarily in the same places all the time and I'm not sure how to go about taking the number out and performing a calculation on it

Some examples of the original file are:
Code:
INSERT INTO EMPLOYEE VALUES
    (7505,'DOYLE','JEAN','K',671,7839,TO_DATE(2446160,'J'),2850,NULL,13)
go
INSERT INTO PRICE VALUES
   (100871,4.8,3.2,TO_DATE(,'J'),TO_DATE(2447862,'J'))
go

So in the first example I need to take the 2446160 out and minus 2400000 from it, then replace the whole "TO_DATE(...)" with "convert(date, dateadd(dd, <new number here>,'16 Nov 1858')"

I've been looking at using sed and maybe even grep and cut but I'm stumped.

Last edited by Franklin52; 12-08-2010 at 09:43 AM.. Reason: Please use code tags, thank you
# 2  
Old 12-08-2010
Code:
 
sed "s/TO_DATE(\([0-9]*\),'J')/convert(date, dateadd(dd,FSXXXX\1FSXXXX,'16 Nov 1858')/g" inputFile | awk 'FS="FSXXXX" {$2=$2-24400000;print;}'

# 3  
Old 12-08-2010
Thanks for the reply.
I'm not sure if awk works on this system, I get the errors:
awk: syntax error near line 1
awk: bailing out near line 1

even awk {print $0} doesn't seem to work
# 4  
Old 12-08-2010
Line borrowed from one of "scottn"'s posts.

Quote:
On Solaris use nawk, or /usr/xpg4/bin/awk.
# 5  
Old 12-08-2010
ok.. I see that error on solaris. Pls try this..

Code:
sed "s/TO_DATE(\([0-9]*\),'J')/convert(date, dateadd(dd,FSXXXX\1FSXXXX,'16 Nov 1858')/g" inputFile | nawk 'FS="FSXXXX" {if($2+0 > 0)$2=$2-24400000;print;}'

Use nawk instead of awk.

Last edited by anurag.singh; 12-08-2010 at 11:02 AM..
# 6  
Old 12-08-2010
Thanks, that works for the most part although -2400000 seems to be replaced in strange places and the calculation hasn't been done eg:

Code:
INSERT INTO PRICE VALUES -2400000
   (100890,54,40.5,convert(date, dateadd(dd, 47314 ,'16 Nov 1858'),convert(date, dateadd(dd, 2447527 ,'16 Nov 1858'))
go -2400000
 -2400000

# 7  
Old 12-08-2010
Corrected in earlier post [add if condition]. That should give right result now.
Not much familierity with nawk yet. Others may tell if anything better is there.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 Replies

2. Shell Programming and Scripting

How to deal with replaced files?

My task is to copy all files from many directories in one. The big problem i encounter is that some files in different directory have the same names. Is they are way to copy the files that have same names in a sub directory ( need to preserve the name of the files unchanged ) I have list with... (6 Replies)
Discussion started by: gogok_bg
6 Replies

3. Shell Programming and Scripting

variable value is not getting replaced

Hi, I am trying to generate list of employees from emp table who joined yesterday. emp.sh YEST=$(date --date='1 day ago' +%Y-%m-%d) cat emp.sql | mysql -u <user> -p<pass> -h <host> -P <port> -D <dbname> > emp.csv emp.sql select * from employee where join_date = '$YEST'; I expected... (3 Replies)
Discussion started by: ravi.videla
3 Replies

4. Shell Programming and Scripting

Query for replacing a string and keeping the non-replaced content

Hi experts, As i am a novice unix player...so need help for the below query...banged my head from quite a while...:confused: i have a set of html files, in which i need to search for string "Page"(case sensitive) and then replace the same with some numeric code ,say, "XXX1234". Here in... (2 Replies)
Discussion started by: rahulfhp
2 Replies

5. Shell Programming and Scripting

awk next time replaced by none

Hi Everyone, 1.txt a b c d e f d g gg output: abc de (3 Replies)
Discussion started by: jimmy_y
3 Replies

6. Shell Programming and Scripting

comma replaced with pipe

Source data: "123","aaa bbb CCC","12000" "134","HHH,bbc","13000" i have a delimited file. i want to replace with the pipe.The sed command is not working for replacing a delimeter. Command : sed s/\,/\|/g filename Output : When i run the command it is replacing the columns value... (7 Replies)
Discussion started by: number10
7 Replies

7. Shell Programming and Scripting

Performing pattern match for a string that might be intermingle with other strings

I have a log file that display the serial output coming from different places. Sometime the string in search gets clobbered with the other strings and consequently change form. For example: serial ouput: -------------- hello world! done with network configuring asic registers comJan 1... (2 Replies)
Discussion started by: timmylita
2 Replies

8. Programming

Can Mutex be replaced with anything?

Hi All, To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment. -Thanks (6 Replies)
Discussion started by: rvan
6 Replies

9. AIX

hdisk0 becomes hdisk2 after replaced

hello, I must've screwed something here.. I just had hdisk0 replaced by IBM.. now it shows up as hdisk2 instead. Before doing that, I've had it split from hdisk1, and reduced from rootvg. Just did a rmdev -dl hdisk2.. ran cfgmgr, but still shows up as hdisk2 instead of hdisk0.. help! (2 Replies)
Discussion started by: kiem
2 Replies

10. Shell Programming and Scripting

need help converting string to number for calculation

I've searched the forum and google, but can't see an answer to this simple problem. Here's my small test script: #!/bin/csh echo "enter a number:" read num echo "you entered $num" set num = `expr $num + 1` echo new value is $num can someone show me how to do this calculation? note that... (4 Replies)
Discussion started by: tpatput
4 Replies
Login or Register to Ask a Question