Search and replace (perl)

How can I achieve this? Perl would be awesome.

Input string a_b c[10_9] //Note there is a blank here
Needed Output a_b_c[10:9]

Thanks

$var ~= s/ /_/;
1 Like
#!/usr/bin/perl
#
#
use strict;

my $str = "a_b c[10_9]";
for($str) {
    s/ /_/;
    s/(.+)_/$1:/;
}
print "$str\n";
exit(0);

./testme.pl
a_b_c[10:9]

9a7c1ad217909b7a1b9d4ecaec3577b0

1 Like

also how can i strip off the text off [] and everything within it.

echo "a_b c[10_9]" | awk '{gsub(/\[[^]]*]/, " ");$1=$1}1'
a_b c