Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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"?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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'
share|improve this answer
    
I understand that the s means substitute and the forward slashes are delimiters, but what do the following commands mean? :a; \+ and ; ta –  user46865 Sep 10 '13 at 20:19
    
:a is a label, ta means "go to a": in short, it is a loop –  enzotib Sep 10 '13 at 20:31
1  
And sed 's/ [[:digit:] ]\+ / /' can do it the same –  enzotib Sep 10 '13 at 20:34
    
I was thinking along the lines of enzotib, but went with sed 's/[[:space:]][[:digit:]]\+//g' which removes one or more numbers [[:digit:]]\+ proceeded by whitespace (tabs, spaces) [:space:], and also specified /g to the options, to do it for all occurrences (and not just the first) –  Drav Sloan Sep 10 '13 at 20:41
    
The reason I searched for the trailing space in a loop was to avoid matching " 123abc" -- i.e. to ensure that the word was composed only with digits. –  glenn jackman Sep 11 '13 at 12:41

Your Answer

 
discard

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