Visit Our UNIX and Linux User Community


Match pattern and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match pattern and replace
# 1  
Old 09-01-2009
Match pattern from a list and replace

Hi All,

I am new to unix shell scripting, I need your help guys in coming up with some thing for the following scenario:

file1

ABC_BASE ${base}
ABC_ACC ${acc}
ABC_TEST ${test}
01-01-2006 ${from_dt}
01-15-2006 ${to_dt}

file 2

I have an file2.sql file which contains:

####This resides in .sql file ----------------------------------------------

Code:
INSERT INTO /*<base>*/ABC_BASE.tablename

SELECT
col1, col2

FROM
 /*<acc>*/ABC_ACC.tablename2

WHERE
          so_dt >= /*<from_date>*/'01-01-2006'
      AND
          so_dt < /*<to_date>*/'01-15-2006'

file3.sql

####This is the desired output--------------------------------------

Code:
INSERT INTO /*<baseDB>*/${base}.${xyz}${def}_tablename

SELECT
 col1, col2

FROM
 ${acc}.ABC_ACC.tablename2

WHERE
          so_dt >= /*<from_date>*/'${from_dt}'
      AND
          so_dt < /*<to_date>*/'${to_dt}'

The tags are commented, by using tags we have to match the pattern and replace as above.

This is the opposite of the regualr stuff

Thanks for you time guys... waiting for ur answers

---------- Post updated 09-01-09 at 11:16 AM ---------- Previous update was 08-31-09 at 09:41 PM ----------

Any ideas please

Last edited by sol_nov; 09-03-2009 at 01:04 AM.. Reason: added code tags
# 2  
Old 09-01-2009
Are you looking for a script that will *create* the .sql file new each time that is run with the 7 variables that you are assigning?
Where are you getting the 7 variables?
Is it part of a larger script?
# 3  
Old 09-01-2009
Yep I am looking for a script that will generate the new .SQL file when we give the original SQL file as input. And the variables will be in a list file
# 4  
Old 09-01-2009
The list file - can you provide an example?
It makes a difference it it's one field per line, or if the fields are on a single line separated by commas or spaces, etc. Also, if there are multiple records or sets, is there space between the records? A little more information is required...
# 5  
Old 09-03-2009
Say we have a list file with the following

file1

ABC_BASE ${base}
ABC_ACC ${acc}
ABC_TEST ${test}
01-01-2006 ${from_dt}
01-15-2006 ${to_dt}

file 2

contains the sql

My idea is, it has to match with the tags and replace apporopriately, I mean like
search for /*<base>*/ and replace the ABC_BASE with ${base} from the list file1

---------- Post updated 09-02-09 at 01:28 PM ---------- Previous update was 09-01-09 at 05:05 PM ----------

It has to find the pattern from the list file and replace accordingly

---------- Post updated at 11:05 PM ---------- Previous update was at 01:28 PM ----------

I have tried using sed, but I am not getting how to get it from the file1

Previous Thread | Next Thread
Test Your Knowledge in Computers #240
Difficulty: Easy
In 1973, a transatlantic satellite link connected the Norwegian Seismic Array (NORSAR) to the ARPANET, making Norway the first country outside the US to be connected to the network.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If pattern match, replace it with #

This command is not working for me. awk '{if ($1 == server) {$1 = #server} }' /etc/ntp.conf # grep server /etc/ntp.conf # Use public servers from the pool.ntp.org project. server 0.rhel.pool.ntp.org iburst server 1.rhel.pool.ntp.org iburst server 2.rhel.pool.ntp.org iburst server... (5 Replies)
Discussion started by: kenshinhimura
5 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

How to replace with pattern match using Perl

I have to replace a line, if it has a pattern for example Suppose file.out contains: <tr><td class="cB">Hello</td><td class="cB">1245</td><td class="cB">958</td><td class="cRB">1.34</td><td class="cRB">1.36</td></tr> <tr><td class="cB">world</td><td class="cB">3256</td><td... (8 Replies)
Discussion started by: sol_nov
8 Replies

4. Shell Programming and Scripting

Help with Pattern match and replace

I have a file containing a multiple lines of the format sddfdsf_gaf/ywrtrtwrt_gaf ghfghfgh_ert/xcvxcvcv_ert werwerwwerw_adf/jkhjkhjkjhkjhk_adf I am interested in only the first 3 letters following the "_" character and make those 3 letters uppercase after extraction. So would like to convert... (5 Replies)
Discussion started by: inditopgun
5 Replies

5. Shell Programming and Scripting

Match a Pattern & Replace The value Using AWK

I have a csv file in which i have to search a particular string and replace the data in any column with something else. How do i do it using awk. file ------ 2001,John,USA,MN,20101001,29091.50,M,Active,Y 2002,Mike,USA,NY,20090130,342.00,M,Pending,N... (3 Replies)
Discussion started by: Sheel
3 Replies

6. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

7. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

8. Shell Programming and Scripting

replace pattern after the first pattern match

I need this. aaa OOOOO bbb ccc OOOOO ddd fff ggg OOOOO iii OOOOO I need all OOOOO replaced with PPPPP, but only change after the pattern ggg. So the first two OOOOO should not be changed. OUTPUT should be :- aaa (2 Replies)
Discussion started by: anilcliff
2 Replies

9. Shell Programming and Scripting

Match pattern and replace with string

hi guys, insert into /*<new>*/abc_db.tbl_name this is should be replaced to insert into /*<new>*/${new}.tbl_name it should use '.' as delimiter and replace is there any way to do it using sed (6 Replies)
Discussion started by: sol_nov
6 Replies

10. Shell Programming and Scripting

Replace 1 word after pattern match

Hi, Here is my pattern CREATE USER LZ IDENTIFIED BY VALUES 'A0144280ESD70' DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP1 PROFILE DEVELOPER_D_1 ACCOUNT UNLOCK / The Sed command must look for the Line that contains TEMPORARY TABLESPACE and replace the immediate word... (4 Replies)
Discussion started by: rajan_san
4 Replies

Featured Tech Videos