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 ...
- 05-18-2009 #1Just 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
- 05-18-2009 #2Just 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
- 05-18-2009 #3Just 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
- 05-18-2009 #4Linux 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
- 05-18-2009 #5Just Joined!
- Join Date
- May 2009
- Posts
- 9
Thank you!
- 05-19-2009 #6Linux 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]])
- 05-19-2009 #7
hello I attempted to do this and I am recieving the following error:
I am on OSX and I used regex.txt as the input file. What am I doing wrong?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 ...]
Jayusnn
- 05-19-2009 #8Linux 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


Reply With Quote
