Replacing pattern spanning multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing pattern spanning multiple lines
# 1  
Old 04-30-2011
Replacing pattern spanning multiple lines

Hi. I have input like this:
Code:
	<tr>
		<td class="logo1" rowspan="2"><a href="index.html"><img
			src="images/logo.png" /></a></td>
		<td class="pad1" rowspan="2">__</td>
		<td class="userBox"><img src="images/person.png"/> <a href="http://good.mybook.com/login.jsp">Sign In</a></td>
		<td class="searchBox"><a href="http://good.mybook.com/searchhelp.jsp">Search mybook</a>
		<input type="text" size="14" id="searchTextBox"
			onkeypress="if(event.keyCode == 13) { search(); return false; }" />
		<input type="button" value="Go" onclick="search();" /></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td class="bentyLogo"><a
			href="http://www.comp.benty.ac.uk/parsic"><img
			src="images/benty.png" /></a></td>
	</tr>
	<tr>
		<td class="logoPad">__</td>
		<td class="pad2">__</td>
		<td class="title">
		<h2>Cat-by-Cat mybook - Cook (56:47)</h2>
		</td>
		<td class="bentyText"><a
			href="http://www.comp.benty.ac.uk/parsic">Mona Research Group</a></td>
	</tr>

I would like to delete the whole thing from <tr to </tr> for the first and third "tr" tag pairs. The problem is I have the same pattern of <tr.*/tr> all over (not just the ones in the input sample) and I do not want to delete them all so I can not simply use sed s/<tr.*\/tr>//g. So I thought maybe try sed s/<tr.*pad2.*\/tr>//g which obviously will not work. I have been searching google and trying to solve this all day but to no avail. Please help. Thanks.
# 2  
Old 04-30-2011
How is your input file look like? Does it have only these <tr></tr> data?

If so, is this your required output?

Code:
<tr>         
<td>&nbsp;</td>         
<td class="bentyLogo"><a href="http://www.comp.benty.ac.uk/parsic">
<img src="https://www.unix.com/images/benty.png" /></a>
</td>    
</tr>

regards,
Ahamed
# 3  
Old 04-30-2011
Thank you for that quick reply brother.
Yes, if it based on just that input sample. Unfortunately there are a lot more "tr" pairs in the original input. By the way, how would you do it if it based just on the input sample?

Thanks. Salam.
# 4  
Old 05-01-2011
I try to write for this issue..There are maybe some bugs but you can test your inputfile.I hope it is helpful for you..Smilie

Code:
# cat file
<tr>
                <td class="logo1" rowspan="2"><a href="index.html"><img
                        src="images/logo.png" /></a></td>
                <td class="pad1" rowspan="2">__</td>
                <td class="userBox"><img src="images/person.png"/> <a href="bookarmy In</a></td>
                <td class="searchBox"><a href="bookarmy mybook</a>
                <input type="text" size="14" id="searchTextBox"
                        onkeypress="if(event.keyCode == 13) { search(); return false; }" />
                <input type="button" value="Go" onclick="search();" /></td>
        </tr>
        <tr>
                <td>&nbsp;</td>
                <td class="bentyLogo"><a
                        href="http://www.comp.benty.ac.uk/parsic"><img
                        src="images/benty.png" /></a></td>
        </tr>
        <tr>
                <td class="logoPad">__</td>
                <td class="pad2">__</td>
                <td class="title">
                <h2>Cat-by-Cat mybook - Cook (56:47)</h2>
                </td>
                <td class="bentyText"><a
                        href="http://www.comp.benty.ac.uk/parsic">Mona Research Group</a></td>
        </tr>
        <tr>
                <td class="logoPad">__</td>
                <td class="pad2">__</td>
                <td class="title">
                <h2>Cat-by-Cat mybook - Cook (56:47)</h2>
                </td>
                <td class="bentyText"><a
                        href="http://www.comp.benty.ac.uk/parsic">Mona Research GroupXXX</a></td>
        </tr>

Code:
#./justdoit 1,3 tr file
       <tr>
               <td>&nbsp;</td>
               <td class="bentyLogo"><a
                       href="http://www.comp.benty.ac.uk/parsic"><img
               <h2>Cat-by-Cat mybook - Cook (56:47)</h2>
               </td>
               <td class="bentyText"><a
                       href="http://www.comp.benty.ac.uk/parsic">Mona Research Group</a></td>
       </tr>
       <tr>
               <td class="logoPad">__</td>
               <td class="pad2">__</td>
               <td class="title">
               <h2>Cat-by-Cat mybook - Cook (56:47)</h2>
               </td>
               <td class="bentyText"><a
                       href="http://www.comp.benty.ac.uk/parsic">Mona Research GroupXXX</a></td>
       </tr>

Code:
#!/bin/bash
## justdoit SED scripts##
 
if [ $# != 3 ]  ; then
echo "Usage $0 tagpairs_nr[1 and 3] tagname[tr,pre,table..] inputfile
eg.. '$0 1,3 tr index.html'" ; exit 1
fi
 
removes=$1
if [ ! $(echo "$removes"|sed -n '/[1-2],[3-6]/p') ] ; then
echo "Tagpair numbers must as [1-2],[3-6] format" ; exit 1
fi
 
tag=$2
f=$(echo $removes|sed 's/\([0-9]\),[0-9]/\1/')
l=$(echo $removes|sed 's/[0-9],\([0-9]\)/\1/')
 
if (( $f >= 3 )) ; then
echo "Script works only as first value in [1 and 2] digit" ; exit 1
fi
 
file=$3
cp $file "${file}bck" ; if [ $? -ne 0 ] ; then
echo "Backup infile process is unsuccess" ; exit 1
fi
 
case $f in
  1) start=4 ;;
  2) start=2 ;;
esac
 
case $l in
  3) scale=6 ;;
  4) [ $f = 1 ] && scale=(2 6) || scale=4 ;;
  5) [ $f = 1 ] && scale=(2 2 6) || scale=(4 2 4) ;;
  6) [ $f = 1 ] && scale=(2 2 2 6) || scale=(4 2 2 4) ;;
esac
 
 
taglines=$(sed -n '/[/]*'$tag'/=' $file|sed -n '$=')
tagpairlines=$(sed -n '/[/]*'$tag'/=' $file|sed '$!N;s/\n/,/'|sed -n '$=')
calv=$(echo $l | awk '{print '$taglines' / $1 }')
[ $(echo $calv | sed -n '/[0-9]\.[0-9]*$/p') ] && calv=$(echo ${calv%.*}) && calv=$(( calv + 1))
sedarrix=$(( tagpairlines - calv ))
 
x=0 ; limit=${#scale[@]}
pattern=("$start s/,*//;")
second=start
while [ $(( sedarrix -=1 )) -gt -1 ] ; do
resume=${scale[x]}
second=$(( second  + resume ))
pattern=(${pattern[@]} "$second s/.*//;")
((x++)) ; [ $x = $limit ] && x=0
done
 
fullsed="sed -n '/[/]*'$tag'/=' $file | sed '$!N;s/\n/,/; ${pattern[@]} '"
fullsedarr=($(eval $fullsed |sed ':a;$!N;s/\n/ /;ta') ) ;
x=0 ; fullsedarrix=${#fullsedarr[@]}
removepairs=${fullsedarr[x]}
 
# open file and sed processing.
while [ $(( fullsedarrix -=1 )) -gt 1 ] ; do
sed ' '$removepairs' d' $file >${file}tmp && mv ${file}tmp $file
((x++)) ; correctsedv=$(($(echo ${fullsedarr[x]}|sed 's/\([0-9]*\),\([0-9]*\)$/\2-\1+1/')))
fullcorrectsedv=$((fullcorrectsedv+correctsedv))
corrv=($(echo ${fullsedarr[x]}|sed 's/\([0-9]*\),\([0-9]*\)$/\1-'$fullcorrectsedv' \2-'$fullcorrectsedv'/'))
y=0 ; for i in ${corrv[@]} ; do reorg[y]=$(($i)); ((y++)) ; done
removepairs=$(echo ${reorg[@]}|sed 's/ /,/' )
done
more $file

regards
ygemici
# 5  
Old 05-01-2011
Try this...
Code:
#to delete 1 and 3rd <tr>...</tr> pairs
awk '/<tr>/ {i=i+1}  {if(i==1 || i==3){next} print}' file

if further if you want to delete say only the second pair, tweak the above code as
Code:
#to delete 2nd <tr>...</tr> pair
awk '/<tr>/ {i=i+1}  {if(i==2){next} print}' file

regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 05-02-2011
Your code is long ygemici. Thanks.

---------- Post updated at 08:57 AM ---------- Previous update was at 08:47 AM ----------

Thank you Mr Ahamed. Your code is much better.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. Shell Programming and Scripting

searching multiple lines and replacing in shell scripting

Hi, I have a file with below contents, ssenthil = rw anilkg = rw I want to search for "ssenthil" and need to delete line 1 and 2 , if the third line starts with "" respectively and blank line immediately and third line starts with " anilkg = rw Please help me . Great day... (5 Replies)
Discussion started by: anil8103
5 Replies

5. Shell Programming and Scripting

CSV: Replacing multiple occurrences inside a pattern

Greatings all, I am coming to seek your knowledge and some help on an issue I can not currently get over. I have been searching the boards but did not find anything close to this matter I am struggling with. I am trying to clean a CSV file and make it loadable for my SQL*Loader. My problem... (1 Reply)
Discussion started by: OCanada
1 Replies

6. Shell Programming and Scripting

Script to display record spanning over multiple lines

Following are the lines from /etc/sudoers.conf bob SPARC = (OP) ALL : SGI = (OP) ALL fred ALL = (DB) NOPASSWD: ALL ALL CDROM = NOPASSWD: /sbin/umount /CDROM,\ /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM Could you please help me with shell/perl script to display the records with... (2 Replies)
Discussion started by: Ujan
2 Replies

7. Shell Programming and Scripting

replacing multiple lines with single line

Can any one give me the idea on replacing multiple blank lines with a single blank line? Please conside it for a file having more than 100 number of characters. Regards, Siba (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

8. UNIX for Dummies Questions & Answers

backup spanning multiple DVDs

Fedora Core 6 2.6.20-1.2962.fc6 I have about 15 GB of data that I need to backup to Single-Layer DVDs. Is there a backup method that will allow me to backup spanning multiple DVDs? (0 Replies)
Discussion started by: dangral
0 Replies

9. Shell Programming and Scripting

using sed command to delete a string spanning multiple lines

file1 contains the following data sssssssssss firstline secondline pppppppppp ssssssssss Using sed comamnd i am trying to delete firtsline secondline. so, output should be sssssssssss pppppppppp ssssssssss I tried in the following the way, but it is not working. sed ... (9 Replies)
Discussion started by: radha.kalivar
9 Replies

10. Shell Programming and Scripting

replacing multiple lines

i have a file : sample1.txt OBJECT="POINT" ACTION="REDEFINE" POINT_NAME="ABCD001G " GHYT_POPRIORITY_1="1" GHYT_POPRIORITY_2="1" GHYT_POPRIORITY_3="1" GHYT_POPRIORITY_4="1" GHYT_POPRIORITY_USER="1" HIGH_ALARM_PRIORITY_1="1" HIGH_ALARM_PRIORITY_2="1" HIGH_ALARM_PRIORITY_3="1" ... (1 Reply)
Discussion started by: ajnabi
1 Replies
Login or Register to Ask a Question