Samanosuke Akechi
Samanosuke Akechi

Reputation: 361

How to insert word in nth line of a file

Is there a way to add a string in a specific line in a file and if something else is there replace it?

eg

command "HELLO" nth_line file

Upvotes: 1

Views: 1397

Answers (2)

user2314737
user2314737

Reputation: 29437

Substitute "HELLO" in filename at line 2

awk 'NR==2{gsub(/.*/, "HELLO")}1' filename

Upvotes: 1

cnicutar
cnicutar

Reputation: 182794

Sure:

sed '42s/.*/something else/'

However this won't work if the file fewer than 42 lines.

Upvotes: 1

Related Questions