1

I have a file that looks like this:

myFile

Hello     World
Hello     World
Hello     World
Hello
Hello
Hello     World
Hello     World

What I want to do is a find and replace of any line in the file that has just the word "Hello" with the string "Hello World", keeping the spacing the same as what's already in the file (let's say 10 characters width from the H to the W).

What I have so far:

perl -pi -e 's/Hello/Hello World/' myFile

I know I didn't get far but I have been Googling this for quite some time and can't figure out what I need to do..

Any help is greatly appreciated!! Thanks!!

Rebulah
  • 11
  • 1

2 Answers2

3
perl -i -pe '$t=$1 and next if /Hello(\s+)World/; s/Hello\K/${t}World/' myFile
mpapec
  • 48,918
  • 8
  • 61
  • 112
1

You have to specify that the string ends after the "Hello":

perl -pi -e 's/Hello$/Hello          World/' myFile
choroba
  • 200,498
  • 20
  • 180
  • 248