215

I have a script file which I need to modify with another script to insert a text at the 8th line.

String to insert: Project_Name=sowstest, into a file called start.

I tried to use awk and sed, but my command is getting garbled.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
ashok
  • 2,161
  • 2
  • 13
  • 4

12 Answers12

328
sed -i '8i This is Line 8' FILE

inserts at line 8

This is Line 8

into file FILE

-i does the modification directly to file FILE, no output to stdout, as mentioned in the comments by glenn jackman.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • 3
    Yes, the -i switch is specific for GNU-sed. – user unknown Dec 21 '12 at 06:39
  • This works flawlessly. Thank you. I am wondering what switch shall I use if the same addition of a no is required not as in like an insert but an append? -i inserts at the specific line. How about appending? – DarkForce Dec 22 '14 at 13:42
  • 2
    No. -i means "modify the specified file in place". Insert vs. append is achieved with '8isomething' vs. '8asomething', independent from the -i-switch. – user unknown Dec 23 '14 at 01:28
  • 22
    mac users: with homebrew, `brew install gnu-sed` and then use this with `gsed` – cwd Jan 10 '15 at 17:58
  • 4
    This is super useful! Is there anyway for me to insert spaces at the beginning of the line? I noticed sed is not paying attention to initial whitespace... – elju Feb 01 '16 at 15:59
  • 11
    @elju: Yes, mask it with a backslash: `sed '8i\ 8 This is Line 8' FILE`. – user unknown Feb 02 '16 at 01:45
  • mac users(2): see [my answer below](http://stackoverflow.com/a/42192768/4694621) for a solution using the native `sed`. – Mateusz Piotrowski Feb 14 '17 at 13:21
  • Note that on my GNU `sed` (4.2.2), operating on multiple files will not display to `stdout` as expected, but the `-i` flag will work. (For example: `echo '1\n2\n4' > a; cp a b; sed '3iInsert' a b` will show the change to `a` but not `b`, whereas the `-i` flag *will* make the change to both files.) This was important to me for debugging before `-i`-ing my files. – nivk Dec 28 '18 at 22:40
  • @nivk: Well, that wasn't part of the question and this is not the place to answer all possible variations of a question, writing a compendium for sed. – user unknown Dec 29 '18 at 02:20
  • @userunknown definitely agree. Just thought it might help someone who saw the same thing I did. +1 from me. :) – nivk Dec 29 '18 at 02:27
  • I wonder why in the [documentation](https://jlk.fjfi.cvut.cz/arch/manpages/man/core/sed/sed.1.en) they mention only the form with a backslash (`i \ CR text`). I don't see any advantatges over specifying line to be added on the same line. – x-yuri May 02 '19 at 21:59
  • There is a problem: it keeps text at line 8! How to NOT keep text at line 8? It seems that I need "replace", not "insert". – pmor Dec 09 '22 at 13:11
  • 1
    @pmor: Yes, if you don't want to insert, but to replace line 8, you use the substitute command: `echo -e "line "{1..3}"\n" | sed '2s/.*/new 2nd line/'` (used line 2 instead of 8 for brevity). – user unknown Dec 11 '22 at 00:40
38

OS X / macOS / FreeBSD sed

The -i flag works differently on macOS sed than in GNU sed.

Here's the way to use it on macOS / OS X:

sed -i '' '8i\
8 This is Line 8' FILE

See man 1 sed for more info.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
36

An ed answer

ed file << END
8i
Project_Name=sowstest
.
w
q
END

. on its own line ends input mode; w writes; q quits. GNU ed has a wq command to save and quit, but old ed's don't.

Further reading: https://gnu.org/software/ed/manual/ed_manual.html

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
21

the awk answer

awk -v n=8 -v s="Project_Name=sowstest" 'NR == n {print s} {print}' file > file.new
AsukaMinato
  • 1,017
  • 12
  • 21
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • @glenn jackman I need to enter `#define SERVER@"http://10.35.42.54/ms0.8"` to a particular line. How can I achieve this? – Nevin Raj Victor May 20 '15 at 11:52
  • 1
    I think Nevin just needs to escape the quotes in his string with backslashes – Chris Koknat Sep 10 '15 at 23:49
  • @waLLe, start with the [awk info page](https://stackoverflow.com/tags/awk/info) which has a nice description of how awk works. Here, I have 2 "condition {action}" pairs, the 2nd has no condition which means the action is performed for every record. After you finish reading and you still have questions, let me know. – glenn jackman Nov 22 '18 at 18:11
  • @glennjackman almost there.. just didn't get this: file > file.new – w411 3 Nov 24 '18 at 03:47
  • "file" represents the name of the file that awk is working on. `>` is the shell [redirection](https://www.gnu.org/software/bash/manual/bash.html#Redirections) symbol so that awk's output is stored in the file named "file.new". – glenn jackman Nov 24 '18 at 04:04
  • @glennjackman. `NR ==n` map the line number, `{print s}` will print the s variable at line 8 . then `{print}` will print all the record in file then you use file.new as redirection. Is my interpretation correct? – jian Jan 10 '22 at 06:18
  • More or less. I'd say "`NR == n` **tests** the line number". Then, yes, print the variable when the current record number is 8. `{print}` prints every line unconditionally. For more information, click the [tag:awk] tag below the question, then click the "Learn more…" link – glenn jackman Jan 10 '22 at 14:02
18

POSIX sed (and for example OS X's sed, the sed below) require i to be followed by a backslash and a newline. Also at least OS X's sed does not include a newline after the inserted text:

$ seq 3|gsed '2i1.5'
1
1.5
2
3
$ seq 3|sed '2i1.5'
sed: 1: "2i1.5": command i expects \ followed by text
$ seq 3|sed $'2i\\\n1.5'
1
1.52
3
$ seq 3|sed $'2i\\\n1.5\n'
1
1.5
2
3

To replace a line, you can use the c (change) or s (substitute) commands with a numeric address:

$ seq 3|sed $'2c\\\n1.5\n'
1
1.5
3
$ seq 3|gsed '2c1.5'
1
1.5
3
$ seq 3|sed '2s/.*/1.5/'
1
1.5
3

Alternatives using awk:

$ seq 3|awk 'NR==2{print 1.5}1'
1
1.5
2
3
$ seq 3|awk '{print NR==2?1.5:$0}'
1
1.5
3

awk interprets backslashes in variables passed with -v but not in variables passed using ENVIRON:

$ seq 3|awk -v v='a\ba' '{print NR==2?v:$0}'
1
a
3
$ seq 3|v='a\ba' awk '{print NR==2?ENVIRON["v"]:$0}'
1
a\ba
3

Both ENVIRON and -v are defined by POSIX.

Lri
  • 26,768
  • 8
  • 84
  • 82
10

sed -e '8iProject_Name=sowstest' -i start using GNU sed

Sample run:

[root@node23 ~]# for ((i=1; i<=10; i++)); do echo "Line #$i"; done > a_file
[root@node23 ~]# cat a_file
Line #1
Line #2
Line #3
Line #4
Line #5
Line #6
Line #7
Line #8
Line #9
Line #10
[root@node23 ~]# sed -e '3ixxx inserted line xxx' -i a_file 
[root@node23 ~]# cat -An a_file 
     1  Line #1$
     2  Line #2$
     3  xxx inserted line xxx$
     4  Line #3$
     5  Line #4$
     6  Line #5$
     7  Line #6$
     8  Line #7$
     9  Line #8$
    10  Line #9$
    11  Line #10$
[root@node23 ~]# 
[root@node23 ~]# sed -e '5ixxx (inserted) "line" xxx' -i a_file
[root@node23 ~]# cat -n a_file 
     1  Line #1
     2  Line #2
     3  xxx inserted line xxx
     4  Line #3
     5  xxx (inserted) "line" xxx
     6  Line #4
     7  Line #5
     8  Line #6
     9  Line #7
    10  Line #8
    11  Line #9
    12  Line #10
[root@node23 ~]# 
jno
  • 997
  • 1
  • 10
  • 18
7

Perl solutions:

quick and dirty:

perl -lpe 'print "Project_Name=sowstest" if $. == 8' file

  • -l strips newlines and adds them back in, eliminating the need for "\n"
  • -p loops over the input file, printing every line
  • -e executes the code in single quotes

$. is the line number

equivalent to @glenn's awk solution, using named arguments:

perl -slpe 'print $s if $. == $n' -- -n=8 -s="Project_Name=sowstest" file

  • -s enables a rudimentary argument parser
  • -- prevents -n and -s from being parsed by the standard perl argument parser

positional command arguments:

perl -lpe 'BEGIN{$n=shift; $s=shift}; print $s if $. == $n' 8 "Project_Name=sowstest" file

environment variables:

setenv n 8 ; setenv s "Project_Name=sowstest"
echo $n ; echo $s
perl -slpe 'print $ENV{s} if $. == $ENV{n}' file

ENV is the hash which contains all environment variables

Getopt to parse arguments into hash %o:

perl -MGetopt::Std -lpe 'BEGIN{getopt("ns",\%o)}; print $o{s} if $. == $o{n}' -- -n 8 -s "Project_Name=sowstest" file

Getopt::Long and longer option names

perl -MGetopt::Long -lpe 'BEGIN{GetOptions(\%o,"line=i","string=s")}; print $o{string} if $. == $o{line}' -- --line 8 --string "Project_Name=sowstest" file

Getopt is the recommended standard-library solution.
This may be overkill for one-line perl scripts, but it can be done

Chris Koknat
  • 3,305
  • 2
  • 29
  • 30
  • 2
    Kudos to you Chris for taking the time to explain all that and lay it out so nicely, but wow this is why I don't like perl. – rsaw Mar 05 '16 at 00:22
5

For those who are on SunOS which is non-GNU, the following code will help:

sed '1i\^J
line to add' test.dat > tmp.dat
  • ^J is inserted with ^V+^J
  • Add the newline after '1i.
  • \ MUST be the last character of the line.
  • The second part of the command must be in a second line.
Nasri Najib
  • 1,261
  • 11
  • 6
1
sed -i "" -e $'4 a\\\n''Project_Name=sowstest' start
  • This line works fine in macOS
umläute
  • 28,885
  • 9
  • 68
  • 122
1

macOS sed solutions

for example: inserts at line 1

  1. ns
# recommended 
# This command only needs to write one line
$ sed -i '' '1s/^/The new First Line\n/' ./your-source-file-name
  1. ni
# not recommended 
# This way the command needs to be written on multiple lines
$ sed -i '' '1i\
The new First Line\
' ./your-source-file-name

test demo

$ sed -i '' '1s/^/Perl  camel\n/' ./multi-line-text.txt

enter image description here

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
0

it is working fine in linux to add in 2 lines.

sed '2s/$/ myalias/' file
buddemat
  • 4,552
  • 14
  • 29
  • 49
0

Thank you umläute

sed -i "" -e $'4 a\\\n''Project_Name=sowstest' filename

the following was usefull on macOS to be able to add a new line after the 4

In order to loop i created an array of folders, ti iterate on them in mac zsh

for foldercc in $foldernames; 

sed -i "" -e $'4 a\\\n''Project_Name=sowstest' $foldercc/filenames;
IAng
  • 1