Find the answer to your Linux question:
Results 1 to 8 of 8
i have problem like: sample input: s123 s3 s32532 s88888888 s3 s56 s23@hotmail.com abcd@hotmail.com i need a sed script to make the output like s00000123 s00000003 s00032532 s88888888 s00000003 s00000056 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Posts
    3

    sed programming


    i have problem like:

    sample input:

    s123 s3
    s32532 s88888888
    s3
    s56
    s23@hotmail.com
    abcd@hotmail.com

    i need a sed script to make the output like

    s00000123 s00000003
    s00032532 s88888888
    s00000003
    s00000056
    s00000023@msn.com
    abcd@msn.com


    it means if the work start with 's' than make it 8 digit(add 0s)
    and also change the hotmail.com to msn.com

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Code:
    # foo.sed
    s/s/s00000000/g
    s/hotmail/msn/g
    Then:
    Code:
    cat input | sed -f foo.sed

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Posts
    3
    it not replace by s0000000
    it is if the digit follow s not 8 digit than add 0s to make it 8 digits

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    Posts
    796
    Try reading the sed man page and doing your own homework.

  6. #5
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    it not replace by s0000000
    Well, sed is a stream editor. So it replaces things.

    In order to do this you're going to need to have a double expression to match every possibility up until eight digits. For example:

    Code:
    sed -e 's/^s[0-9]\{2\}$/s000000&/g' -e 's/s//2' some-file
    This will match a value like 's91' and turn it into 's00000091'. You will need a double expression likewise to match 's1', 's391', s'0391', etc. etc. (To break this down into little details: the first expression matches a line that starts with s and then has two digits and nothing else on the line; then it replaces it with s, a series of zeroes, and the expression it matched; then the second expression replaces the second s with nothing.)

    Pretty complicated, eh? Maybe sed is not the right tool for this. You should look into awk as well, and maybe python.

    "When all you have is a hammer, everything looks like a nail." - as they say.

  7. #6
    Just Joined!
    Join Date
    Sep 2005
    Posts
    3
    i still got the problem in the line like:
    s1 s2 s3

    i can replace like
    s00000001 s2 s0000003

    but i can not do any thing with the s2

  8. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Why have you chosen to use sed for this task?

  9. #8
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    is this homework or are you teaching yourself out of a book?

    if you're teaching yourself, re-read about count arguments to sed commands and such.

    if it's homework, stop asking us to do *your* homework.
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •