sud03r
sud03r

Reputation: 19759

in place editing using awk

I want to add a line at top of file say f1 using awk.
Is there a better way than the following?

awk 'BEGIN{print "word"};{print $0}' f1 > aux;cp aux f1;\rm aux<br/>

Does awk has something like -i option in sed?

Upvotes: 4

Views: 2771

Answers (2)

sud03r
sud03r

Reputation: 19759

An alternate way to do this is:

sed -i '1s:^: Word1\nWord2 :' file

Upvotes: 1

Beano
Beano

Reputation: 7831

Why not use sed - it would make the solution more straightforward

$sed -i.bak '1i\
word
' <filename>

Upvotes: 2

Related Questions