Find the answer to your Linux question:
Results 1 to 8 of 8
Hello All, I was just wondering if someone with good experience in regex could help me, I am trying to remove a Goss and a dash from various lines but ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    9

    Regex to substitude text

    Hello All,

    I was just wondering if someone with good experience in regex could help me, I am trying to remove a Goss and a dash from various lines but I am not sure how to achieve this.

    For example I have to following lines on a txt file:
    retention.dat:comment_data=goss- 1149803
    retention.dat:comment_data=goss - 1149839
    retention.dat:comment_data=goss-1150312
    retention.dat:comment_data=12312 - Goss
    retention.dat:comment_data=423553 -GOSS

    In other words I just want to keep the number (maybe this could be another option, just print numbers after the '=' sign) removing all the variations of Goss.

    Regards,
    Carlos

  2. #2
    Just Joined!
    Join Date
    May 2009
    Posts
    5
    Hi Carlos,
    If you want to do this in Python, it'll be fairly simple.

    Read a line, convert it into a list. Now search for Goss and if there is a number after/before Goss, print it.

    Blue

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    9
    I am not good with Python, could you please provide a line as an example.

    Thank you!
    Carlos

  4. #4
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    You could use sed as follows:

    Code:
    sed -r 's/( *- *[gG][oO][sS][sS]|[gG][oO][sS][sS] *- *)//' filename

  5. #5
    Just Joined!
    Join Date
    May 2009
    Posts
    9
    Thank you!

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    if you have Python
    Code:
    #!/ussr/bin/env python
    for line in open("file"):
        one,two=line.strip().split("=")
        t=[j for j in [i.strip() for i in two.split("-")] if str.isdigit(j)]
        print '='.join([one,t[0]])

  7. #7
    Just Joined! jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    New York City - USA
    Posts
    18
    Quote Originally Posted by lomcevak View Post
    You could use sed as follows:

    Code:
    sed -r 's/( *- *[gG][oO][sS][sS]|[gG][oO][sS][sS] *- *)//' filename
    hello I attempted to do this and I am recieving the following error:

    Code:
    shine:bin jasonralph$ sed -r 's/( *- *[gG][oO][sS][sS]|[gG][oO][sS][sS] *- *)//' regex.txt 
    sed: illegal option -- r
    usage: sed script [-Ealn] [-i extension] [file ...]
           sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
    I am on OSX and I used regex.txt as the input file. What am I doing wrong?

    Jayusnn

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    that simply means your sed version does not support -r
    Code:
    awk -F"=" '{gsub(/[^0-9]/,"",$2); print $1,$2}' OFS="=" file

Posting Permissions

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