Replace string with lines stored in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace string with lines stored in a file
# 1  
Old 08-22-2017
Replace string with lines stored in a file

OS version: RHEL 6.7
Shell : Bash


I have a file like below

Code:
$ cat pattern.txt
'T_PKT_HEADER'
'T_ORD_ITM_LOYX'
'T_ORDERITM_TRMS'
'T_ORDER_ITEM'
'T_ORDER_ITM_PRI'
'T_ORDER_ITEM_OM'
'T_ORDER_ITEM_XA'
'T_ORDER_ATT'
'T_ORDER_ACTNSET'
'T_ORDER_XM'
'T_ORDER_X'
'T_ORDER_TNTX'
'T_ORDER_POSTN'
'T_ORDER_DTL'
'T_ORDER'
'T_ORDER_BU'
.
.

For the below command I want to replace <tableName in Single Quotes> with each line from the above file.
Is there any way I could do this ? Expected output shown below

exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => <tableName in Single Quotes> ,degree => 3, cascade => true );

Expected output:

Code:
exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => 'T_PKT_HEADER' ,degree => 3, cascade => true );
exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => 'T_ORD_ITM_LOYX' ,degree => 3, cascade => true );
exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => 'T_ORDERITM_TRMS' ,degree => 3, cascade => true );
.
.
.

# 2  
Old 08-22-2017
Code:
while read tableName
do
   echo "exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => <tableName in Single Quotes> ,degree => 3, cascade => true );" | sed 's/<tableName in Single Quotes>/'"$tableName"'/'
done < pattern.txt

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-22-2017
Why sed?
Code:
while read tableName
do
   echo "exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => '$tableName', degree => 3, cascade => true );"
done < pattern.txt

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 08-22-2017
Why shell? Try
Code:
sed "s/^/exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => /; s/$/ ,degree => 3, cascade => true );/" pattern.txt

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-22-2017
Thank You Rudic.

what does the sed clause /; s/$/ do ?
# 6  
Old 08-22-2017
In fact, there's two substitute operations, the ; separates the two. One (s/^/.../)substitutes the begin-of-line, the other (s/$/.../) the end-of-line.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 08-22-2017
Wouldn't
Code:
sed "s/\(^.*\)$/exec dbms_stats.gather_table_stats(ownname => 'SIEBEL', tabname => \1 ,degree => 3, cascade => true );/" pattern.txt

have done it with one substitution?

Andrew
PS Where's the AWK solution? There has to be an AWK solution! Smilie
This User Gave Thanks to apmcd47 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string on specific lines which match a pattren

Hi Experts, I have a file which contains a pattern multiple times i.e. matchthispattren. If a line is matched with this pattern. I want a number in 1234567890 to 123456789 in that line. (Basically remove the last digit from that number. Please help. Thanks, Varun (1 Reply)
Discussion started by: varun22486
1 Replies

2. Shell Programming and Scripting

Replace a string with multiple lines

Hello Guys, I need to replace a string with multiple lines. For eg:- ABC,DEF,GHI,JKL,MNO,PQR,STU need to convert the above as below:- ABC,DEF, GHI1 GHI2 GHI3, JKL,MNO, PQR1 PQR2 PQR3, STU i have tried using code as:- (2 Replies)
Discussion started by: jassi10781
2 Replies

3. Shell Programming and Scripting

Removing md5sum lines stored in text file

Hello. I'm writing a script where every file you create will generate a md5sum and store it into a text file. Say I create 2 files, it'll look like this in the text file: d41d8cd98f00b204e9800998ecf8427e /helloworld/saystheman d41d8cd98f00b204e9800998ecf8427e /helloworld/test I... (3 Replies)
Discussion started by: batarangs_
3 Replies

4. Shell Programming and Scripting

Grep and sed (replace string in patterned lines)

Grep and Sed (replace string in patterned lines) Hi all, I want to grep for "PATTERN" and only if "PATTERN" is in a line, this line shall be used as replacement input e.g. for SED. I don't get it running in one line. NOT RUNNING - just first idea... I don't know how to redirect grep... (2 Replies)
Discussion started by: unknown7
2 Replies

5. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

6. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

7. Shell Programming and Scripting

Searching a string stored in other file

I need to design a script which can do the following: I have two files abc.txt and constant.hmtl abc.txt contains some 5 string that I need to see if they exist in contants.html. Constants.hmtl is a very large file around 800 lines. I want to search all the strings present in file... (8 Replies)
Discussion started by: hemasid
8 Replies

8. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

9. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

10. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies
Login or Register to Ask a Question