Replace a string for every record after the 1st record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string for every record after the 1st record
# 1  
Old 02-10-2016
Replace a string for every record after the 1st record

I have data coming in the below format for each record

Code:
<?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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox>

I need to remove the the XML prolog on the 2nd record until the last record
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>


The o/p should look like below
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox>
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox>

I might have more than 100 records and this should happen to all the records starting from 2nd record.

Last edited by Scrutinizer; 02-10-2016 at 02:35 PM.. Reason: CODE tags
# 2  
Old 02-10-2016
Hello dsravanam,

Could you please use code tags in spite of using quotes for commands/Inputs/codes into your posts as per forum rules. Because of not using code tags your Input_file didn't come properly so let's say you have following Input_file:
Code:
cat Input_file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins.......</test_sox>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins.......</test_sox>

Now following is the code which may help you in same.
Code:
awk 'NR==1{print;next} !/<\?xml version=\"1\.0\" encoding=\"UTF-8\" standalone=\"no\"\?>/{print}'   Input_file

Output will be as follows.
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins.......</test_sox>
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins.......</test_sox>

on a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk. Also if you have a different requirement then request you to please show us sample input with expected sample output, with your O.S details to us to help you.

Thanks,
R. Singh
# 3  
Old 02-10-2016
How about
Code:
awk 'NR>1 {sub (/^<[^>]*>/, "")} 1' file
<?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox>
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox>

---------- Post updated at 16:32 ---------- Previous update was at 16:29 ----------

You may want to refine the regex for the sub function to be more specific if need be.
# 4  
Old 02-10-2016
Thanks Rudic. If i have to replace the below string at the end of each record except the last record what should i use

PHP Code:
</test_sox
# 5  
Old 02-10-2016
Are you sure? That would result in invalid xml elements <test_sox ...> as they all wouldn't be closed.
# 6  
Old 02-10-2016
Rudic

I am sorry for the confusion. Actually I need from the 2nd record onwards to remove the below string as well

Code:
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox>
<test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox>


Last edited by Scrutinizer; 02-10-2016 at 02:34 PM..
# 7  
Old 02-10-2016
Confusion grows. What should the result look like?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

2. 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

3. Shell Programming and Scripting

Replace string and append Indicator to end of record in Linux

Hi All, Could you please help me to achieve below solution. I have a FILE1.txt as below.TEXAS CALIFORNIA TEXAS DALLAS CALIFORNIA CALIFORNIA DALLAS DALLAS TEXAS TEXAS DALLAS NEW YORK NEW YORk FILE2.txt as below.TEXAS,TX DALLAS,DA Now I need to compare the string in FILE2.txt... (6 Replies)
Discussion started by: ureddy
6 Replies

4. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

5. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

7. UNIX for Advanced & Expert Users

Print Full record and substring in that record

I have i got a requirement like below. I have input file which contains following fixed width records. 00000000000088500232007112007111 I need the full record and concatenated with ~ and characters from 1to 5 and concatenated with ~ and charactes from 10 to 15 The out put will be like... (1 Reply)
Discussion started by: ukatru
1 Replies

8. UNIX for Dummies Questions & Answers

how to read record by record from a file in unix

Hi guys, i have a big file with the following format.This includes header(H),detail(D) and trailer(T) information in the file.My problem is i have to search for the character "6h" at 14 th and 15 th position in all the records .if it is there i have to write all those records into a... (1 Reply)
Discussion started by: raoscb
1 Replies

9. Shell Programming and Scripting

splitting a record and adding a record to a file

Hi, I am new to UNIX scripting and woiuld appreicate your help... Input file contains only one (but long) record: aaaaabbbbbcccccddddd..... Desired file: NEW RECORD #new record (hardcoded) added as first record - its length is irrelevant# aaaaa bbbbb ccccc ddddd ... ... ... (1 Reply)
Discussion started by: rsolap
1 Replies

10. Shell Programming and Scripting

how to replace field for each record

Hello, I have the following xml formatted file. I would like to get the newnumber field number and replace into customernumber for each record. For example: <XMLFORMAT> <customernumberR11>9</customernumberR11> ... (12 Replies)
Discussion started by: happyv
12 Replies
Login or Register to Ask a Question