Shell – How to write a sed script to delete numbers from a line

linuxscriptingsedshell-script

Sample File

a.b.gg3d 0000000001 0000000001 y
a.b.112a 0000000000 0000000001 y
a.b.gg3s 0000000001 0000000001 y
a.b.1111 0000000000 0000000001 m

I have a file that includes about three thousand unique lines, similar to the sample above. I want to use sed to remove the "xxxxxxxxxx xxxxxxxxxx" numbers from each line. Using the sample above, I would like the sed script to produce the following output:

a.b.gg3d y
a.b.112a y
a.b.gg3s y
a.b.1111 m

How can I accomplish this? Is there a string of commands to tell sed something like "delete all numbers after the first whitespace, but leave the last character"?

Best Answer

Given this input, you want to keep the first and last fields. Pretty simple with awk:

awk '{print $1, $NF}' filename

Using sed, this will replace all space delimited digit-only words:

sed ':a; s/ [[:digit:]]\+ / /; ta'
Related Question