LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-17-2011, 10:00 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Question how to update a file with some configuration changes.


Hi Guys,

I have a challenege infront of me.

There is a configuration file which is generated post deployment say orion-web.xml Post deployment I have to append the file with contents,
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
between one node <webapp>
</webapp>
so post deployment it should look like,
<web-app>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</web-app>

how can we achieve this? ...

any pointers I have no clue if at all i can acheiev this.

This case would have been different if I had to change the value which would have been possible with my small little knowledge of sed.


Please assist.

many thanks.
 
Old 06-17-2011, 10:18 AM   #2
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Rep: Reputation: 47
hm, how about this:
echo '<web-app>' >output.txt
more orion-web.xml >>output.txt
echo '</web-app>' >>output.txt


er, if you need to do it in several places, this won't work. in that case, I would do search&replace;
search for: <session-config>
replace with: <web-app> <session-config>

Last edited by qrange; 06-17-2011 at 10:22 AM.
 
Old 06-17-2011, 10:29 AM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Nope this won't work straight away.

what you are doing is adding webapp then orion-web.xml file (which is a complete file with header and footer) and then </webapp>
also where are appending we appending the nodes session config and session timeout?

so yeah we need some other mechanism, first to parse file and grep for <webapp> then add those two node values ... something like this...
 
Old 06-17-2011, 10:39 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
You can insert a file with sed:
Code:
$ sed "/<web-app>/r content" orion-web.xml 
fubar
fubar
<web-app>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</web-app>
fubar
fubar
where content contains the three additional lines.
 
Old 06-17-2011, 11:11 AM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
cooking

Getting there

sed -i "/<web-app>/r <session-config><session-timeout>-1s</session-timeout></session-config>" orion.web.xml

I have inserted a defensive check point $? just after execution of this command it does change time stamp but content of the file is not changed....

Please assist.
 
Old 06-17-2011, 11:18 AM   #6
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
You can think of this perl onliner.

perl -p -i -e 's/<session-config>/<web-app> <session-config>/g' orion-web.xml
 
Old 06-17-2011, 11:20 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
The r command in sed needs a filename. content in my example was a file with the to be inserted lines. There are other sed commands like a which you can use instead when you want to do it in one line:
Code:
$ sed -i "/<web-app>/a <session-config>\n<session-timeout>-1s</session-timeout>\n</session-config>" orion.web.xml
Interesting that it doesn’t throw an error about the missing file in your case.

(Please use CODE tags, it makes it more readable.)

Last edited by Reuti; 06-17-2011 at 11:23 AM. Reason: Rephrased
 
Old 06-18-2011, 09:41 AM   #8
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Unhappy

Thanks for that Perl code, at this stage I have no idea about how to use your eprl script as If i try to execute just the way gave it doesnot do anything

Reuti
Cooking


Quote:
echo "<session-config>\n" > /tmp/somefile
echo "<session-timeout>-1s</session-timeout>\n" >> /tmp/somefile
echo "</session-config>" >> /tmp/somefile

sed "/<web-app>/r /tmp/somefile " orion-web.xml
Interesting is that file is created!

Now atleast this should work right?

I even tried with i but thats still didnt work.
Quote:
sed -i "/<web-app>/r /tmp/somefile " orion-web.xml
Hushhhhh

Last edited by sysmicuser; 06-18-2011 at 09:49 AM.
 
Old 06-18-2011, 10:54 AM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Yep, both versions are correct. Which version of sed are you using on which platform?
Code:
$ sed --version
 
Old 06-18-2011, 06:51 PM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Code:
$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
$ uname -a
Linux tmelbld01 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux
 
Old 06-19-2011, 10:59 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
If you want to append this block of text
Quote:
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
between one node <webapp>
</webapp>
so post deployment it should look like,
<web-app>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</web-app>
to file orion-web.xml, I'd just put it in a file eg append.txt, then it's
Code:
cat append.txt >> orion-web.xml
 
Old 06-20-2011, 05:46 AM   #12
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
@chrism01: he wants to insert some text in the middle of the file, not at the end.

@sysmicuser: I have no clue why it’s not working for you. I have the same version of sed and it’s working with the a and r commands as documented.
 
Old 06-20-2011, 07:05 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
@Reuti : re Post #1
Quote:
Post deployment I have to append
 
Old 06-21-2011, 02:34 AM   #14
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Well - english is not my native language, but when he describes his effort that he wants to put stuff between <web-app> and </web-app> I would describe it with to “insert” something into the file between these tags (and he used “append” by accident). “append” in computer terms is strictly at the end of a file, although the usage in common speech might be different. So, the goal is to transform somewhere in the middle of a file:
Code:
<webapp>
</webapp>
to become:
Code:
<web-app>
<session-config>
<session-timeout>-1s</session-timeout>
</session-config>
</web-app>
I can issue my suggested sed command with a testfile and it works as advertised.

Was the original file created on a Windows platform and has wrong line breaks, i.e. 0x0D 0x0A instead of a plain 0x0A?
 
1 members found this post helpful.
Old 06-21-2011, 09:03 AM   #15
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
My apologies Guys the wording was incorrct I should not said "append".

Reuti your understanding of problem is correct.
chrism01 that operation would pump out text at the end of file, I want it to be in between<webapp> and </webapp>

Reuti, no the file is created on Unix box so we can rule of windows possibility any idea how to check with od command?

I beleiev od -ox filename would be good point to check?

Let us keep the discussion going.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not add update server into my configuration vitalstrike82 SUSE / openSUSE 4 03-24-2008 08:42 AM
DNS configuration and configuration file in RHEL5 remo1225 Linux - Newbie 1 02-20-2008 04:25 AM
Online update configuration John Boyle Linux - Software 3 08-29-2006 02:58 PM
Yum Update complains of missing file to do update, but file exists! davidas Linux - Newbie 0 03-28-2004 11:14 AM
kernel update; what about the old configuration b0uncer Linux From Scratch 4 01-06-2004 05:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration