Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have line of 3 long numbers separated by comma. What I want to do is to delete the number in the middle, and leave only first number and last number separated by a comma.

Currently:

123456789123,723456789123456,834567885544334
123456789125,723456789123457,834567885544333
123456789126,723456789123458,834567885544337

Desired result:

123456789123,834567885544334
123456789125,834567885544333
123456789126,834567885544337

I tried doing this, but it didn't work with these regular expressions:

  • regular expression to find string: %[0-9]+,[0-9]+,[0-9]+
  • regular expression to replace string: ^1,^2

Which regular expression actually does what I want to achieve?

Note - I'm currently using an application called Ultraedit that runs on Windows 7, if you recommend a better text editing application for Windows that can solve my problem please let me know.

share|improve this question
    
I edited your question, if you are unhappy with the changes feel free to revert or edit it again. –  Baarn Aug 1 '12 at 10:50
    
Thank You Walter Maier-Murdnelch !! –  user836026 Aug 1 '12 at 10:58

4 Answers 4

up vote 9 down vote accepted

You can simply replace the middle number of each line with nothing.

In the editor

That is, in your editor, search-and-replace the regex

,[0-9]+,

(which only matches numbers with commas on both sides, which for your input is just the middle number) with a single comma:

,

I assume Ultraedit supports regex search-and-replace. If not, try Notepad++, which I know does.

From the command line

Since you tagged your question , here's how to do it from the command-line.

sed

Use sed, a standard Linux command also available for Windows as part of Cygwin or GnuWin32:

C:\>sed -e 's/,[0-9]+,/,/g' filename.txt

Powershell

Jens pointed out that you can also do it in Windows Powershell; see this explanation.

share|improve this answer
1  
It would not work if you replace the middle sub-expression with an empty string. You may, however, replace it with a single comma to get the desired effect. –  Adeel Zafar Soomro Aug 1 '12 at 10:55
    
Oops, that was stupid of me. Fixed. –  Mechanical snail Aug 1 '12 at 10:56
    
Thank for all of you ... it works!! –  user836026 Aug 1 '12 at 11:02
3  
Without Cygwin, you could use the powershell: blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/… –  Jens Aug 1 '12 at 13:54
    
@Jens: Thanks, added. –  Mechanical snail Aug 1 '12 at 14:22

Regex syntax varies from application to application. I am unfamiliar with Ultredit and will give a generaql anser

Your regex lacks capturing parentheses

([0-9]+),[0-9]+,([0-9]+)

share|improve this answer

In order to replace text, one needs to tag the proper sub-expression using parentheses.

In UltraEdit, you must search for the following regular expression:

 %^([0-9]+^),[0-9]+,^([0-9]+^)
share|improve this answer

Awk Equivalent!

echo "$Variable" | awk 'FS="," { print $1","$3 } '

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.