views:

77

answers:

3

Hi,

I have over 1300 .txt files where I need to edit the first line of text, replacing one name for another. Can someone please advise of the best way to achieve this?

Any advice would be appreciated.

Thanks

Stu

A: 

If this is Linux, then sed is the answer.

joefis
Thanks I am on Windows, not Linux.
Stuart
A: 

Use sed. Here's a simple one-liner that would do what you want:

sed -i '1s/oldtext/newtext/' *.txt

The -i tells sed to edit the files in-place. The 1 at the beginning of the pattern applies it only to the first line. The s// constrution replaces the text.

scompt.com
Thanks for the reply.
Stuart
Keep in mind that sed is available on windows through Cygwin: http://www.cygwin.com/
scompt.com
A: 
perl -npi~ -e "s/old/new/g" file.txt

If you're on a Windows machine, install Strawberry Perl.

Robert Wohlfarth
I am downloading Strawberry Perl, so I'll give it try. Thank you for your reply!!!
Stuart