Using sed/awk to remove anything after first space

awkgrepsed

aaaaaaaa 09  
bbbbbbbb 90   
ccccccccccccccc  89  
ddddd 09

Using sed/awk/replace, in the above text I want to remove anything that comes after the first space in each line. For example the output will be:

aaaaaaaa  
bbbbbbbb    
ccccccccccccccc  
ddddd 

any help will be appreciated.

Best Answer

Sed

sed 's/\s.*$//'

Grep

grep -o '^\S*'

Awk

awk '{print $1}'

As pointed out in the comments, -o isn't POSIX; however both GNU and BSD have it, so it should work for most people.

Also, \s/\S may not be on all systems, if yours doesn't recognize it you can use a literal space, or if you want space and tab, those in a bracket expression ([...]), or the [[:blank:]] character class (note that strictly speaking \s is equivalent to [[:space:]] and includes vertical spacing characters as well like CR, LF or VT which you probably don't care about).

The awk one assumes the lines don't start with a blank character.

Related Question