4

I work on a company that will not let me install any software on my computers and I run awful windows there.

I need to clean a lot of texts I copy from the intranet and save as a txt file. So I have to use sed and/or awk online live editors, like this or this

These texts are like this

01

010010-26.2010.501.0026  fafas fasdf asdf asdfsadf asdfasd fasd asasdff

fdfsadf adsf adsf asdf asdfas fadsf asdfa

02

0011-15.2016.501.0012  fafas fasdf asdf asdfsadf asdfasd fasd asasdff
asdfasd fasd asasdff
asdfasd fasd asasdff
0011-125.2013.501.0012
asdfasd fasd asasdff

See the numbers like 0011-15.2016.501.0012 this is what I want. I do not care for the rest but I want to create a new clean text with all these numbers, one per line. In the previous example, I need a text with

010010-26.2010.501.0026
0011-15.2016.501.0012
0011-125.2013.501.0012

the .501. is always present, in all numbers, as the 4th group.

I have tried this command on the sed online editor

's/\([0-9]*\-[0-9]*\.[0-9]*\.501\.[0-9]*\)/\1/'

Not working.

  • I am using this online tool... sed.js.org this is the tool I will have to use, so the command has to work there. – SpaceDog Feb 17 at 18:42
  • I have mentioned in my question. – SpaceDog Feb 17 at 18:57
  • This is not an answer for your original question (that's already solved), but you might want to check if you can use Powershell. On newer windows machines it's installed by default (I think), so if it's on your machine, you might be able to use it. It has regular expression support, exactly what you need here. In case you're forced to use one MS product anyway, you could just go all in. ;) – Nyos Feb 17 at 20:32
7

It does work, but you don't change anything, or rather change it to what it was. But with very small modification of this code you can get what you want:

sed -n 's/\([0-9]*\-[0-9]*\.[0-9]*\.501\.[0-9]*\).*/\1/p'

Notice three things:

  • -n switch, it means to not print anything by default
  • .* at the end of the group selected with (...)
  • p as a last command means print this line

Result:

010010-26.2010.501.0026
0011-15.2016.501.0012
0011-125.2013.501.0012

BTW, you can simplify a little by adding -E and using extended regular expression, i.e. get rid of backslashes in front of capturing groups:

sed -E -n 's/([0-9]*-[0-9]*\.[0-9]*\.501\.[0-9]*).*/\1/p'

Both ways work on mentioned webpage.

  • yeah, i hate this. you post your answer 12 seconds LATER and get all the credit. i will take this to meta one day ... – Bananguin Feb 17 at 18:58
  • @Bananguin Yeah, I see, perhaps because of not corrected (imho) statement about escaping -? But I've just upvoted your question as it is very good in most part. – jimmij Feb 17 at 19:03
  • thanks :D. In your oppinion not correct? But you still modified your answer and removed the escaping ``. The hyphen is no special character. Why would it need escaping? I didn't care much for the web-implementation, I just guessed that it might have tripped over that, if the statement was "not working". Turns out on my commandline I can "escape" the hyphen. So it's more a stuff character than an escape sequence. Why not ... I guess. – Bananguin Feb 17 at 19:11
  • I would ask about escaping the - but it is working without the escape. So, thanks. – SpaceDog Feb 17 at 19:12
  • 1
    @Bananguin Backslash in front of a - is not necessary, but at the same time doesn't do any harm. It only says to treat character that follows literally, but - is treated like that anyway (in this context). So in my "minimal" modified example I left it, but when it comes to simplification I removed it together with other backslashes. It is working either way. – jimmij Feb 17 at 19:18
7

It is fairly straight forward with AWK, b/c usually AWK doesn't do anything, so we just need to tell it when to do things, i.e. print the ID at the beginning of the line, if it's there

/^[0-9]+-[0-9]+\.[0-9]+\.501\.[0-9]+/{
    print $1
}

With sed it's a little different, b/c by default sed will print everything. (At least that's how these tools have been working for me.) First, we need to invoke sed as sed -n, to change its default behaviour to not do anything. Then we can

s/^\([0-9]\+-[0-9]\+\.[0-9]\+\.501\.[0-9]\+\).*$/\1/p

We need the p at the end to tell sed to print the result, if we had a matching pattern. Your particular sed expression is a NOOP because it replaces every match with itself and prints everything else as it was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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