sed, awk [TAG]$content[/TAG] How to get var in $content in textfile?

Hello,

I got a Qstion. Im posting to a phpbb forum with bash and curl.. i have a text file with the following tags that i post to the forum:


[bb]$var1[/bb]
        
[aa]$var2[/aa]
        
[cc]$var3[/cc]
 

How can i with sed or awk put var content from shell script between the [tags]...[/tags] in the textfile? I tried with awk and sed to replace it. But i just dont get it to work :frowning:

thanx in advance

You can try with sed:

sed -e 's/\[.*\($.*\)\[.*/\1/g' file.txt

will output:

$var1

$var2

$var3
brkt="]"
sed "s/\[[^$brkt]*\]//g" file

Thx for the replies! But if the content is for example:

nfotext="$(cat "$nfo")"


and in text file looks like this:

[nfo]$nfotext[/nfo] 

How do i get the nfo content between the nfo tags?

If the [nfo]$nfotext[/nfo] are in the text file, where are the replacement strings? ie, Where is the 'nfotext="$(cat "$nfo")"' input?

try something like this (assuming here that you are replacing the literal string "$nfotext")

#!/bin/bash

TAGS="nfo1 nfo2 nfo3 nfo4"

nfo1var="text for nfo1"
nfo2var="text for nfo2"
nfo3var="text for nfo3"
nfo4var="text for nfo4"

cmdfile=./cmds.sed
echo "#commands to insert data between tags" > $cmdfile

for i in $TAGS; do
    eval REP="\$${i}var"
    echo "s/\(\$${i}text\)/"$REP"/ " >> $cmdfile
done

sed -f "$cmdfile" infile.dat > outfile.dat

easy to change this so you don't need a cmds.sed file...

You can also do this in the following way.

sed -r 's/\[(.*)?\](.*)\[\/\1?\]/\2/g' <filename>

Thanks again, but i dont get it.. :frowning: Let me try to explain it better.

i have a file called: file.nfo and file.txt

the content of file.nfo is

test23423
test 234
test3443
whole lot of text

the content of file.txt is

[nfo]nfotxt[/nfo]

I want to replace nfotxt with the content of the file.nfo

i do that with:

txtfile=file.txt
nfo=file.nfo
nfotext="$(cat "$nfo")"

then i use sed to search and replace like:

sed -e "s/nfotxt/$nfotext/g" $txtfile

The problem i have is that it doesnt replace the nfotxt with the content of the file.nfo
and that it wil replace it with the name of the file rather then the content.. Hope someone can help me with this..