Search Parameter in first line and replace next line content


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Search Parameter in first line and replace next line content
# 1  
Old 03-23-2010
Java Search Parameter in first line and replace next line content

Hi,

I need help. I have XML file as below
Code:
    <a n="infoLevel">
        <v s="true"/>
    </a>
    <a n="localAddr">
        <v s="server.host.com"/>
    </a>
    <a n="ListenPort">
        <v s="21111"/>
    </a>

I need to find variable "ListenPort" in line and then replace next line's 21111 to 98765.

How do I do it using sed or awk or perl?

Thanking you in advance,

Last edited by zaxxon; 03-23-2010 at 03:47 AM.. Reason: use code tags please, ty
# 2  
Old 03-23-2010
With gnu sed:

Code:
sed '/ListenPort/ {n; s/21111/98765/}' infile

Other seds might want to have each part on a separate line like:
Code:
sed '/ListenPort/{
   n
   s/21111/98765/
}' infile

# 3  
Old 03-23-2010
Using awk,

Code:
awk 'f==1 {gsub(/21111/,"98765");f=0; } /ListenPort/ {f++}1' < file >outfile

# 4  
Old 03-23-2010
Thanks dennis.jacob and zaxxon for your time to help me. I am newbie in awk/sed/perl one liner world so unable to understand error on your solution.

I tried SED command as suggested but getting below error.

server:/home> sed '/ListenPort/{ n; s/21111/98765/ }' /tmp/xp.xml
sed: command garbled: /ListenPort/{ n s/21111/98765/ }

I tried AWK command as suggested but getting below error.

server:/home> awk 'f==1 {gsub(/21111/,"98765");f=0; } /ListenPort/ {f++}1' < /tmp/xp.xml > /tmp/xp1.xml
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

---------- Post updated at 12:04 PM ---------- Previous update was at 09:40 AM ----------

I need help. I have XML file as below

Code:

&lt;a n=&quot;infoLevel&quot;&gt;
&lt;v s=&quot;true&quot;/&gt;
&lt;/a&gt;
&lt;a n=&quot;localAddr&quot;&gt;
&lt;v s=&quot;server.host.com&quot;/&gt;
&lt;/a&gt;
&lt;a n=&quot;ListenPort&quot;&gt;
&lt;v s=&quot;21111&quot;/&gt;
&lt;/a&gt;


I need to find variable "ListenPort" in line and then replace next line's 21111 to 98765.

And, this problem is not to solve only one string "21111" but it can be any number between "0-99999". It means displayed problem has 21111 but it could be 1 or 23 or 456 or 7890 or 98711. The good part is starting position of first number is always fixed in that line. It means number always start from position 17 and has surrounded by "&quot;" string.

Please help me to solve this problem using sed/awk/perl.

Thanks in advance,
# 5  
Old 03-24-2010
I posted you another version of the sed where every subcommand is in a separate line since older versions of sed do not support writing everything in the same line without ^J for example.

So take the sed script that is more than one line into a script and try it again.

Also I asked you to use code tags - the PM you got explains it very well - you got another one.

dennis' awk might need another version of awk so you might check if you have gawk or if you are on Solaris check for nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with replace comma with new line and repeat the content

Input file G A G A,T A C A G,T,C T C . . Desired Output file G A G A G T A C A G A T A C T C (4 Replies)
Discussion started by: perl_beginner
4 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

search for a string ,replace the whole line with new line

hai i am very new to unix. i am having two files like this. first.properties cache.ZA.TL_CCY=SELECT trim(CCY_CODE)||trim(COUNTRY_CODE)||trim(CITY_CODE) AS... (4 Replies)
Discussion started by: kkraja
4 Replies

8. Shell Programming and Scripting

How to replace a line content

Hi Experts, I have binary files contain an ID Line as: : : $Header: FNDSCMON.fmb 115.6 2000/01/11 10:26:10 pkm ship$ : : where the ID Line format is: $Header: <File_Name> <Version> <Last_update_date_time> pkm ship$ In this Example: File_Name = FNDSCMON.fmb Version = 115.6... (13 Replies)
Discussion started by: victorcheung
13 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

search and replace the whole line

Hi, I have this line in a file called test.cfg SOURCEFILE=/usr/platform/sun4v/driver/file.cfg But i have many occurances of "SOURCEFILE" in test.cfg , i need to search only for this line and replace that line with SOURCEFILE=/usr/platform/sun4x/driver/file.cfg again there are many... (17 Replies)
Discussion started by: Jartan
17 Replies
Login or Register to Ask a Question