Ananta
Ananta

Reputation: 3711

strip words after matching character

I have a file where there are some lines in the pattern. I want to remove text after _. How do I do that in unix?

x y z 1_2 3_4 5_6

I tried this command:

$ sed 's/_.*//' 

but it returns:

x y z 1

however I want

x y z 1 3 5

Thanks

Upvotes: 1

Views: 50

Answers (1)

fedorqui
fedorqui

Reputation: 290165

Just remove every _ + character:

$ echo "x y z 1_2 3_4 5_6" | sed 's/_\w//g'
x y z 1 3 5

or, if you want to remove up to a space, remove any nonspace characters:

sed 's/_[^ ]*//g'

Upvotes: 4

Related Questions