inserting data into a file at a specified place...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.