how to replace field for each record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to replace field for each record
# 8  
Old 06-26-2007
Try nawk instead of awk


Another way always with awk (or nawk)
Code:
awk -F '[<>]' '
   $2=="customernumberR11" { next }
   $2=="newnumberR11" { print "<customernumberR11>" $3 "</customernumberR11>"}
   1
' file

# 9  
Old 06-26-2007
yes...I tried to you nawk or awk. but the script look like not work. It move the cusomernumber after the newnumber and duplicated the newnumber


<XMLFORMAT>

<newnumberR11>30</newnumberR11>
<customernumberR11>30</customernumberR11>
<newnumberR11>30</newnumberR11>
<customerdetailR11>
# 10  
Old 06-26-2007
The two awk solutions (ghostdog74 and mine) work fine on my AIX box
Input file:
Code:
$ cat xml.txt
<XMLFORMAT>
<customernumberR11>9</customernumberR11>
<newnumberR11>30</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>20</customernumberR11>
<newnumberR11>18</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>11</customernumberR11>
<newnumberR11>32</newnumberR11>
<customerdetailR11>

</XMLFORMT>

$

The ghostdog74' solution :
Code:
$ cat xml2.sh
awk '/<customernumberR11>/{ line=$0 }
     /<newnumberR11>/{  
        current=$0
        gsub("<newnumberR11>|</newnumberR11>","",$0)
        gsub(/>(.*)</ , ">"$0"<",line)
        print line
        print current   
        next
     }
     !/<customernumberR11>/ && !/<newnumberR11>/ {print}
' xml.txt
$ sh xml2.sh
<XMLFORMAT>
<customernumberR11>30</customernumberR11>
<newnumberR11>30</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>18</customernumberR11>
<newnumberR11>18</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>32</customernumberR11>
<newnumberR11>32</newnumberR11>
<customerdetailR11>

</XMLFORMT>

$

My solution :
Code:
$ cat xml.sh
awk -F '[<>]' '
   $2=="customernumberR11" { next }
   $2=="newnumberR11" { print "<customernumberR11>" $3 "</customernumberR11>"}
   1
' xml.txt
$ sh xml.sh
<XMLFORMAT>
<customernumberR11>30</customernumberR11>
<newnumberR11>30</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>18</customernumberR11>
<newnumberR11>18</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>32</customernumberR11>
<newnumberR11>32</newnumberR11>
<customerdetailR11>

</XMLFORMT>

$

# 11  
Old 06-26-2007
oh...very sorry..
forgot provide one more IMPORTANT information. The customernumber and newnumber field is not the same field number for each records. Sometime, the customernumber in row 1, sometime in row 2. Therefore, it difficult for me..

<XMLFORMAT>
....
...
<customernumberR11>9</customernumberR11>
<newnumberR11>30</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
....
<customernumberR11>20</customernumberR11>
...
<newnumberR11>18</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
....
...
...
<customernumberR11>11</customernumberR11>
....
..
<newnumberR11>32</newnumberR11>
<customerdetailR11>

</XMLFORMT>
# 12  
Old 06-26-2007
hey iam trying like this, but iam not getting

while read line
do
str1=`grep 'customernumberR11' file1 | cut -c 20-21`
str2=`grep 'newnumberR11' file1 | cut -c 15-16`
if [ "$str1" -ne "$str2" ] ; then
sed -e "s/newnumberR11/ s/$str2/\$str1/g" $line
fi
done < file1
# 13  
Old 06-26-2007
Try this :
Code:
awk -F '[<>]' '
   $2=="customernumberR11" { customernumber = 1; next}
   $2=="newnumberR11" { number = $3 ; newnumber = 1; next}
   customernumber && newnumber {
      printf("<customernumberR11>%d</customernumberR11>\n<newnumberR11>%d</newnumberR11>\n", number, number);
      customernumber = newnumber = 0;
   }
   1
' xml.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace value in each field until a certain character in each record?

Each record coming with column names. I have to replace them in each record as shown below TIME=20181219110000261|CHAN=FMBKHJBAAAADPCFNAAAAAABA|EVNT=SWIclst|VALU=Session FMBKHJBAAAADPCFNAAAAAABA started|SRC=NSS|UCPU=0|SCPU=0 Output should look like: ... (9 Replies)
Discussion started by: sudhakar1987
9 Replies

2. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

3. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

4. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

5. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

6. Shell Programming and Scripting

Replace field in one file with whole record data of another

Hello Group, I need to replace the city field in “File 1 (fileld 3), with the entire record line of “File 2” (including delimiters) where the “city” field (File 1, Field 3)matches city field (File 2, Field1). All of the other data in “File 1” should remain intact(Fields 1,2,4,5,6). Only field... (1 Reply)
Discussion started by: vestport
1 Replies

7. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. Shell Programming and Scripting

Replace comma by space for specified field in record

Hi, i want to replace comma by space for specified field in record, i mean i want to replace the commas in the 4th field by space. and rest all is same throught the record. the record is 16458,99,001,"RIMOUSKI, QC",418,"N",7,EST,EDT,902 16458,99,002,"CHANDLER,... (5 Replies)
Discussion started by: raghavendra.cse
5 Replies

9. Shell Programming and Scripting

Replace third field of the first record in a file....

Hi, I am new to unix and am trying to do something below: I have a pipe delimited file with millions of records. I need to replace the third column of the first record to the number of lines in the file. How can I do that. Will appreciate any advice and help. Thanks Simi (3 Replies)
Discussion started by: simi28
3 Replies

10. Shell Programming and Scripting

get a field from a record

I have a file as: A,B,C,D,E G,H,I,J,K I need to find if fourth field is blank or has a space and print that line to other file. I tried using awk but am not getting the desired result. Pls help. (6 Replies)
Discussion started by: praveenK_Dudala
6 Replies
Login or Register to Ask a Question