Sponsored Content
Top Forums UNIX for Dummies Questions & Answers sed - Add a variable line to the end of a block beginning with a regex Post 302795257 by andyatit on Wednesday 17th of April 2013 01:08:07 PM
Old 04-17-2013
sed - Add a variable line to the end of a block beginning with a regex

Hi,

Need some help with sed.

I have a file that has sections :

e.g.
Code:
[section]
a=blah
b=blah
 
[section2]
d=blah
e=blah


There's many sections in the file.

I have a variable $line which contains a new line.
I have another variable $section with the section.
There may be sapces in the $line

I need to append line $line to section $section.

I tried this but I got a Label too long error :

Code:
section="section"
line="blah = blah"
sed -e '/\['$section'\]/{:a;n;/^$/!ba;i\'"$line"'' -e '}' testFile

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

2. Shell Programming and Scripting

Add white space to the end of a line with sed

Im trying to add 5 blank spaces to the end of each line in a file in a sed script. I can figure out who o put the spaces pretty much anywhere else but at the end. thanks Karl (7 Replies)
Discussion started by: karlanderson
7 Replies

3. Shell Programming and Scripting

Want to use sed to add some parameters at the end of the line

Hello All, I have a doubt in sed, i want to add some parameter at the end of the tag inside a xml tag. how to i do that. so i want to add Results="true" value="high" inside the xml tag. Orignal <execute description="reboot"> <execute description="Stop Servlet"> After adding the... (5 Replies)
Discussion started by: asirohi
5 Replies

4. UNIX for Dummies Questions & Answers

Regex for beginning of line until a comma

What is a regex for "the dalai lama, his holiness the" that would just grab "the dalai lama" and one that would just grab "his holiness the"? Both should exclude the comma.. I was trying '^.*' and many variants with no luck. (6 Replies)
Discussion started by: glev2005
6 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

7. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

8. Shell Programming and Scripting

[Solved] Inserting a line beginning with regex

I have been trying to insert a line after a regex but I can't do it. Here is the code I am using: cat saved_doc SET type = type1 SET type = STORE = y /vol/san_e1 /vol/san_5 /vol/san_e9 /vol/san_e11 /vol/san_e12 /vol/san_e13 /vol/san_e14 /vol/san_e16 /vol/san_e17 /vol/san_e18... (4 Replies)
Discussion started by: newbie2010
4 Replies

9. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

10. Shell Programming and Scripting

Add the word "prefix" to beginning of line using sed

SUSE linux bash shell this works test -d /tmpp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /" prefix directory doesn't exists but why doesn't this work? test -d /tmp && echo "directory exists" || echo "directory doesn't exists" |sed -e... (3 Replies)
Discussion started by: snoman1
3 Replies
Template::Test(3pm)					User Contributed Perl Documentation				       Template::Test(3pm)

