Taj Morton
Taj Morton

Reputation: 1638

Negating whitespace regex in character class

What's the best way to capture all content in a line up to the whitespace. For example, from this:

>Peak_7847       Chr1    23988535_23998535

I would like to capture

Peak_7847

I tried:

sed -e 's/^>\([^\s]\+\)\s.*$/\1/'

(which doesn't work, and captures the whole line after the >). If I replace \s with " \t", it works:

sed -e 's/^>\([^ \t]\+\)\s.*$/\1/'
Peak_7847

How can I negate the \s operator?

Thanks!

Upvotes: 3

Views: 1524

Answers (2)

potong
potong

Reputation: 58391

This might work for you (GNU sed):

sed -r 's/(\S+).*/\1/' file

But why not:

sed 's/\s.*//' file

Upvotes: 1

anubhava
anubhava

Reputation: 785098

You can use:

str='>Peak_7847       Chr1    23988535_23998535'
sed 's/^>\([^[:space:]]*\).*$/\1/' <<< "$str"
Peak_7847

Upvotes: 4

Related Questions