Multiple Replacement in a Text File in one operation (sed/awk) ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple Replacement in a Text File in one operation (sed/awk) ?
# 1  
Old 08-02-2016
Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all,

Saying we have two files:

1. A "Reference File" whose content is "Variable Name": "Variable Value"

2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a program tailored for my need !

Code:
[x004191a@xsnl11p317a TEST_DAE]$ cat Reference
@TargetTable:TT
@CurrentDate:2016-08-02
@TargetDb:CDH_DEV


Code:
[x004191a@xsnl11p317a TEST_DAE]$ cat Model_File
/*
===============================================
   NOM Script  : @TargetTable.tpt
   TYPE        : Script TPT
   DESCRIPTION : DOMAINE METADATA - CHARGEMENT -
   AUTEUR      : DAE
   DATE        : @CurrentDate
===============================================   HISTORIQUE

   DATE        AUTEUR  CREATION/MODIFICATION
   08/01/2016  DAE     CREATION
===========================================================================
*/

USING CHARACTER SET UTF-8
DEFINE JOB TPT_@TargetTable
DESCRIPTION 'CHARGEMENT @TargetTable'

The aim is to produce that third file without using multiple sed substitution operations:

Code:
[x004191a@xsnl11p317a TEST_DAE]$ cat Program_File
/*
===========================================================================
   NOM Script  : TT.tpt
   TYPE        : Script TPT
   DESCRIPTION : DOMAINE METADATA - CHARGEMENT -
   AUTEUR      : DAE
   DATE        : 2016-08-02
===========================================================================
*/

USING CHARACTER SET UTF-8
DEFINE JOB TPT_TT
DESCRIPTION 'CHARGEMENT TT'

[x004191a@xsnl11p317a TEST_DAE]$

Any kind of help would be greatly appreciated,

Didier.

Last edited by RudiC; 08-02-2016 at 07:52 AM.. Reason: code tags
# 2  
Old 08-02-2016
Try something like:
Code:
awk -F: 'NR==FNR{A[$1]=$2; next} {for(i in A) gsub(i, A[i])}1' Reference Modelfile

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-02-2016
Excellent Scrutinizer !!! Smilie

Thanks a lot,

Didier.

Next step: understand how it is running, not obvious to me ! Smilie
# 4  
Old 08-02-2016
Code:
awk -F: '                        # Set the input field separator to ":"
  NR==FNR{                       # When the first file is being read (only then are FNR and NR equal) 
    A[$1]=$2                     # create an (associative) element in array A with the first field as the index and the 2nd field as value (this contains the replacements)
    next                         # start reading the next record (line)
  } 
  {                              # while reading the second file, for every line (record)
    for(i in A) gsub(i, A[i])    # for every replacement that was read from the first file, replace every occurrence on the line
  }
  1                              # print the record (line)
' Reference Modelfile            # read file 1 and then file 2

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 08-02-2016
Thanks a lot for your detailed explanation, Scrutinizer: I would never be able to find that by myself !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

sed text replacement

Hello, I'm using Bash and Sed to replace text within a text file (1.txt) twice in one script. Using a for loop I'm initially replacing any 'apple' words with the variable 'word1' ("leg). I'm then using another for loop to replace any 'apple' words with the variable 'word2' ("arm"). This task is... (2 Replies)
Discussion started by: Flip-Flop
2 Replies

3. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

4. Shell Programming and Scripting

Text replacement with awk or sed?

Hi guys, I worked for almost a half-day for the replacement of some text automatically with script. But no success. The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong. For example, I... (2 Replies)
Discussion started by: liuzhencc
2 Replies

5. Shell Programming and Scripting

Complex string operation (awk, sed, other?)

I have a file that contains RewriteRules for 200 countries (2 examples for 1 country below): RewriteRule ^/at(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=de_AT #& RewriteRule ^/at_english(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=en_AT I have... (5 Replies)
Discussion started by: usshadowop
5 Replies

6. Shell Programming and Scripting

Block of text replacement using sed

Hi, I have a requirement in which i need to replace text as below - <stringProp name="Recipe">&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls/1001/ServiceSwitches&quot;&gt; &lt;Param Name=&quot;AttributeName&quot; Value=&quot;HeaderManipRspIngressRuleSet&quot; Type=&quot;String&quot; /&gt; &lt;Param Name=&quot;Value&quot;... (0 Replies)
Discussion started by: abhitanshu
0 Replies

7. Shell Programming and Scripting

How to replace multiple text in a file using sed

can anyone please help me in the below scenario: File1: Hello1 Hello1 i want to use sed to replace multiple occurances of Hello1 in file 1 to welcome. Thanks a ton for the help (9 Replies)
Discussion started by: amithkhandakar
9 Replies

8. Shell Programming and Scripting

sed xml file multiple line replacement

I have a file called config.xml, it's a simple xml file, and I need use sed/awk to erase some lines. <machine xsi:type="unix-machineType"> <name>server1</name> <node-manager> <name>server1</name> <listen-address>server1</listen-address> </node-manager> ... (3 Replies)
Discussion started by: cbo0485
3 Replies

9. UNIX for Dummies Questions & Answers

Sed text replacement issue.

Hi, Im trying to find and replace text within a unix file using sed. The command that i have been using is sed '/,null,/ s//, ,/g' result.txt>result.tmp for replacing ",null," with ", ,". But this only replaces the first occurrance of ,null, in every line. I want to do it globally. It... (7 Replies)
Discussion started by: sohaibs
7 Replies

10. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies
Login or Register to Ask a Question