LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2008, 11:07 PM   #1
divya_flora
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Rep: Reputation: 0
inserting data into a file at a specified place...


Hi all,
I m new to shell programming..Can anyone please guide me how to insert data at a specified location in the file..
I have a configuration file..I want to add data to it through script..I am able to do it...I get that data written at end of my configuration file..I want data to be placed at a specified location...
As,I have a header [mydata] in my configuration file..I want to place the data here...
Is it possible to do that...
Example:
My configuration file:
[global]
hello
aaa
bbbb
[share]
gghg
hnnmm
bbb
[mydata]
gggg
[printing]
cvbvbb
vbvbb
Now,I want my data to be entered after header [mydata], not at the end of Configuration file...

Thanking you...
 
Old 10-24-2008, 12:21 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If the line you want to enter can be inserted just after the [mydata] header, you can use sed with the "a" command:
Code:
sed '/\[mydata\]/a\
> inserted line' junk.conf
[global]
inserted line
hello
aaa
inserted line
bbbb
[share]
inserted line
gghg
hnnmm
inserted line
bbb
[mydata]
inserted line
gggg
[printing]
inserted line
cvbvbb
vbvbb
You could enclose the sed command in double quotes and use a variable containing the line you want to insert.
 
Old 10-24-2008, 12:24 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
But you have to make sure your sed delimiter isn't a part of the data being inserted...
ta0kira
 
Old 10-24-2008, 03:32 AM   #4
divya_flora
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Hi,
Thanks so much..This has helped me to understand the concept...
Actually,This is my script which inserts the data into the configuration file..

#!/bin/bash
CF="/etc/samba/smb-test.conf" //Configuration file
.....................
...............

insert_mod() { //Function to insert data into config file
NCF="$CF.$$"
[ ! -f $CF ] && touch $CF || :
V="$1"
grep -v -E "^$V=" $CF > $NCF
mv -f $NCF $CF
eval echo "$V\"'\${$V}'\"">> $CF

}
hello=yes
insert_mod hello //Function called with argument
else
illegal
fi
...............

I have tried different alternatives using sed..But,not able to insert in the file under mydata..
Please assist me...
Thanks so much....
 
Old 10-24-2008, 04:49 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
jschiwal;
In your example, "inserted line" appears after every instance of "[...]"

Quote:
Originally Posted by divya_flora
I have tried different alternatives using sed..But,not able to insert in the file under mydata..
Did you actually try the solution jschiwal offered? It worked for me.
 
Old 10-24-2008, 05:55 AM   #6
divya_flora
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Hi,
It did worked..If we directly write the data in sed command...
but,if i want to take it as variables....
V=hello
i mean :sed '/\[mydata\]/a $V' file..
It will print $V..not the value of variable...
Here,m facing the problem....
Can you guide me on this ...
Thanking you...
 
Old 10-24-2008, 06:25 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
For this line by line processing, I would use awk. as below.

While line doesn't begin with [mydata], write the line.
Write the line that begins with [mydata].
Write a new line with your specific data.
Write all balance lines.

HTH.

End
 
Old 10-24-2008, 06:28 AM   #8
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
i mean :sed '/\[mydata\]/a $V' file..
It will print $V..not the value of variable...
Use weak rather than strong quoting i.e.

Code:
sed "/\[mydata\]/a $V" file..
This was mentioned here - please read your replies carefully..

Quote:
You could enclose the sed command in double quotes and use a variable containing the line you want to insert.
 
Old 10-28-2008, 02:30 AM   #9
divya_flora
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Hi,
Thanks i used the code in double quotes for variable..It worked...
Thanks....
 
  


Reply



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
inserting data into an existing file Magius363 Linux - Newbie 2 03-12-2008 07:10 PM
Perl: Inserting new data into the middle of a file R00ts Programming 5 03-07-2005 06:48 PM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
Inserting data into normalized databases. Travis86 Programming 2 11-19-2004 03:43 PM
inserting data in mysql zuessh Linux - Software 4 05-19-2003 01:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 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