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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed, awk [TAG]$content[/TAG] How to get var in $content in textfile?
# 1  
Old 03-04-2010
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:


Code:
[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 Smilie

thanx in advance
# 2  
Old 03-04-2010
You can try with sed:

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

will output:
Code:
$var1

$var2

$var3


Last edited by Scott; 03-05-2010 at 01:20 AM.. Reason: Code tags
# 3  
Old 03-04-2010
Code:
brkt="]"
sed "s/\[[^$brkt]*\]//g" file

# 4  
Old 03-04-2010
Thx for the replies! But if the content is for example:

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


and in text file looks like this:

[nfo]$nfotext[/nfo]

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

Last edited by atmosroll; 03-04-2010 at 05:50 PM..
# 5  
Old 03-04-2010
If the [nfo]$nfotext[/nfo] are in the text file, where are the replacement strings? ie, Where is the 'nfotext="$(cat "$nfo")"' input?
# 6  
Old 03-05-2010
Quote:
Originally Posted by atmosroll
Thx for the replies! But if the content is for example:

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


and in text file looks like this:

[nfo]$nfotext[/nfo]

How do i get the nfo content between the nfo tags?
try something like this (assuming here that you are replacing the literal string "$nfotext")


Code:
#!/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...
# 7  
Old 03-05-2010
You can also do this in the following way.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find files in sub dir with tag & add "." at the beginning [tag -f "Note" . | xargs -0 {} mv {} .{}]

I am trying find files in sub dir with certain tags using tag command, and add the period to the beginning. I can't use chflags hidden {} cause it doesn't add period to the beginning of the string for web purpose. So far with my knowledge, I only know mdfind or tag can be used to search files with... (6 Replies)
Discussion started by: Nexeu
6 Replies

2. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

XML Parse between to tag with upper tag

Hi Guys Here is my Input : <?xml version="1.0" encoding="UTF-8"?> <xn:MeContext id="01736"> <xn:VsDataContainer id="01736"> <xn:attributes> <xn:vsDataType>vsDataMeContext</xn:vsDataType> ... (12 Replies)
Discussion started by: pareshkp
12 Replies

5. Shell Programming and Scripting

Search for a html tag and print the entire tag

I want to print from <fruits> to </fruits> tag which have <fruit> as mango. Also i want both <fruits> and </fruits> in output. Please help eg. <fruits> <fruit id="111">mango<fruit> . another 20 lines . </fruits> (3 Replies)
Discussion started by: Ashik409
3 Replies

6. HP-UX

XML tag name content replacement

Hi, Need to replace an XML tag name contents, please provide any suggestions. Scenario is : <abc_def>Value_some_content</abc_def> Expected output : <abc:def>Value_some_content</abc:def> We have many tag with different names & contents in a file or a string. Please help on the... (3 Replies)
Discussion started by: periyasamycse
3 Replies

7. Shell Programming and Scripting

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

8. Shell Programming and Scripting

changing files content with sed or awk

Hi, Example File: (jumped, bumped, ) how to jumped, FROM tree; EXIT I have some hundreads of files like this with the different words and I want to remove the comma before the bracket and also I have to remove the comma before FROM word. I am trying to use this command : awk '... (5 Replies)
Discussion started by: rajshashi
5 Replies

9. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies
Login or Register to Ask a Question