0

I have a text file in which I have certain numbers of the type-

a.bcdef0000000000000e+05

where a,b,c,d,e,f can take any values in 0-9. I want to find all of them and replace their value with abcdef i.e. 6 digits without the decimal point. How can I do it using regex in Linux or an editor like Sublime Text?

4 Answers 4

1

cheack for the regex ([\d]).([\d]{5})0*e+\d* or make changes in grouping of your regex and replace it with $1$2

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

([0-9]\.[0-9]{5}[0]{13}[e]\+05)

Demo:

http://regex101.com/r/jM9tQ6

2 Comments

Thanks for this great resource. But how do I replace them with abcdef?
An improved one for my case would be ([0-9]\.[0-9]{5}[0]{13}[e]\+05*). Now that the find part is done, any way for the replace?
0

Sublime Text solution. Go to Find->Replace... (Ctrl+H), change to Regular expression search (Alt+R), then:

"Find What:" ([\d]).([\d]{5})0*e\+\d*

"Replace With:" $1$2$3$4$5$6

Comments

0

Try this sed command:

sed -i 's/\([0-9]\)\.\([0-9]\{5\}\)0000000000000e+05/\1\2/g' <filename>

Explanation of the individual parts of the command:

  • sed: program name
  • -i: use in-place substitution (make sure to create a backup of your files before you start!)
  • 's/.../.../g': sed command: will search for the pattern between the first two slashes and substitute (hence the s) with the replacement between the second two slashes. g makes sed search/replace all matches, not just the first
  • \([0-9]\.[0-9]\{5\}\)0000000000000e+05: search pattern:
    • \(...\): grouping: allows us to reference the pattern in the substitution using \1, \2 etc.
    • [0-9]: matches any single digit
    • \.: matches a single dot (just . would match any non-newline character)
    • \{n\}: match the preceding pattern n times (in our case: match 5 consecutive digits)
    • 0000000000000e+05: matches the literal characters
  • \1\2 replace pattern: \0, \1, \2 etc. put partial matches of the pattern into the substitution string. \0 matches the entire pattern, and \1 matches the first subpattern, \2 the second subpattern etc. Subpatterns are marked using escaped parens, like so: \(...\)

3 Comments

I am getting sed: -e expression #1, char 49: unknown option to `s'
ok, I corrected the ending quote but it did not remove the decimal point. Can you edit with the correct answer?
Done. You must place the closing quote after the .../g, not after the filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.