0

I have a text file like this:

B1=text1
B2=text2
B3=text3

I have the following sed that will convert this text format to text between tags.

echo "String= Hello World" | sed 's#\([^=]\+\)=\(.*\)#<\1>\2</\1>#'

This sed gives this output:

<String > Hello World</String >

How to put the tag only if String beguns with B and a number, for example B45? so string will be B45

If String matches B+number

Then Sed command to give this output (example)

<B1>text1</B1>
<B2>text2</B2>
<B3>text3</B3>
Abdel
  • 11
  • 4

2 Answers2

0

I think this will work for You :

echo "String= Hllo World" | sed 's#\([^=]\+\)=\(\s*B[0-9]*.*\)#<\1>\2</\1>#'
Take_Care_
  • 1,957
  • 1
  • 22
  • 24
  • What happend here : I added : \s* - match as many whitespace as possible B - match letter B [0-9]* - match as many numbers as possible I think it will do the job for You – Take_Care_ May 22 '16 at 23:00
  • but it removes the tags and the condition is still not applied. – Abdel May 22 '16 at 23:08
  • What do You mean ? Your question was "How to put the tag only if String beguns with B and a number" So this is answer. Try giving more details so I will try help – Take_Care_ May 22 '16 at 23:32
  • Edited, check please. – Abdel May 23 '16 at 21:20
  • so for this proupose sed command will look like this : sed 's#\(B[0-9]\+\)=\(.*\)#<\1>\2\1>#' – Take_Care_ May 31 '16 at 14:00
  • Thanks , this was the solution `echo "B56=Hello World | Tesfsqd f" | sed 's#\(B[0-9][^=]\+\)=\(.*\)#<\1>\2\1>#'` – Abdel Jun 05 '16 at 23:52
0

Please don't use regular expression to manipulate XML. XML is a contextual language. Regex isn't, so it can NEVER work properly. At best, you have a dirty hack, that will one day break for no discernible reason, because it's making assumptions that aren't valid.

Please use a parser. It's not hard, but does mean you avoid creating brittle code.

Longhand in perl, it's:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $input = "String= Hello World";
my ($tag, $content) = split /=/, $input;

XML::Twig::Elt -> new ( $tag, $content ) -> print;

This outputs:

<String> Hello World</String>

As a more extensive example:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $input = "";
my ( $tag, $content ) = split /=/, $input;

my $doc = XML::Twig->new( pretty_print => 'indented_a' ) ;
$doc->set_xml_version("1.0");
$doc->set_encoding('utf-8');
$doc->set_root( XML::Twig::Elt->new('root') );

while (<>) {
   chomp;
   my ( $tag, $content ) = split /=/;
   if ( $content =~ m/^B/ ) {
      $doc->root->insert_new_elt( 'last_child', $tag, $content );
   }
}

$doc->print;

Input of:

String= Hello World
tag=B1234 some text here
newtag=fish heads fish heads roly poly fish heads
String=Better fun joy here

Gives a result of:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <String>Better fun joy here</String>
  <tag>B1234 some text here</tag>
</root>

It's not too hard to use a proper parser, and if you need more reason to do so: RegEx match open tags except XHTML self-contained tags

Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101