extract till occurance of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract till occurance of a string
# 1  
Old 06-29-2010
extract till occurance of a string

hi ,
i have an xml that comes in a single, the entire xml file is read as a single line when i open in edit plus or unix. i need to amend the contents of this xml file.

below is the extract from the file

HTML Code:
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><data><IntegratedSite xmlns="http:google.com">[B]<Pattern>[/B]<BladeSystem><name>SIS</name><alias>SIS</alias><swgRef xmlns:IntegratedSite="http://www.google.com">/IntegratedSite:IntegratedSite/IntegratedSite:Software/IntegratedSite:SoftwareInventory/IntegratedSite:SoftwareGroup[IntegratedSite:prodNo="CXS10138"][IntegratedSite:prodRev="R4L06"]</swgRef>
I need to append <Is> before the occurance of tag <Pattern> ..

i tried the below but it did not work ..

Code:
sed -e 's/<Pattern>/<Is><Pattern>/' file >file1.

but it is generating file1 with zero byte for some reason .. Please suggest what could be done here.
# 2  
Old 06-29-2010
Strange, but the same is working OK with me. Try like this:

Code:
 
sed -e 's!<Pattern>!<Is><Pattern>!' file > file1

Please note that if the file is from windows you need to check for carriage returns and try dos2unix.
# 3  
Old 06-29-2010
Quote:
Originally Posted by sais
Code:
sed -e 's/<Pattern>/<Is><Pattern>/' file >file1.

Did you verify that the blue marked part of the command works, respectively prints some output Smilie
# 4  
Old 06-29-2010
i tried that command with dummy input files, they replace the string <Pattern> with <Is><Pattern> but when i try it on the xml file then it is not giving any output.

i tried the below ... but that is also generating a file with zero byte

Code:
sed -e 's!<Pattern>!<Is><Pattern>!' file > file1

# 5  
Old 06-29-2010
What does it give:
Code:
perl -ne '/...Pattern.../;print $&' file | od -c

# 6  
Old 06-30-2010
Code:
0000000   s   >   <   P   a   t   t   e   r   n   >   <   B
0000015

this was the output for that i got for that perl command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string until 3Rd occurance of forward slash(/)

I have a file abc.txt which has records like 456 /home/fgg/abdc.txt 3567 /home/fdss/vfgb.txt 23 /home/asd/dfght.txt I WANT TO REMOVE STRING UNTIL 3RD OCCURANCE OF FORWARD SLASH Output should be like abdc.txt vfgb.txt dfght.txt (5 Replies)
Discussion started by: himanshupant
5 Replies

2. UNIX for Beginners Questions & Answers

Extract only first occurance

Hi All, From the below file. I need to get only the first occurrence and print. I tried to do it in separate grep not coming as expected Original file 11001;1213;304;;;;;;;111020677.64;;;;;;;;;;;;;;;;;;;;;;;;;; 11001;1214;304;;;;;;;102376462.96;;;;;;;;;;;;;;;;;;;;;;;;;;... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

3. Shell Programming and Scripting

Extract multiple occurance of strings between 2 patterns

I need to extract multiple occurance strings between 2 different patterns in given line. For e.g. in below as input ------------------------------------------------------------------------------------- mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)... (8 Replies)
Discussion started by: sameermohite
8 Replies

4. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

5. Shell Programming and Scripting

Removing last occurance of string in text

I have text file as follows and would like to remove the last occurance of "UNION ALL" string and replace @@ with single quote ('). Input text in file is with temp as ( ( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL ( select... (9 Replies)
Discussion started by: Vaddadi
9 Replies

6. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

7. UNIX for Dummies Questions & Answers

String search - Command to find second occurance

Hi, I am new to Unix world. Is there any command which can directly return the second occurance of a particular string in a file? Basically, I want to read the contents of the file from the second occurance of a particualr string. Can be implemented using a loop, but am just wondering if there... (5 Replies)
Discussion started by: saurabhsinha23
5 Replies

8. UNIX for Dummies Questions & Answers

count string occurance in a file hourly

Hi, I file that has all the status for one day (24hours). Now what I want to do is to count the occurence of a string in its output hourly like for example count occurance of successful or asynchronous clear destinon for every hour and redirect it to file. Please see sample file below. Please... (2 Replies)
Discussion started by: ayhanne
2 Replies

9. Shell Programming and Scripting

Find term and extract till end of data

Hi All, I have an awk code below. How can i edit it such that once it finds the term "start" , it will extract all the rest of data thereafter ? awk '/start/, /stop/' file (4 Replies)
Discussion started by: Raynon
4 Replies

10. Shell Programming and Scripting

Removing the last occurance of string

I have a small query. I have a file containing the following lines abcd<12></12>fdfgdf<12>sdfgfg<12> sdfsdf<12></12>ytunfg<12> hggfhf<12>rtysb<12>zdfgdfg<12> Now I wish to delete ONLY the last occurance of string <12> from every lines of code. That mease my final output will be like this:... (7 Replies)
Discussion started by: dkhanna01
7 Replies
Login or Register to Ask a Question