Eytan Eytan - 5 months ago 13
Perl Question

How to remove only the last word from a file



I created the following Perl one-liner in order to remove a word from a file

This Perl also escapes special characters such as

@
or
$
or
*
, so every word that contains a special character will removed from the file.

How to change the Perl syntax in order to delete only the last word from a file and not all the words?

Example

more file

Kuku
Toto
Kuku
kuku




export REPLACE_NAME="Kuku "
export REPLACE_WITH=""
perl -i -pe 'next if /^#/; s/(^|\s)\Q$ENV{REPLACE_NAME }\E(\s|$)/$1$ENV{ REPLACE_WITH }$2/' file


expected results

more file
Kuku
Toto
Kuku

Answer

Use Tie::File to make this easier.

$ perl -MTie::File -E'tie @file, "Tie::File", shift or die; $file[-1] =~ s/\b\Q$ENV{REPLACE_NAME}\E\b/$ENV{REPLACE_WITH}/' file

Update: Rewriting as a program in order to explain it.

# Load the Tie::File module
use Tie::File;

# Tie::File allows you to create a link between an array and a file,
# so that any changes you make to the array are reflected in file.
# The "tie()" function connects the file (passed as an argument and
# therefore accessible using shift()) to a new array (called @file).
tie my @file, 'Tie::File', shift
  or die;

# The last line of the file will be in $file[-1].
# We use s/.../.../ to make a substitution on that line.
$file[-1] =~ s/\b\Q$ENV{REPLACE_NAME}\E\b/$REPLACE_WITH/;