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

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Help two pieces of regex


    Hey,

    I am a complete noob and have just started reading some regex tutorials today after I realised they are crucial for programming. I am struggling to find information on two pieces of regex code that I need. I was wondering if anyone would be able to help.

    The first piece of code. I need to keep the first and last characters but all other characters need to be replaced with an asterix. Example below:

    flickhype
    this is a test
    website.com
    another test

    The above needs to come back as

    f*******e
    t*** ** * ***t
    w*********m
    a****** ***t

    each character needs to be replaced with an asterix and the spaces must remain, only the first and last character in the line must remain. Also want to say that wont always be English characters.


    Second piece of code

    example.com

    The above needs to come back as:

    *******.com

    I need to keep the dot and everything after the dot. I do apologise for asking this as I know these forums are based on the poster giving some code initially but I am a complete noob and this is way beyond me.

    Would greatly appreciate any help

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    164
    Rep Power
    19
    Code:
    $ cat file
    flickhype
    this is a test
    website.com
    another test
    $ cat test.sh
    #! /bin/bash
    
    while read line
    do
        f=${line:0:1}
        l=${line: -1:1}
        echo $line | sed "s/[^ ]/*/g; s/^./$f/; s/.$/$l/"
    done < file
    $ ./test.sh
    f*******e
    t*** ** * ***t
    w*********m
    a****** ***t
    $

IMN logo majestic logo threadwatch logo seochat tools logo