Useful commands

From pressy's brainbackup
Jump to: navigation, search

Some useful commands

Delete the last line in a file

#!/usr/bin/perl
my $file = $ARGV[0];
open (FH, "+< $file") or die "can't update $file: $!";
while ( <FH> ) {
    $addr = tell(FH) unless eof(FH);
}
truncate(FH, $addr) or die "can't truncate $file: $!";
# ./truncate.pl <filename>

Edit a file

# echo '/^hosts/s/$/ dns/\nw' | ed nsswitch.conf

oder

# perl -pi -e 's/OLD/NEW/' filename