How to remove specific numbers from a txt file with SED or AWK

awksedtext processing

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.

Best Answer

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.

Related Question