use strict; use warnings; my $new_shell = "/bin/nologin"; # in-place editing with backup $^I = ".bak"; # diamond operator "<>" reads from command line arguments or STDIN while ( my $line = <> ) { # skip empty lines next if $line =~ /^\s*$/; # split on ":" and return the 7th field (list slice) my $old_shell = ( split ":", $line )[6]; $line =~ s/$old_shell/$new_shell/; print "$line\n"; }