To substitute multiple variable by their content in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To substitute multiple variable by their content in a file
# 8  
Old 09-14-2016
Quote:
Originally Posted by dae
Rudi & Ravinder, thanks a lot ! ... just let me check and understand ... the last point is much more difficult to me ! Smilie
---------- Post updated at 04:55 PM ---------- Previous update was at 04:41 PM ----------
Rudi, your code is perfect ... I do not understand anything but it is performing exactly what I would like !
Thanks a lot !
Hello dae,

I thought you have to read variables from file, so I have given solution like that. Following explanation for sir Rudi's solution may help you in same.
Code:
awk -v RP="$var" '                          #### defining the variable named var into awk variable named RP by defining it with -v RP, if we have to get shell variable's values into awk we have to do this procedure, we can't directly get shell variable's value into awk variables.
BEGIN                                       #### starting BEGIN block/section here, which is one of awk's block where statements will be executed first, before reading files or doing other operations.
{for (n=split(RP, T, "[\n:]"); n>0; n-=2)   #### starting a for loop here, whose starting value is variable n(whose value we are getting by the number of elements in array T which is defined by split, split is an awk's built-in utility where we could take any variable's or line's value into array by defining delimiter. eg--> split(line/vriable, array_name,delimiter)). Like here we are splittig variable named rp and storing it's values to array named T and delimiter is either new_line or colon(:). Then defining the condition as per for loop's syntax eg--> for(variable_initilization, condition,decrement/increment of variable).
REPL[T[n-1]] = T[n]                         #### Here we are creating an array named REPL whose index is value of array T whose index is n-1. REPL  array has value of array T whose index is n here. So basically in REPL array we are storing the indexes of array REPL which we have to search in your Input_file and values of array REPL are the values which we have to get into Input_file.
}                                           #### completing the BEGIN section here.
        {for (r in REPL) sub (r, REPL[r])   #### Now starting a for loop which will traverse inside array REPL and then using sub which is an awk's internal utility to perform substitution whose syntax is eg--> sub(pattern/value which needs to be subsituted,pattern/value new one,line/variable). So here we are mentioning r,REPL[r] which means index pr array REPL in Input_file if it's present in Input_file should be subsituted with value of array REPL.
        }
1                                           #### awk works on method of pattern(condition)/action, so if any condition is TRUE it performes the action mentioned after it. So here by mentioning 1 we are making the condition TRUE and didn't mention any action here so it performs by default action here which is printing the current line.
' file                                      #### mentioning Input_file here.

Sorry not able to put comments into new lines, so they are in a single line only.

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-14-2016 at 12:40 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 09-14-2016
Hi R. Singh,
Note that you can easily put comments on multiple lines... You just have to put a # somewhere on each line before the continued comment text as in:
Code:
awk -v RP="$var" '                          #### defining the variable named var into
                                            #### awk variable named RP by defining
                                            #### it with -v RP, if we have to get shell
                                            #### variable's values into awk we have
                                            #### to do this procedure, we cannot directly
                                            #### get shell variable's value into awk variables.

Note that I changed can't in your original comment to cannot... Even though # starts a comment in awk, the single quote is still recognized by the shell as the end of the single-quoted awk program text argument.

Note also that you cannot separate a condition and its action such as in:
Code:
BEGIN                           # comment
{ begin section actions         # comment
}

because awk will interpret it as a BEGIN section with no action and interpret the following action section as an action with no condition (which will be performed on every record read from any input files processed). To get what you intended, you would need:
Code:
BEGIN {                         # comment
  begin section actions         # comment
}

These 2 Users Gave Thanks to Don Cragun For This Post:
# 10  
Old 09-15-2016
Dear all,

I allow myself to come back to that thread because one point is quite difficult to understand to me ...

Just a quick reminder of the context: the aim was to instantiate a "pattern file" by replacing "variable name" in that file by their respective value (a shell variable "var" contains the link between the variable and its value):

Code:
[x004191a@xsnl11p317a tmp]$ echo "$var"
@VAR1:Value_Var1
@VAR2:Value_Var2
@VAR3:Value_Var3

The syntax you suggested was perfect to me:

Code:
        awk -v RP="$var" '
        BEGIN   {for (n=split(RP, T, "[\n:]"); n>0; n-=2)
                   REPL[T[n-1]] = T[n]
                }
                {for (r in REPL) sub (r, REPL[r])
                }
        1' file

I would like to emphasize the BEGIN clause: the aim of that step is, I think, to fill the REPL array that way:

REPL[@VAR1]=Value_Var_1
REPL[@VAR2]=Value_Var_2
REPL[@VAR3]=Value_Var_3

I do not understand why it is necessary to use a "for loop" to perform that action: if I am not wrong, "n" value is a constant for each line equals to 2 (because there are two different fields for each line) ...

I can verify that issuing the following command:

Code:
[x004191a@xsnl11p317a tmp]$ cat DAE_20160915.txt
@VAR1:Value_Var1
@VAR2:Value_Var2
@VAR3:Value_Var3
[x004191a@xsnl11p317a tmp]$ awk ' {n=split($0, array, "[\n:]")
> REPL[array[n-1]]= array[n]
> print n, array[n], array[n-1]
> }
> ' DAE_20160915.txt
2 Value_Var1 @VAR1
2 Value_Var2 @VAR2
2 Value_Var3 @VAR3

Applying that syntax to the code you suggested yesterday (ie omitting the "for loop" in the BEGIN clause), I checked the program was not good, only replacing the content of the last variable in the list (ie @VAR3) in the pattern file.

so, I am afraid I am still lost with that syntax ! Smilie
# 11  
Old 09-15-2016
I think something got lost in translation...

The for loop was needed in the BEGIN section of the script because the variable var contained multiple lines of replacements. The for loop copied six elements from the array T[] split out from the three lines in the variable RP into 3 elements in the array REPL[].

If you are saying that your replacement patterns are stored in a file instead of being stored in a variable, and you want to convert RudiC's suggested code:
Code:
awk -v RP="$var" '
BEGIN   {for (n=split(RP, T, "[\n:]"); n>0; n-=2) REPL[T[n-1]] = T[n]
        }
        {for (r in REPL) sub (r, REPL[r])
        }
1
' file

to work with that file (named DAE_20160915.txt in the following suggestion) as input instead of having the replacements specified by the shell variable var, you could do that with something like:
Code:
awk -F':' ' # Set input field separator to colon.
FNR == NR { # Gather replacement data from the 1st input file...
            REPL[$1] = $2
            next
          }
          { # Loop through the replacement array and make the desired
            # substitutions in subsequent input files...
            for(r in REPL) sub(r, REPL[r])
          }
1           # Copy (possibly modified) subsequent input file data to
            # standard output.
' DAE_20160915.txt file

# 12  
Old 09-16-2016
Thanks for all your contributions,

I spent a while this morning to understand the way it is running and realize my previous question was stupid !

Thanks again for your help and the time you spent on my question !

Regards,

Didier.
# 13  
Old 09-16-2016
There is no such thing as a stupid question. If asking the question and reading the responses that were offered helped you figure out how to do what you wanted to do, it was a great question. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to substitute a word in multiple file?

Team, I want to change below parameter in all the files in a directory, Check for HOSTNAME=`hostname` Change to HOSTNAME=localhost And I tried below but, its not working ☹ find /tmp -type f -exec sed 's/"HOSTNAME\=\`hostname\`"/"HOSTNAME\=localhost/g'" Help me if I am missing... (6 Replies)
Discussion started by: natraj005
6 Replies

2. Shell Programming and Scripting

Replacement of variable by their content in a file

Dear all, I have a "SQL request" in a file: that request include different "host variable" and I would like to substitute the different "host variable" by their respective content before executing the request. For example: $ echo $SHELL /bin/bash $ cat dae2.txt DELETE FROM ... (11 Replies)
Discussion started by: dae
11 Replies

3. Shell Programming and Scripting

Substitute one line of multiple files according to another file

I need to make ~96 configure files from a template config file which has hundreds of rows that looks like: template.config: #average insert size avg_ins=1000 ...... other information omitted Those config files are named in sequence from S01.config, S02.config, ... etc with different... (11 Replies)
Discussion started by: yifangt
11 Replies

4. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

5. Shell Programming and Scripting

How to replace multiple file content?

Hi Gurus, I need replace multiple files content. the file name pattern likes currentfile_code_* the content pattern in the file like text=value I need replace the content as text=abcde Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

6. Shell Programming and Scripting

Variable resolution in File content

I have a file File1 containing lines like below apple ${FRUIT}-Color orange ${FRUIT}-Color banana ${FRUIT}-Color Now, in my shell I'm reading the file like below while read FRUIT DESC; do echo $FRUIT $DESC; done < File1 which outputs - apple ${FRUIT}-Color orange ${FRUIT}-Color... (3 Replies)
Discussion started by: nexional
3 Replies

7. Shell Programming and Scripting

How to put content of file into a variable?

For example, I have a simple text file note: this a note a simple note a very very simple notewhen I use this command, temp=$(cat "note.txt")then I echo temp, the result is in one line. echo $temp note: this a note a simple note a very very simple noteMy variable doesn't have newline. How... (7 Replies)
Discussion started by: 14th
7 Replies

8. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

9. UNIX for Dummies Questions & Answers

How to assign the content of a file to a variable?

Hi all, I have a problem here. I have a file and let we take the content of the file is just '32' (only a numeric value in that file). Now I need to assign this numeric value ( value in that file) to a variable. Is that possible? If so, can you plz advice me on this? Thanks in... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. Shell Programming and Scripting

redirecting variable content to a file!

Hello! I'm having problems trying to extract the contents of a variable and placing it into a text file. Grateful for any help. Been trying something along the lines of: $variable > file.txt or `cat < $variable` > file.txt As you can see I'm a newbie to this :D (2 Replies)
Discussion started by: lloowen
2 Replies
Login or Register to Ask a Question