Lei Zhang
Lei Zhang

Reputation: 103

Replace line based on line number

I have not got any standard answer on how to use sed to replace a specific line with the line number.

May I know how to do that?

For example, I tried to replace line 45 using

sed -i '45s/*/T T F/' POSCAR

Not working...

Upvotes: 0

Views: 81

Answers (2)

Sobrique
Sobrique

Reputation: 53478

You can use perl for this job too.

perl -i '$. == 45 && $_ = "T T F\n"' POSCAR

(Assuming you want to replace all of line 45)

Upvotes: 0

Cyrus
Cyrus

Reputation: 88626

Add a dot:

Replace

sed -i '45s/*/T T F/' POSCAR

by

sed -i '45s/.*/T T F/' POSCAR

.: match any character

*: the preceding expression can match zero or more times

Upvotes: 2

Related Questions