The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 06:11 AM
search excat string in another string (grep "fails") bora99 UNIX for Dummies Questions & Answers 0 06-05-2008 06:41 AM
search text string itik AIX 2 01-28-2008 07:01 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 08:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-16-2008
kesu2k kesu2k is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 10
Search a string and append text after the string

Hi,

I have a file like this...

<o t="Batch" id="8410" p="/" g="32">
<a n="name">
<v s="DBBA1MM"/>
</a>
<a n="owner">
<v r="/[AdminUser]Administrator"/>
</a>
<a n="rights">
<v s="95"/>

</a>
<a n="debugLevel">
<v s="3"/>

</a>
<a n="avsStoreLoc">
<v s="/home/kp1771/fw_base_path/collectors/DBBA1MM/avs"/>
</a>
</o>
<o t="Batch" id="8410" p="/" g="27">
<a n="name">
<v s="DBBA1MM"/>
</a>
<a n="owner">
<v r="/[AdminUser]Administrator"/>
</a>
<a n="rights">
<v s="95"/>
</a>

<a n="avsStoreLoc">
<v s="/home/kp1771/fw_base_path/collectors/DBBA1MM/avs"/>
</a>
</o>

I have to add

<a n="debugLevel">
<v s="3"/>
</a>


after

<a n="rights">
<v s="95"/>
</a>



if <a n="debugLevel">
<v s="3"/>
</a>
does not exits. I have to add the tag in red
If exists has to modify
<a n="debugLevel">
<v s="4"/>
</a>



can you help me on this..any type of help is appriciated thanks
  #2 (permalink)  
Old 07-16-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Have you tried anything so far? I would use awk to do this. When it finds <a n="rights"> you could read ahead a few lines using getline. If it contains "debugLevel" you could print out the new debug level, if not, just add debug level 3. For every other line, just print it.
  #3 (permalink)  
Old 07-17-2008
kesu2k kesu2k is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 10
Hi Annihilannic,
I used awk '/rights/,/avsStoreLoc/ { print }' a.xml
to extract the text between "rights" and "avsStoreLoc" ..but it didnt help me much
  #4 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Here, I've done most of the work for you, you should be able to finish it easily:

Code:
awk '
        /n="rights"/ {
                print
                getline ; print
                getline ; print
                getline
                if (match($0,"debugLevel")) {
                        print "<a n=\"debugLevel\">\n<v s=\"4\"/>\n</a>"
                        # consume and discard next two lines
                        getline
                        getline
                } else {
                        # do some stuff here
                        print
                }
                next
        }
        1 # print other lines
' inputfile > outputfile
You just need to fill in the "do some stuff here" part.
  #5 (permalink)  
Old 07-17-2008
kesu2k kesu2k is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 10
awesome..Thanks Annihilannic..It helped ..thanks
  #6 (permalink)  
Old 07-17-2008
kesu2k kesu2k is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 10
Hi Annihilannic,
The script works good..I have one more concern i should be able to print the changes in the same file where we are searching..how can I do.
  #7 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Code:
cp -p originalfile originalfile.bak && awk '<your script here>' originalfile.bak > originalfile
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0