LinuxQuestions.org
Visit Jeremy's Blog.
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 03-13-2017, 05:50 AM   #1
techjaymindave
LQ Newbie
 
Registered: Mar 2017
Location: South Africa
Distribution: Centos
Posts: 10

Rep: Reputation: Disabled
Lightbulb 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
 
Old 03-13-2017, 05:55 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,114

Rep: Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139
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.
 
Old 03-13-2017, 06:08 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,097
Blog Entries: 3

Rep: Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666
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.
 
Old 03-13-2017, 06:11 AM   #4
techjaymindave
LQ Newbie
 
Registered: Mar 2017
Location: South Africa
Distribution: Centos
Posts: 10

Original Poster
Rep: Reputation: Disabled
Good day Pan64,

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:

# cat /etc/jay

Line1text
line2text
line3text
line4text
Newtextline5
Newtextline6
______________________________________________________

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.

Thanks and regards,
Jay!
 
Old 03-13-2017, 06:14 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,097
Blog Entries: 3

Rep: Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666
If you want the 4th and 5th lines pushed down and 'one' and 'two' inserted, the i command in sed would be used like this:

Code:
sed -e '4ione' -e '4itwo';
This cannot append new lines though.

Last edited by Turbocapitalist; 03-13-2017 at 06:15 AM.
 
Old 03-13-2017, 06:24 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,114

Rep: Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139
Code:
sed '4iline_num_4\nline_num_5' inputfile > outfile
- or
awk ' NR==4 { print "line_num_4" } 1'
worked for me too.
 
Old 03-13-2017, 06:45 AM   #7
techjaymindave
LQ Newbie
 
Registered: Mar 2017
Location: South Africa
Distribution: Centos
Posts: 10

Original Poster
Rep: Reputation: Disabled
Good day pan64,

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?

Also, is that going to generate a new output file?

_________________________________

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?

_________________________________

If I am not mistaken, you are giving two different ways to achieve the required.
 
Old 03-13-2017, 07:40 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,097
Blog Entries: 3

Rep: Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666Reputation: 3666
A useful, generic way is to use redirection of both input and output

Code:
sed 'somesedstuff' < input.file.txt > output.file.txt

# or

awk 'someawkstuff' < input.file.txt > output.file.txt
 
Old 03-13-2017, 10:11 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,424
Blog Entries: 4

Rep: Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838
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:
  1. Lock the file to ensure that no one else is doing this at the same instant.
  2. Read the contents of the file into a memory array.
  3. Make changes to the array.
  4. Write the contents of the array back out to the file.
  5. Unlock the file.

"Now, pick a language ... any [real] language." PHP, Perl, Java, JavaScript, Ruby, Haskell, Common LISP ...
 
Old 03-13-2017, 07:10 PM   #10
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,274

Rep: Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707Reputation: 2707
If the absolute line numbers are known, then you can use a combination of head, echo and tail in conjunction with a second file.
Code:
#!/bin/bash

myfile="/file/to/update"

cp "$myfile" "$myfile.orig"

head -n 6 "$myfile.orig" > "$myfile"
echo "New line 1 here" >> "$myfile"
echo "New line 2 here" >> "$myfile"
tail -n +7 "$myfile.orig" >> "$myfile"
 
Old 03-13-2017, 07:48 PM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,019

Rep: Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102Reputation: 4102
Ugh. sed can handle it all.
It can insert before (as above) or after a given line. It is also aware of the last line of a file (hint).
 
Old 03-14-2017, 02:45 AM   #12
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Using the line 'line4text' as point of reference, try this:

Code:
sed -i /^line4text/a\Newtextline5\nNewtextline6' /etc/jay
 
Old 03-14-2017, 02:45 AM   #13
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Using the line 'line4text' as point of reference, try this:

Code:
sed -i /^line4text/a\Newtextline5\nNewtextline6' /etc/jay
 
Old 03-14-2017, 03:02 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,114

Rep: Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139Reputation: 7139
Quote:
Originally Posted by techjaymindave View Post
Good day pan64,

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 View Post
Also, is that going to generate a new output file?
the usual syntax is:
Code:
sed 'script' inputfile > outputfile
Quote:
Originally Posted by techjaymindave View Post
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 View Post
If I am not mistaken, you are giving two different ways to achieve the required.
yes, one with sed, the second with awk
 
Old 03-14-2017, 09:24 AM   #15
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan with some Tiny Core, Fatdog, Haiku, & BSD thrown in.
Posts: 5,288

Rep: Reputation: Disabled
Seems to me that just using the append (>>) from the command line would do it.
Code:
echo "textABC" >> nameofserverfile
echo "textDEF" >> nameofserverfile
 
  


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
Inserting a block of text into a text file on system boot krptodr Linux - Newbie 5 02-14-2012 08:11 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11:59 AM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 11:40 AM
Inserting blank lines in text file David the H. Linux - General 5 11-24-2007 04:34 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 11:55 AM

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

All times are GMT -5. The time now is 11:55 PM.

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