find and replace last occurance of letter in string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnsreee
    New Member
    • Apr 2007
    • 34

    find and replace last occurance of letter in string

    Hi all,
    I have a string "Post.lang.tmp. txt" and i have to replase the ".txt" with "_large.txt ".

    Please help me regarding this problem.

    I got it in shell scripting using
    `echo $string | awk -F"/" '{print $NF}' | sed s/.txt/_valid.txt/`

    Regards
    Pnsreee
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    One way:

    [CODE=perl]
    my $string = "Post.lang.tmp. txt";
    $string =~ s/\.txt$/_large.txt/;
    print $string;
    [/CODE]

    Another way.

    [CODE=perl]
    use File::Basename qw(fileparse);

    my $string = "Post.lang.tmp. txt";

    my ($basename) = fileparse($stri ng, '.txt');
    my $newstring = "${basename}_la rge.txt";
    print $newstring;
    [/CODE]

    - Miller

    Comment

    • pnsreee
      New Member
      • Apr 2007
      • 34

      #3
      Hi Miller

      Thanks for your replay.

      I tried with this regexp s/.txt/_valid.txt/ in perl but i did not got it ...

      Please tell what $ will do in this regexp

      Regards
      Pnsreee

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        perldoc perlrequick

        - Miller

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by pnsreee
          Hi Miller

          Thanks for your replay.

          I tried with this regexp s/.txt/_valid.txt/ in perl but i did not got it ...

          Please tell what $ will do in this regexp

          Regards
          Pnsreee
          Why don't you use the regexp Miller posted? The $ on the end of .txt anchors the match to the end of the string.

          Comment

          Working...