Reputation: 68
This is my file example.txt
{foo}
{bar}
{f}oo}
{ba{r}
{fo}o}
{b{ar}
I want this result:
<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>
i have command in notepad++ to get result as above
([}]\r\n)|(\r\n[{])|(^[{])|([}]$)
(?1}</div>)(?2<div class="id">{)(?3<div class="id">{)(?4}</div>)
I tried with this sed
command
sed -i 's@}\r\n@</div>@g' *.html
which left my file unchanged. How to do this correctly with sed
?
Upvotes: 2
Views: 213
Reputation: 43089
How about using two simple expressions, like this:
sed -i '' -e 's/^./<div class="id">/;s/.$/<\/div>/' file
<div class="id">
and the last character with </div>
leaving the rest of the line intact.Gives this output:
<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>
Upvotes: 1
Reputation: 23924
It may interest you to user Perl
$ cat file
{foo}
{bar}
{f}oo}
{ba{r}
{fo}o}
{b{ar}
$
$ perl -lpe 's/^.(.*?).$/<div class="id">\1<\/div>/' file
$
<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>
It also has save in place like sed
with -i
Upvotes: 0
Reputation: 92894
Use the following sed
command with specific regex pattern:
sed -ri 's/\{(.+)\}/<div class="id">\1<\/div>/g' testfile
-r
option allows extended regular expressions
\{(.+)\}
- matches any characters enclosed with curly braces {}
\1
- points to the first captured group which is (.+)
Upvotes: 2
Reputation: 247210
How about
sed -e 's/{\(.*\)}/<div class="id">\1<\/div>/'
sed is line-oriented, so you won't find \n
in a record (unless you're using some of the advanced commands).
Upvotes: 0