Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first word of string with sed

Tags:

bash

sed

How can i cut with sed the following property to have only MAC?

MAC evbyminsd58df

I did this but it works in the other side:

sed -e 's/^.\{1\}//'
like image 508
Vladimir Voitekhovski Avatar asked Nov 26 '14 12:11

Vladimir Voitekhovski


1 Answers

Just remove everything from the space:

$ echo "MAC evbyminsd58df" | sed 's/ .*//'
MAC

As you mention cut, you can use cut selecting the first field based on space as separator:

$ echo "MAC evbyminsd58df" | cut -d" " -f1
MAC

With pure Bash, either of these:

$ read a _ <<< "MAC evbyminsd58df"
$ echo "$a"
MAC

$ echo "MAC evbyminsd58df" | { read a _; echo "$a"; }
MAC
like image 163
fedorqui 'SO stop harming' Avatar answered Oct 12 '22 14:10

fedorqui 'SO stop harming'