I came across a post today in which someone mentioned that they used Perl for text replacement and, despite the simplicity of the solution, I hadn't ever thought of it. By simply using three options with the perl command, you can run a quick script to do regex replacement in a file. The -p option tells Perl to treat your script as a loop, reading in from the diamond (<>) operator. -i makes it do in-place editing of files and -e allows you to script from the command line. For example:
~$ perl -p -i -e "s/foo/bar/g" baz.txt
The above command will replace 'foo' with 'bar' throughout the file 'baz.txt'


hi i'm trinig to add one word in the end of specific line ( line X ) do you know how can i run this command form a perl script :
sed -i "3s/$/text/" file.txt ( the line here is : 3)
i tried your command : perl -p -i -e "3s/$/text/" file.txt
but i got error : syntax error at -e line 1, near "3s/$/text/" Execution of -e aborted due to compilation errors.
without the number : "3" , it is working
I don't believe that what you intend to do with Perl is possible in that fashion; Perl's s/// operator is meant to operate on a single line and pays no attention to line numbers. To do what you want, the Perl code simply needs to be a bit more complex:
perl -p -i -e "s/$/text/ if $. == 3" file.txt
That if checks Perl's $INPUT_LINE_NUMBER which is also known as $.. See this article on Perl's special variables.