#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Replace alphabetical string by a string of dashes


    Hello,

    I have an alphabetical string with both lower and upper case characters, and I want to replace lower cases by dashes. What makes things tricky is that I want to retain strings of lower case characters that are 7 or less in length.

    Typical example:
    AAAaaaAAAAaaaaaaaaaaaAAAAAAAA
    should give me:
    AAAaaaAAAA-----------AAAAAAAA

    Code:
    sed  -e 's/[a-z]\{7,\}/-/g'
    The previous code replaces the string with only ONE dash, but I do need one dash to replace each character.

    I was told perl would work for this, but I am not too familiar with the language.

    Can anyone help me?

    Thanks in advance,
    Manuka
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    861
    Code:
    s/([a-z]+)/length($1)>3?($t=$1,$t=~tr#a-z#-#,$t):$1/ge;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    I couldn't pass up the challenge...

    Code:
    C:\>sed -e "/^\n/{s/\n//g;n}; s/^\(.\)\1\+/&\n/; h; s/\n.*//; /[a-z]\{7,\}/s/./-/g; H; g; D"
    AAAaaaAAAAaaaaaaaaaaaAAAAAAAA
    AAAaaaAAAA-----------AAAAAAAA
    And another perl approach:

    Code:
    C:\>perl -pe "s/[a-z]{7,}/'-' x length $&/ge"
    AAAaaaAAAAaaaaaaaaaaaAAAAAAAA
    AAAaaaAAAA-----------AAAAAAAA

    Comments on this post

    • vb.net agrees
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,251
    Rep Power
    1810
    Code:
    $string =~ s/(A{5,})$/$1!!!!!!!!!!!!/;
    couldn't pass up the joke
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0
    This is great, thanks for your genius!

IMN logo majestic logo threadwatch logo seochat tools logo