Home > Mobile >  remove string after finding specific character in Perl scripting
remove string after finding specific character in Perl scripting

Time:10-12

I am new to Perl. my current program only removes the last three characters of the string. Currently, I am finding a way to find the count including and remove using substr or if there is any built-in function in perl

open my $hfile, $ARGV[0] or die "Can't open $ARGV[0] for reading: $!";
while( my $line = <$hfile> ){
   if ($line =~ / /){
       $line = substr($line, -3);
       print $line;
   }
}
close $hfile;

Input file

hello_aba 32
gaww_ajnd_arhb 176
ajnbjsdsjn 416

Output file

hello_aba
gaww_ajnd_arhb
ajnbjsdsjn

CodePudding user response:

This is a classic task for a perl one-liner. I would use some caution when deleting after a control character. First off, anchor it to the end of line. Second, make sure only expected characters are deleted.

Assuming the characters to delete 1) does not include plus-signs , 2) does not include newlines, I would write:

perl -pe' s/\ [^ \n]*$// ' file.txt

[^ ... ] is a character class, and it is negated with ^ to mean "match any character that does not match what is inside".

While following nothing is probably considered a literal plus and not a meta character, I think escaping it \ is proper, and prevents future update errors. Assuring that the rest of the line does not contain assures that any extra plus signs do not cause us to lose data, e.g.

if foo = 2, then foo   bar = 4 123
#                    ^ first  ^ second

Adding $ for end of line will anchor the match to the end of the line. This will prevent any extra signs to mess up our input. Otherwise it would delete between the two first plus signs found.

Since we do not delete the line endings \n, the file structure remains unchanged.

Demonstration:

$ cat plus.txt
hello_aba 32
gaww_ajnd_arhb 176
ajnbjsdsjn 416
foo   bar = 3 123

$ perl -pe' s/\ [^ \n]*$//' plus.txt
hello_aba
gaww_ajnd_arhb
ajnbjsdsjn
foo   bar = 3

If you want to change the input file, you can either use redirection:

$ perl -pe ..... > newfile.txt

Or add the -i switch to perform in-place edit:

$ perl -pi.bak -e .... 

(.bak will create a backup file with extension .bak). Note that the original file is overwritten, so use caution.

CodePudding user response:

To remove anything after " " in your lines, use a substitution regex:

$line =~ s/\ .*// && print $line;
  • Related