0

Coming to the sed part of an assignment I am facing difficulty with a RegEx (on Ubuntu) which deletes every word that contains a one or more numbers.

Here is the expression I got so far: echo sed /\w.*[0-9]+.*\w/g text > text

Sample:

asdkbasdnas jasndasn7bkjns 789 jksndkasnd 092 jkasdsa

Desired output would be:

asdkbasdnas jksndkasnd jkasdsa
αғsнιη
  • 2,627
  • 2
  • 25
  • 38

2 Answers2

0

sed only removes whole lines which contain the regex. You can't remove a word on one line with sed. Here's a possible workaround:

tr ' ' '\n' | sed '/[0-9]/d' | tr '\n' ' '
  1. Replace spaces by newlines.
  2. Remove all lines(previously words) containing numbers.
  3. Replace newlines again by a space.

This only works if you only have on line to filter.

Kokogino
  • 984
  • 1
  • 5
  • 17
  • Thank you for the shart answer. I've been trying a similar method a while ago, but one of my colleagues solved it only with a sed so I thought I'll ask around. This way it perfectly solves everything. –  Apr 17 '18 at 12:32
0

You can use sed here:

$ sed 's/\w*[0-9]\w*\s*//g' infile
asdkbasdnas jksndkasnd jkasdsa
αғsнιη
  • 2,627
  • 2
  • 25
  • 38
  • `\s*` isn't needed – 123 Apr 17 '18 at 14:17
  • that's required to delete extra spaces will appear once repeated match seen and deleted, so it will match spaces after each match and will delete those as well. check on OP's sample with and without that, then you will see the differences, or test on `abc 123 543 354 f5fg 5ddd hhg6 66666g xyz` – αғsнιη Apr 17 '18 at 16:40