find and replace! Urgent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace! Urgent
# 1  
Old 03-06-2009
find and replace! Urgent

I am trying to find and replace the following line in log4j.xml:

<param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>

to

<param name="ConversionPattern" value="[%d{ABSOLUTE},%p,%c{1}] %m%n"/>

The problem here is that when I find the first string above using sed or grep, it returns more than one line that matches my string in the file log4j.xml. However, I only want to replace one line that matches my criteria. For example: The log file has the following tags:

<appender name="PAYMENTLOG" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.home.dir}/log/payment.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value=".yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
</layout>
</appender>




<appender name="EMAIL" class="org.apache.log4j.net.SMTPAppender">
<param name="Threshold" value="ERROR"/>
<param name="From" value="jboss@dummy.com"/>
<param name="Subject" value="${hostname} - PRODUCTION ERROR: MIDDLE TIER"/>
<param name="BufferSize" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
</layout>
</appender>


Notice the strings in bold? I ONLY want the string to be replaced under the tag "PAYMENTLOG" but not the other. Please help me how to achieve this.
# 2  
Old 03-06-2009
I suppose its the first occurence of the file and hence...

Code:
sed 's/SEARCH_STRING/REPLACE_STRING/1'  infile >> New_file

# 3  
Old 03-06-2009
Code:
awk '/"PAYMENTLOG"/{++f}f==1 && /%d\{ABSOLUTE\}/{sub(/%c/,"%p,&"); print; f++; next}1' file


--
If you need to apply the code to all occurrences of "PAYMENTLOG" tags use:

Code:
awk '/"PAYMENTLOG"/{++f}f && /%d\{ABSOLUTE\}/{sub(/%c/,"%p,&"); print; f=0; next}1' file


Last edited by rubin; 03-06-2009 at 01:36 PM.. Reason: added last code
# 4  
Old 03-06-2009
Thanks guys!. I tried the following

sed -i 's/<param\ name=\"ConversionPattern\"\ value=\"\[%d{ABSOLUTE},%c{1}\]\ %m%n\"\/>/<param\ name=\"ConversionPattern\"\ value=\"\[%d{ABSOLUTE},%p,%c{1}\]\ %m%n\"\/>/1' log4j.xml

I expected it to change the first occurance but rather it replaced all the 3 ocurrances. Did I do it in a wrong way?
# 5  
Old 03-06-2009
What I meant is that I tried the following :

sed -i 's/STRING1/STRING2/1' file_name


I expected it to replace the first occurrence of string 1 but it rather replaced all occurances. Please help!
# 6  
Old 03-06-2009
You can use this format

sed -i '0,/STRING1/s//STRING2/' inputfile

That is the number 0 not the letter O.
# 7  
Old 03-06-2009
Thanks Bubbajoe. I tried it but it failed with following error

"sed: -e expression #1, char 2: invalid usage of line address 0"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Urgent: please replace the perl script in the post with following code

Hi This is very urgent as it is confidential info posted by mistake.Kindly help us asap. post link below: https://www.unix.com/shell-programming-and-scripting/201037-perl-while-loop-each.html please replace the perl script with following script in the post ... (1 Reply)
Discussion started by: ptappeta
1 Replies

2. Shell Programming and Scripting

Problem with Find command urgent help!!

hi I used find command to find some file names as per input from user. I used it for current directory. It was working fine. Now I tried with giving some other directory path. Its giving issues. Here what I tried. Script will take input from user say 1_abc.txt, find the file and print list. if... (1 Reply)
Discussion started by: sukhdip
1 Replies

3. Shell Programming and Scripting

replace a string in perl - Urgent

Hi, Can anyone help me to replace ='is NOT NULL' to is NOT NULL in perl. I tried all the methods which i know, i didnt arrive at the solution. please help!! ---------- Post updated at 03:01 PM ---------- Previous update was at 02:56 PM ---------- I got it.......! Thanks, (1 Reply)
Discussion started by: jam_prasanna
1 Replies

4. Shell Programming and Scripting

Urgent help needed !!!....to replace a exact string

Hi experts, As i am a novice unix player...so need help for the below query...banged my head from quite a while...:confused: i have a set of html files, in which i need to search for string "Page"(case sensitive) and then replace the same with some numeric code ,say, "XXX1234". Here in... (1 Reply)
Discussion started by: rahulfhp
1 Replies

5. UNIX for Dummies Questions & Answers

very urgent..need of a script which finds a file without the use of find command..hlp

im a beginner in shell scripting and i need a script which will find a file in a given path without the use of find or grep command.......i need some kind of code.....plzzz plzzzz help me......ive tried n searched every where but i couldn't find the solution for my particular problem..... (4 Replies)
Discussion started by: mishi
4 Replies

6. Shell Programming and Scripting

Urgent: find some strings in all the tables

Hi ,, I want to write a shell script to search following strings values in columns of all the tables in the database: BO_OPSWAP,BOOPSWAP FO_OPSWAP,FOOPSWAP, OPSWP not swapwire please help on this.. it's quite urgent....... i 'm working on UNIX AIX servers....and using sqlplus... (1 Reply)
Discussion started by: unknown123
1 Replies

7. UNIX for Advanced & Expert Users

urgent: problem with find command

find / * -print > /var/tmp/updatedfilelist.txt 2>&1 i need to run the above file in cron. problem is, this process takes a long long long time to finish up. and even worse, it fills up the directory the output file is located in. i dont want the /var directory to ge filled up because of... (10 Replies)
Discussion started by: Terrible
10 Replies

8. HP-UX

URGENT: UNIX FTP cannot find path error

Dear all We are currently working on to install some ERP system in wjhich we need to FTP from unix to windows 2000 machine We run ./lodrun to get files from d/xxxxx/xxxx directory but the ftpoutput.log file shows following error FTP: xxxxxxxxx system cannot find the path specified... (1 Reply)
Discussion started by: minix
1 Replies

9. UNIX for Dummies Questions & Answers

find command - Urgent Plz.

Hi, In my current directory, i have the following files: x1.dat x2.dat.gz x3.dat I want to use the find command and display only files which has *.dat and NOT *.gz extension files. Please help me out. Thanks, Kris Kart. (2 Replies)
Discussion started by: Kris_Kart_101
2 Replies

10. UNIX for Advanced & Expert Users

help on find with chmod.. urgent help needed

Hello to all Unix gurus.. I am writing a generic script which takes the options of unix command as input and concatenate all the pieces and forms a complete executable command. I am getting an error with the following command as I am resetting my own permission on the root directory. When the... (4 Replies)
Discussion started by: sdlayeeq
4 Replies
Login or Register to Ask a Question