Sponsored Content
Top Forums Shell Programming and Scripting Sed/awk/perl substitution with multiple lines Post 302928198 by sudo on Tuesday 9th of December 2014 06:47:48 PM
Old 12-09-2014
Sed/awk/perl substitution with multiple lines

OSX
I have been grinding my teeth on a portion of code. I am building a bash script that edits a html email template. In the template, I have place holders for SED (or whatever program is appropriate) to use as anchors for find and replace, with user defined corresponding html code. The HTML code is pulled from a text file which contains all the different options of code. A simulation is:
(simplified)
Code:
The template with placeholders:

headerPlaceholder
productPlaceholder
infoPlaceholder
datePlaceholder

Code:
product=$(awk '/PRODSTART/,/PRODEND/{if (!/PRODSTART/&&!/PRODEND/) print}' snippets.txt)    # range of desired html code selected from file, setup as a variable
sed -i '' "s/productPlaceholder/$product\n/g" ~/Desktop/emailTemplate.eml    #replaces the placeholder with specified html code

If it was a single line, this works fine. But, the html snippet is usually multiple lines, filled with special characters such as:
Code:
<p><font color=3D"#929292">Product =
Code:&nbsp;</font><span class=3D"Apple-tab-span" style=3D"color: rgb(51, =
51, 51); white-space: pre; ">		</span><font =
color=3D"#333333">prodnum</font><br><font color=3D"#929292">Model =

Is there a better way to approach this? Thanks!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed substitution and multiple lines

I have a file names 'log.txt' that looks something like this: #This is a comment /sbin/iptables -A INPUT -p tcp -s ip.of.a machine --destination-port 21 -j ACCEPT #This is the comment to read# /sbin/iptables -A INPUT -p tcp -s ip.of.a.machine --destination-port 21 -j ACCEPT I would like... (1 Reply)
Discussion started by: manouche
1 Replies

2. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

3. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

4. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

5. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

6. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

7. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

8. Shell Programming and Scripting

Awk/sed : help on:Filtering multiple lines to one:

Experts Good day, I want to filter multiple lines of same error of same day , to only 1 error of each day, the first line from the log. Here is the file: May 26 11:29:19 cmihpx02 vmunix: NFS write failed for server cmiauxe1: error 5 (RPC: Timed out) May 26 11:29:19 cmihpx02 vmunix: NFS... (4 Replies)
Discussion started by: rveri
4 Replies

9. Shell Programming and Scripting

Process alternate lines in awk/sed/perl

hi.. i have a fasta file with the following format >sequence1 CCGGTTTTCGATTTGGTTTGACT >sequence2 AAAGTGCCGCCAGGTTTTGAGTGT >sequence3 AGTGCCGCAGAGTTTGTAGTGT Now, i want to read alternate line and add "GGGGGGGGGGG" to end of every sequence Desired output: >sequence1... (4 Replies)
Discussion started by: empyrean
4 Replies

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

NAME
Text::MicroTemplate - Micro template engine with Perl5 language SYNOPSIS
use Text::MicroTemplate qw(:all); # compile template, and render $renderer = build_mt('hello, <?= $_[0] ?>'); $html = $renderer->('John')->as_string; # or in one line $html = render_mt('hello, <?= $_[0] ?>', 'John')->as_string; # complex form $mt = Text::MicroTemplate->new( template => 'hello, <?= $query->param('user') ?>', ); $code = $mt->code; $renderer = eval << "..." or die $@; sub { my $query = shift; $code->(); } ... $html = $renderer->(CGI->new)->as_string; DESCRIPTION
Text::MicroTemplate is a standalone, fast, intelligent, extensible template engine with following features. standalone Text::MicroTemplate does not rely on other CPAN modules. fast Based on Mojo::Template, expressions in the template is perl code. intelligent Text::MicroTemplate automatically escapes variables when and only when necessary. extensible Text::MicroTemplate does not provide features like template cache or including other files by itself. However, it is easy to add you own (that suites the most to your application), by wrapping the result of the module (which is a perl expression). The module only provides basic building blocks for a template engine. Refer to Text::MicroTemplate::File for higher-level interface. TEMPLATE SYNTAX
The template language is Perl5 itself! # output the result of expression with automatic escape <?= $expr ?> (tag style) ?= $expr (per-line) # execute perl code (tag style) <? foo() ?> ? foo() # comment (tag style) <?# comment ?> ?# comment # loops <ul> ? for my $item (@list) { <li><?= $item ?></li> ? } </ul> EXPORTABLE FUNCTIONS
build_mt($template) Returns a subref that renders given template. Parameters are equivalent to Text::MicroTemplate->new. # build template renderer at startup time and use it multiple times my $renderer = build_mt('hello, <?= $_[0] ?>!'); sub run { ... my $hello = $renderer->($query->param('user')); ... } render_mt($template, @args) Utility function that combines build_mt and call to the generated template builder. # render $hello = render_mt('hello, <?= $_[0] ?>!', 'John'); # print as HTML print $hello->as_string; # use the result in another template (no double-escapes) $enc = render_mt('<h1><?= $_[0] ?></h1>', $hello); Intertally, the function is equivalent to: build_mt($template)->(@_); encoded_string($str) wraps given string to an object that will not be escaped by the template engine OO-STYLE INTERFACE Text::MicroTemplate provides OO-style interface to handle more complex cases. new($template) new(%args) new(\%args) Constructs template renderer. In the second or third form, parameters below are recognized. template template string (mandatory) escape_func escape function (defaults to Text::MicroTemplate::escape_html), no escape when set to undef package_name package under where the renderer is compiled (defaults to caller package) code() returns perl code that renders the template when evaluated filter(sub filter_func { ... })->(sub { template lines }) filters given template lines ? $_mt->filter(sub { s/Hello/Good bye/g })->(sub { Hello, John! ? }) DEBUG
The "MICRO_TEMPLATE_DEBUG" environment variable helps debugging. The value 1 extends debugging messages, 2 reports compiled Perl code with "warn()", 3 is like 2 but uses "die()". SEE ALSO
Text::MicroTemplate::File Text::MicroTemplate::Extended AUTHOR
Kazuho Oku <kazuhooku gmail.com> Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM> The module is based on Mojo::Template by Sebastian Riedel. LICENSE
This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2010-09-06 Text::MicroTemplate(3pm)
All times are GMT -4. The time now is 07:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy