replace using pattern using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace using pattern using awk or sed
# 1  
Old 09-25-2012
CPU & Memory replace using pattern using awk or sed

I have file file_1.txt which contains:
file_1.txt
Code:
<tr> 1 MAIL # 1 TO src_1 </tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">a</td> <td class="bwcol">15</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">b</td> <td class="bwcol">90</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">c</td> <td class="bcol">6</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">d</td> <td class="bcol">4</td></tr>

I am trying to count no of occurence of '</td>' from line 2 and store it to a variable
'x' and print like below:

For getting the x I am using
Code:
nawk -F'td ' 'NR==2 { print NF-1}'

Now I have to use the x and replace the first line as below
here x is 4

Code:
<tr><td colspan = 4 > 1 MAIL # 1 TO src_1 </td></tr>
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">a</td> <td class="bwcol">15</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">b</td> <td class="bwcol">90</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">c</td> <td class="bcol">6</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">d</td> <td class="bcol">4</td></tr>

Thanks for your help.
# 2  
Old 09-25-2012
Try:
Code:
#!/bin/ksh
awk '/MAIL #/ {save=$0;next}
save!="" {
        x=gsub("</td>","&")
        sub(" .* <","<td colspan=" x ">&/td><", save)
        print save
        save=""
}
        {print}
' file_1.txt

You'll want to use nawk instead of awk and you'll probably want to change /bin/ksh to the pathname for you shell. (I use ksh, but this will work at least with bash, ksh, and sh.)

This script doesn't put as many spaces in the td colspan tag as you had, but it should do what you want.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-25-2012
Try
Code:
awk 'NR==1{getline L2; $1=$1"<td colspan = "gsub(/\/td/,"&",L2)" >";$8="</td>"$8;print; print L2} NR>2' file


Last edited by RudiC; 09-25-2012 at 05:05 PM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find pattern and replace using sed

Hi, i want to replace the following lines in such a way that if the word merge exists in first column it must replace the 3rd column as M and if parse exists in first column then the last column must P, if neither it must mark it as X. I have tried the solution using awk, but it is saying... (6 Replies)
Discussion started by: charlie87
6 Replies

2. UNIX for Beginners Questions & Answers

sed replace pattern

I have a file with multiple lines, all in the same format. For each line, I need to replace the sequence of digits after the last : with a new value, but keep the single quote at the end of the line. Example: Input: ( two lines of file) Name: 'text1:200/text2:1.2.3.4' Name2:... (19 Replies)
Discussion started by: Beginner101
19 Replies

3. Shell Programming and Scripting

sed find/replace a pattern, but not this one..

I've got a file like so: ...lots of lines, etc. push "route 10.8.0.0 255.255.255.0" push "route 192.168.1.123 255.255.255.0" ...lots of lines, etc. I want to sed find/replace the IP address in the second line, whatever it is, with a new IP address, but I don't want to touch the first line.... (5 Replies)
Discussion started by: DaHai
5 Replies

4. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

6. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

Pattern Replace using sed or awk

Hi , My file have data like 4:ALMOST NEVER PR 1925836 5:NEVER PR W DDA 5857610 6:NEVER PR WO DDA 26770205 but i want to replace the spaces before last numric digits out put should be like this 4:ALMOST NEVER PR=1925836 5:NEVER PR W DDA=5857610 6:NEVER PR WO... (7 Replies)
Discussion started by: max_hammer
7 Replies

9. Shell Programming and Scripting

How to replace the last pattern using sed?

myfile: AAAaaa BBBbbb CCCccc AAAeee DDDddd how to replace the last AAA as EEEEE using sed? like this: AAAaaa BBBbbb CCCccc EEEEEeee DDDddd (14 Replies)
Discussion started by: vistastar
14 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question