NAME
Template::Test - Module for automating TT2 test scripts SYNOPSIS
use Template::Test; $Template::Test::DEBUG = 0; # set this true to see each test running $Template::Test::EXTRA = 2; # 2 extra tests follow test_expect()... # ok() can be called any number of times before test_expect ok( $true_or_false ) # test_expect() splits $input into individual tests, processes each # and compares generated output against expected output test_expect($input, $template, \%replace ); # $input is text or filehandle (e.g. DATA section after __END__) test_expect( $text ); test_expect( *DATA ); # $template is a Template object or configuration hash my $template_cfg = { ... }; test_expect( $input, $template_cfg ); my $template_obj = Template->new($template_cfg); test_expect( $input, $template_obj ); # $replace is a hash reference of template variables my $replace = { a => 'alpha', b => 'bravo' }; test_expect( $input, $template, $replace ); # ok() called after test_expect should be declared in $EXTRA(2) ok( $true_or_false ) ok( $true_or_false ) DESCRIPTION
The "Template::Test" module defines the test_expect() and other related subroutines which can be used to automate test scripts for the Template Toolkit. See the numerous tests in the t sub-directory of the distribution for examples of use. PACKAGE SUBROUTINES
text_expect() The "test_expect()" subroutine splits an input document into a number of separate tests, processes each one using the Template Toolkit and then compares the generated output against an expected output, also specified in the input document. It generates the familiar "ok"/"not ok" output compatible with "Test::Harness". The test input should be specified as a text string or a reference to a filehandle (e.g. "GLOB" or "IO::Handle") from which it can be read. In particular, this allows the test input to be placed after the "__END__" marker and read via the "DATA" filehandle. use Template::Test; test_expect(*DATA); __END__ # this is the first test (this is a comment) -- test -- blah blah blah [% foo %] -- expect -- blah blah blah value_of_foo # here's the second test (no surprise, so is this) -- test -- more blah blah [% bar %] -- expect -- more blah blah value_of_bar Blank lines between test sections are generally ignored. Any line starting with "#" is treated as a comment and is ignored. The second and third parameters to "test_expect()" are optional. The second may be either a reference to a Template object which should be used to process the template fragments, or a reference to a hash array containing configuration values which should be used to instantiate a new Template object. # pass reference to config hash my $config = { INCLUDE_PATH => '/here/there:/every/where', POST_CHOMP => 1, }; test_expect(*DATA, $config); # or create Template object explicitly my $template = Template->new($config); test_expect(*DATA, $template); The third parameter may be used to reference a hash array of template variable which should be defined when processing the tests. This is passed to the Template process() method. my $replace = { a => 'alpha', b => 'bravo', }; test_expect(*DATA, $config, $replace); The second parameter may be left undefined to specify a default Template configuration. test_expect(*DATA, undef, $replace); For testing the output of different Template configurations, a reference to a list of named Template objects also may be passed as the second parameter. my $tt1 = Template->new({ ... }); my $tt2 = Template->new({ ... }); my @tts = [ one => $tt1, two => $tt1 ]; The first object in the list is used by default. Other objects may be switched in with a '"-- use $name --"' marker. This should immediately follow a '"-- test --"' line. That object will then be used for the rest of the test, or until a different object is selected. -- test -- -- use one -- [% blah %] -- expect -- blah, blah -- test -- still using one... -- expect -- ... -- test -- -- use two -- [% blah %] -- expect -- blah, blah, more blah The "test_expect()" sub counts the number of tests, and then calls ntests() to generate the familiar ""1..$ntests "" test harness line. Each test defined generates two test numbers. The first indicates that the input was processed without error, and the second that the output matches that expected. Additional test may be run before "test_expect()" by calling ok(). These test results are cached until ntests() is called and the final number of tests can be calculated. Then, the ""1..$ntests"" line is output, along with ""ok $n"" / ""not ok $n"" lines for each of the cached test result. Subsequent calls to ok() then generate an output line immediately. my $something = SomeObject->new(); ok( $something ); my $other = AnotherThing->new(); ok( $other ); test_expect(*DATA); If any tests are to follow after "test_expect()" is called then these should be pre-declared by setting the $EXTRA package variable. This value (default: 0) is added to the grand total calculated by ntests(). The results of the additional tests are also registered by calling ok(). $Template::Test::EXTRA = 2; # can call ok() any number of times before test_expect() ok( $did_that_work ); ok( $make_sure ); ok( $dead_certain ); # <some> number of tests... test_expect(*DATA, $config, $replace); # here's those $EXTRA tests ok( defined $some_result && ref $some_result eq 'ARRAY' ); ok( $some_result->[0] eq 'some expected value' ); If you don't want to call "test_expect()" at all then you can call "ntests($n)" to declare the number of tests and generate the test header line. After that, simply call ok() for each test passing a true or false values to indicate that the test passed or failed. ntests(2); ok(1); ok(0); If you're really lazy, you can just call ok() and not bother declaring the number of tests at all. All tests results will be cached until the end of the script and then printed in one go before the program exits. ok( $x ); ok( $y ); You can identify only a specific part of the input file for testing using the '"-- start --"' and '"-- stop --"' markers. Anything before the first '"-- start --"' is ignored, along with anything after the next '"-- stop --"' marker. -- test -- this is test 1 (not performed) -- expect -- this is test 1 (not performed) -- start -- -- test -- this is test 2 -- expect -- this is test 2 -- stop -- ... ntests() Subroutine used to specify how many tests you're expecting to run. ok($test) Generates an ""ok $n"" or ""not ok $n"" message if $test is true or false. not_ok($test) The logical inverse of ok(). Prints an ""ok $n"" message is $test is false and vice-versa. callsign() For historical reasons and general utility, the module also defines a "callsign()" subroutine which returns a hash mapping the letters "a" to "z" to their phonetic alphabet equivalent (e.g. radio callsigns). This is used by many of the test scripts as a known source of variable values. test_expect(*DATA, $config, callsign()); banner() This subroutine prints a simple banner including any text passed as parameters. The $DEBUG variable must be set for it to generate any output. banner('Testing something-or-other'); example output: #------------------------------------------------------------ # Testing something-or-other (27 tests completed) #------------------------------------------------------------ PACKAGE VARIABLES
$DEBUG The $DEBUG package variable can be set to enable debugging mode. $PRESERVE The $PRESERVE package variable can be set to stop the test_expect() from converting newlines in the output and expected output into the literal strings ' '. HISTORY
This module started its butt-ugly life as the "t/texpect.pl" script. It was cleaned up to became the "Template::Test" module some time around version 0.29. It underwent further cosmetic surgery for version 2.00 but still retains some remarkable rear-end resemblances. Since then the "Test::More" and related modules have appeared on CPAN making this module mostly, but not entirely, redundant. BUGS
/ KNOWN "FEATURES" Imports all methods by default. This is generally a Bad Thing, but this module is only used in test scripts (i.e. at build time) so a) we don't really care and b) it saves typing. The line splitter may be a bit dumb, especially if it sees lines like "-- this --" that aren't supposed to be special markers. So don't do that. AUTHOR
Andy Wardley <abw@wardley.org> <http://wardley.org/> COPYRIGHT
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Template perl v5.14.2 2011-12-20 Template::Test(3pm)
All times are GMT -4. The time now is 04:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy