Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell: How to cut a single string with "Cut"?

i'm trying to cut a string into the shell. I'd like to do something like:

cut -d' ' -f1 "hello 12345 xyz" 

but the problem is that cut accept a file, so if i pass the string to it, it tries to open the unexistent file called "hello 12345 xyz" and then tries to cut its content

I'd like to resolve this problem with the base programs, so don't tell me to use awk

thanks!

like image 489
Ryno Avatar asked Jul 05 '14 13:07

Ryno


2 Answers

To get cut to act on a string instead of a file, you have to give it the string via STDIN. The most common way to do so is this:

$ echo 'hello 12345 xyz' | cut -f 1
hello
like image 160
whereswalden Avatar answered Sep 27 '22 18:09

whereswalden


You can use Here Strings in BASH:

cut -d' ' -f1 <<< "hello 12345 xyz"
hello
like image 44
anubhava Avatar answered Sep 27 '22 19:09

anubhava