Skip to content
Advertisement

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

I have a very large text file that is difficult to open in text editors.

Lines 12 – 15 are:

1 15.9994
2 24.305

Atoms

I would like to add:

3 196 to line 14 and then have a blank line between 3 196 and Atoms like it is currently. I tried:

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

But it did not seem to change anything. Anyone know of how I can do this?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement