Add Text to File at Certain Line in Linux

Add text to file at certain line in Linux

You can use sed to solve this:

sed "15i avatar" Makefile.txt

or use the -i option to save the changes made to the file.

sed -i "15i avatar" Makefile.txt

To change all the files beginning that start Makefile:

sed "15i avatar" Makefile*

Note: In the above 15 is your line of interest to place the text.

How can I insert a text file at specific line of another

will be simpler with sed

$ sed '/insert the text file/r methods_to_import.txt' my_api.rb

appending text to specific line in file bash

It can be done using awk:

awk -F, 'NF==16{$0 = $0 FS "xx"} 1' file
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,xx
b,b,b,b,b,b
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a
b,b,b,b,b,b
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,xx
  • -F, sets input field separator as comma
  • NF==16 is the condition that says execute block inside { and } if # of fields is 16
  • $0 = $0 FS "xx" appends xx at end of line
  • 1 is the default awk action that means print the output

How to add text at the beginning of specific lines using sed?

Whenever @PL is found, read next line and prepend PREFIX to it.

sed '/@PL/{n;s/^/PREFIX/}' file

Add string to file at certain line number

Older seds are a bit pickier with how you type commands like i, a and c. Try an actual line continuation:

sed '5i\
helloworld' test.txt

The i text syntax is a GNU extension. POSIX sed only know about the i\ version with linebreak.

Also, notice that there is a difference between the sed i command1 (insert text) and the -i option (in-place editing).


1 Or "function".

Modifying a particular line in a text file from the command-line

A simple sed substitute will also work given the line number, e.g.

sed -i '2s/^/# /' file

The above will edit file in place commenting line 2 inserting "# " at the beginning of the line. To pass in the line number as an argument, double-quote the expression, e.g.

#!/bin/bash

sed -i "$1s/^/# /" "$2"

Will take the line number as the first argument and the file to comment as the second and edit the file in-place making the comment. You should validate the first argument is an integer and that the second is a file that exists and is non-empty. If you save the script as cmt and make it executable (e.g. chmod +x cmt), then you would comment the 2nd line in file as:

./cmt 2 file

How to append text to a specific lines in a file using shell script?

$ sed '/3696/ s/$/running/' file.txt 
foo1 3464
foo2 3696 running
foo3 4562

or

$ sed 's/3696/& running/' file.txt 
foo1 3464
foo2 3696 running
foo3 4562

Add the -i option to save the changes back to file.txt.

How to add a string to line 13 in my text file

Normally, sed only writes out the changes. It does not modify the file.

If you want the input file to be modified, you can use GNU sed -i:

sed -i '14 a <3 196>' file.data

Before:

[...]
9
10
11
1 15.9994
2 24.305

Atoms
16
17
[...]

After:

[...]
9
10
11
1 15.9994
2 24.305

<3 196>
Atoms
16
17
[...]

Note: If you want it after line 13 instead of 14, change 14 to 13 in your code. Similarly, if you wanted 3 196 instead of <3 196>, change <3 196> to 3 196 in your code.

How to append a string at end of a specific line in a file in bash

Using sed and the pattern described:

sed '/192.168.1.2/s/$/ myalias/' file

Using sed and a specific line number:

sed '2s/$/ myalias/' file


Related Topics



Leave a reply



Submit