How do I add a line of text to the middle of a file using bash?

Asked
Active3 hr before
Viewed126 times

7 Answers

middleusing
90%

If the first line in pattern space is blank we replace it with nothing.,Now we just need to print the results:, 4 Oh, yes, you are right! The command sed -n 'H;${x;s/^\n//;s/nameserver .*\n/nameserver 127.0.0.1\n&/;p;}' resolv.conf, however, solves this problem. I added s/^\n// to the command so it will remove the newline at the beginning of the content of pattern space. – brandizzi Jul 18 '11 at 21:17 ,$f is used as a flag to indicate that the nameserver string has already been found

Here is a solution using sed:

$ sed - n 'H;${x;s/^\n//;s/nameserver .*$/nameserver 127.0.0.1\n&/;p;}'
resolv.conf

# Generated by NetworkManager
domain dhcp.example.com
search dhcp.example.com
nameserver 127.0 .0 .1
nameserver 10.0 .0 .1
nameserver 10.0 .0 .2
nameserver 10.0 .0 .3

How it works: first, suppress the output of sed with the -n flag. Then, for each line, we append the line to the hold space, separating them with newlines:

H

When we come to the end of the file (addressed by $) we move the content of the hold space to the pattern space:

x

If the first line in pattern space is blank we replace it with nothing.

s / ^ \n //

Then we replace the first line starting with nameserver by a line containing nameserver 127.0.0.1, a new line (Your version of sed may not support \n, in which case replace the n with a literal newline) and the original line (represented by &):

s / nameserver.*$ / nameserver 127.0 .0 .1\ n & /

Now we just need to print the results:

p
load more v
88%

sed '6i\
This is the new line
' file
load more v
72%

sed '/\[option\]/i Hello World' input,Insert line before match,sed '/\[option\]/a Hello World' input,Additionally you can take backup and edit input file in-place using -i.bkp option to sed

Yes, it is possible with sed:

sed '/pattern/a some text here'
filename

An example:

$ cat test
foo
bar
option
baz
$ sed '/option/a insert text here'
test
foo
bar
option
insert text here
baz
$
load more v
65%

Details of the `sed` command:,sed: is the command itself. 2: line number where new line will be inserted. i: parameter, which told sed to insert line. tecadmin 2: text to be added. file1.txt: is the file in which new line need to add. file1.txt.tmp: New file with updated content.,Now you required to add text “tecadmin 3” at line number 3, use below command,If you want to add a line in file at specific line number through script. Below examples will help you to do it.

For example you have a file named file1.txt, file content are as below.

[email protected]: ~# cat file1.txt
tecadmin 1
tecadmin 2
tecadmin 4
load more v
75%

You can do it using sed insert with bash command substitiution,this way text from inside inputFile.txt will be inserted in line preceeding </xa-datasource>,If you want it to be inserted before the given string but in the same line, you can use sed substitution instead of insert:,Connect and share knowledge within a single location that is structured and easy to search.

You can do it using sed insert with bash command substitiution

sed "/<\/xa-datasource>/i $(<inputFile.txt)"
file1.txt

If you want it to be inserted before the given string but in the same line, you can use sed substitution instead of insert:

sed "s/<\ /xa-datasource>/ $(<inputFile.txt)<\ /xa-datasource>/" file1.txt
load more v
40%

You can do those two things using command line options.,Free script: Shellshock vulnerability setector,So change the script to use $1 for the positional variable, and run it again:,[ You might also like: More stupid Bash tricks: Variables, find, file descriptors, and remote operations ]

Starting with a simple script that displays an entered name on the screen. Create a file called script1.sh with the following content and make it executable.

#!/bin/bash

echo $0
load more v
22%

Example 10-1. Inserting a blank line between paragraphs in a text file,A Bash script may invoke the string manipulation facilities of awk as an alternative to using its built-in operations.,For more on string manipulation in scripts, refer to Section 10.2 and the relevant section of the expr command listing.,A simple emulation of getopt using substring-extraction constructs.

stringZ = abcABC123ABCabc

echo $ {
   #stringZ
}
# 15
echo `expr length $stringZ`
# 15
echo `expr "$stringZ" : '.*'`
# 15
load more v

Other "middle-using" queries related to "How do I add a line of text to the middle of a file using bash?"