[SOLVED] Inserting Text lines in a Text file that is existing via Bashscript
Linux - NewbieThis 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
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 Text lines in a Text file that is existing via Bashscript
Good day Team,
I am trying to create a script but I am stuck. The file already exists in multiple servers, and we just need to append two more lines in this file.
The below is the scenario details of this exercise.
Existing file at multiple linux machines,
Have to add Two lines in the file via bash Script
Line numbers are known, there’s no specific string to search for
And the two lines has to be in the same file in consecutive order.
Line 65 – TEXTABCD
Line 66 – TEXT DEFG
your specification is unclear (at least for me). Would be nice to see what did you try and what do you mean by "stuck".
Looks like an easy task for awk.
It might also be easy in sed with the i command. However, I agree, more information is needed to clarify what you need, which approach you have been trying, and how far you have gotten.
This file is a config file of a service. Let me give you an example.
# cat /etc/jay
Line1text
line2text
line3text
line4text
_____________________________________________________
That's current fine, I have to add two more lines in this same file and it should look like below:
To do this manually at each server, is one way of doing it. However, I want to create a bashscript which can do the job by just running it at each server.
I have tried to put my brain together with SED Command, but it can edit if a string is there, but cannot insert at a specific line. Unless if you know any other way.
May I cordially suggest that you use "a real programming language" to write this script. The #!shebang facility of the shell allows you to use any language you please.
The program that you write will need to do the following:
Lock the file to ensure that no one else is doing this at the same instant.
Read the contents of the file into a memory array.
Make changes to the array.
Write the contents of the array back out to the file.
Unlock the file.
"Now, pick a language ... any [real] language." PHP, Perl, Java, JavaScript, Ruby, Haskell, Common LISP ...
Can you please explain with example, how can I use those two commands as per the example mentioned above?
4iline_num_4\nline_num_5 is that a specific parameter, part of SED family?
4 is an address, means: look for line number 4 i is a command (of sed): insert line_num_4 is the text inserted as the new line (number 4) - can be replaced by your own text \n is a newline line_num_5 is the text inserted as the next new line (number 5)
Quote:
Originally Posted by techjaymindave
Also, is that going to generate a new output file?
the usual syntax is:
Code:
sed 'script' inputfile > outputfile
Quote:
Originally Posted by techjaymindave
awk ' NR==4 { print "line_num_4" } 1'
Where do I mention the text which I want to inject into the text file if I am using awk command?
in between " "
Quote:
Originally Posted by techjaymindave
If I am not mistaken, you are giving two different ways to achieve the required.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.