Sponsored Content
Top Forums Shell Programming and Scripting To substitute multiple variable by their content in a file Post 302981600 by Don Cragun on Thursday 15th of September 2016 01:30:31 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Devel::REPL(3pm)					User Contributed Perl Documentation					  Devel::REPL(3pm)

NAME
Devel::REPL - a modern perl interactive shell SYNOPSIS
my $repl = Devel::REPL->new; $repl->load_plugin($_) for qw(History LexEnv); $repl->run Alternatively, use the 're.pl' script installed with the distribution system$ re.pl DESCRIPTION
This is an interactive shell for Perl, commonly known as a REPL - Read, Evaluate, Print, Loop. The shell provides for rapid development or testing of code without the need to create a temporary source code file. Through a plugin system, many features are available on demand. You can also tailor the environment through the use of profiles and run control files, for example to pre-load certain Perl modules when working on a particular project. USAGE
To start a shell, follow one of the examples in the "SYNOPSIS" above. Once running, the shell accepts and will attempt to execute any code given. If the code executes successfully you'll be shown the result, otherwise an error message will be returned. Here are a few examples: $_ print "Hello, world! " Hello, world! 1 $_ nosuchfunction Compile error: Bareword "nosuchfunction" not allowed while "strict subs" in use at (eval 130) line 5. $_ In the first example above you see the output of the command ("Hello, world!"), if any, and then the return value of the statement(1). Following that example, an error is returned when the execution of some code fails. Note that the lack of semicolon on the end is not a mistake - the code is run inside a Block structure (to protect the REPL in case the code blows up), which means a single statement doesn't require the semicolon. You can add one if you like, though. If you followed the first example in the "SYNOPSIS" above, you'll have the History and LexEnv plugins loaded (and there are many more available). Although the shell might support "up-arrow" history, the History plugin adds "bang" history to that so you can re-execute chosen commands (with e.g. "!53"). The LexEnv plugin ensures that lexical variables declared with the "my" keyword will automatically persist between statements executed in the REPL shell. When you "use" any Perl module, the "import()" will work as expected - the exported functions from that module are available for immediate use: $_ carp "I'm dieeeing! " String found where operator expected at (eval 129) line 5, near "carp "I'm dieeeing! "" (Do you need to predeclare carp?) Compile error: syntax error at (eval 129) line 5, near "carp "I'm dieeeing! "" BEGIN not safe after errors--compilation aborted at (eval 129) line 5. $_ use Carp $_ carp "I'm dieeeing! " I'm dieeeing! at /usr/share/perl5/Lexical/Persistence.pm line 327 1 $_ To quit from the shell, hit "Ctrl+D" or "Ctrl+C". MSWin32 NOTE: control keys won't work if TERM=dumb because readline functionality will be disabled. Run Control Files For particular projects you might well end up running the same commands each time the REPL shell starts up - loading Perl modules, setting configuration, and so on. A run control file lets you have this done automatically, and you can have multiple files for different projects. By default the "re.pl" program looks for "$HOME/.re.pl/repl.rc", and runs whatever code is in there as if you had entered it at the REPL shell yourself. To set a new run control file that's also in that directory, pass it as a filename like so: system$ re.pl --rcfile myproject.pc If the filename happens to contain a forwardslash, then it's used absolutely, or realive to the current working directory: system$ re.pl --rcfile /path/to/my/project/repl.rc Within the run control file you might want to load plugins. This is covered in "The REPL shell object" section, below. Profiles To allow for the sharing of run control files, you can fashion them into a Perl module for distribution (perhaps via the CPAN). For more information on this feature, please see the Devel::REPL::Profile manual page. A default profile ships with "Devel::REPL"; it loads the following plugins: o Devel::REPL::Plugin::History o Devel::REPL::Plugin::LexEnv o Devel::REPL::Plugin::DDS o Devel::REPL::Plugin::Packages o Devel::REPL::Plugin::Commands o Devel::REPL::Plugin::MultiLine::PPI o Devel::REPL::Plugin::Colors o Devel::REPL::Plugin::Completion o Devel::REPL::Plugin::CompletionDriver::INC o Devel::REPL::Plugin::CompletionDriver::LexEnv o Devel::REPL::Plugin::CompletionDriver::Keywords o Devel::REPL::Plugin::CompletionDriver::Methods o Devel::REPL::Plugin::ReadlineHistory Plugins Plugins are a way to add funcionality to the REPL shell, and take advantage of "Devel::REPL" being based on the Moose object system for Perl 5. This means it's simple to 'hook into' many steps of the R-E-P-L process. Plugins can change the way commands are interpreted, or the way their results are output, or even add commands to the shell environment. A number of plugins ship with "Devel::REPL", and more are available on the CPAN. Some of the shipped plugins are loaded in the default profile, mentioned above. These plugins can be loaded in your "$HOME/.re.pl/repl.rc" like: load_plugin qw( CompletionDriver::Global DumpHistory ); Writing your own plugins is not difficult, and is discussed in the Devel::REPL::Plugin manual page, along with links to the manual pages of all the plugins shipped with "Devel::REPL". The REPL shell object From time to time you'll want to interact with or manipulate the "Devel::REPL" shell object itself; that is, the instance of the shell you're currently running. The object is always available through the $_REPL variable. One common requirement is to load an additional plugin, after your profile and run control files have already been executed: $_ $_REPL->load_plugin('Timing'); 1 $_ print "Hello again, world! " Hello again, world! Took 0.00148296356201172 seconds. 1 $_ REQUIREMENTS
In addition to the contents of the standard Perl distribution, you will need the following: o Moose >= 0.74 o MooseX::Object::Pluggable >= 0.0009 o MooseX::Getopt >= 0.18 o MooseX::AttributeHelpers >= 0.16 o namespace::clean o File::HomeDir o Task::Weaken o B::Concise o Term::ANSIColor o Devel::Peek Optionally, some plugins if installed will require the following modules: o PPI o Data::Dump::Streamer o Data::Dumper::Concise o File::Next o Sys::SigAction o B::Keywords o Lexical::Persistence o App::Nopaste o Module::Refresh AUTHOR
Matt S Trout - mst (at) shadowcatsystems.co.uk (<http://www.shadowcatsystems.co.uk/>) CONTRIBUTORS
Stevan Little - stevan (at) iinteractive.com Alexis Sukrieh - sukria+perl (at) sukria.net epitaph mgrimes - mgrimes (at) cpan dot org Shawn M Moore - sartak (at) gmail.com Oliver Gorwits - oliver on irc.perl.org Andrew Moore - "<amoore@cpan.org>" Norbert Buchmuller "<norbi@nix.hu>" Dave Houston "<dhouston@cpan.org>" Chris Marshall LICENSE
This library is free software under the same terms as perl itself perl v5.14.2 2012-05-19 Devel::REPL(3pm)
All times are GMT -4. The time now is 08:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy