anarchy
anarchy

Reputation: 5174

How to add a string to a specific line in a file using sed?

I have the following file test.txt

node1
node2
node3
node4

I want to add the string "1" next to it after a space next to the word node2 using sed so it looks like this.

node1
node2 1
node3
node4

I tried sed '/node2/a 1' test.txt but it adds a new line. How do I just add a space followed by a 1.

Upvotes: 0

Views: 41

Answers (1)

Barmar
Barmar

Reputation: 780688

a is for adding new lines. Use s to modify the line in place.

sed '/node2/s/$/ 1/' test.txt

Upvotes: 2

Related Questions