On Mon, Dec 12, 2022 at 12:35:11PM +1100, Jason White wrote:
> How would Perl (e.g., with -pi -e options) perform compared with Sed?
Like sed, perl creates a new file too but does it in a slightly different
way. IIRC, sed writes to a new file then renames it over the original, while
perl renames the original and then creates a new file with the original name
then deletes the renamed original file. Different method, same basic result.
In most cases, the fact that it's a new file makes no difference. You will at
some point in the edit have two versions of the file on the disk at the same
time. IMO this is mostly irrelevant - if you don't have enough free space for
this then you have a much bigger problem to solve.
More significantly, however, the new file will have a new inode number which
will break any hard links (but not symbolic links) to that file if any exist.
As hard links aren't used all that often, this probably isn't a big deal
either...but it is definitely something you need to be aware of when using
perl or sed's -i options (and similar options in other programs).
If you want to do an actual in-place edit (with edits being performed on the
same file with the same inode), you could something like the ancient (but
still useful) ed or ex. ed is the original unix text line editor, dating
back to 1969 and still included with modern unix & linux systems. ex is
included with all versions of vi that I know of, including vim. They're both
line-oriented text editors without fancy new-fangled stuff like a curses
visual interface. They're very similar - ex is like a greatly enhanced
superset of ed. ex can do a lot more than ed: everything that vi or vim can do
except the "visual" stuff that only makes sense in a visual editor like vi.
e.g.
printf '%s\n' '/SET SQL/' i '-- ' . j w q | ed -s ./file.sql
or
printf '%s\n' '/SET SQL/' i '-- ' . j w q | ex ./file.sql
Here printf is used to pipe a series of commands into ed or ex (with a newline
character between each command):
'/SET SQL/' - regex search for line matching "SET SQL"
i - insert text before current line
'-- ' - the text to insert, two dashes and a space
. - tells ed that input text is finished
j - join the two lines ("-- " and "SET SQL...")
w - write (i.e. save the edited file)
q - quit
NOTE: ed/ex commands and input text which contain spaces, tabs, shell
metacharacters, etc will need to be quoted as usual.
Alternatively to just delete the line:
printf '%s\n' '/SET SQL/' d w q | ed -s ./file.sql
or
printf '%s\n' '/SET SQL/' d w q | ex ./file.sql
craig
PS: These programs are all related and share a common history. And they
all build upon a common set of basic editing commands. ex was an advanced
re-implementation of ed and vi was originally a visual mode for ex. and vim
is an enhanced re-implementation of vi. Also, it's worth remembering that ed
is a **file** editor (as are ex and vi and vim), while sed was a **stream**
editor (nowadays, the -i option allows it to edit files too) - it modifies its
stdin and outputs to stdin.
The above is a gross simplification. For a better and more detailed
simplification of the history, see:
https://en.wikipedia.org/wiki/Ed_(text_editor)
https://en.wikipedia.org/wiki/Sed
https://en.wikipedia.org/wiki/Ex_(text_editor)
https://en.wikipedia.org/wiki/Vi
https://en.wikipedia.org/wiki/Vim_(text_editor)
_______________________________________________
luv-main mailing list -- [email protected]
To unsubscribe send an email to [email protected]