I have a command, for example: echo "word1 word2"
. I want to put a pipe (|
) and get word1 from the command.
echo "word1 word2" | ....
I don't know what to put after the pipe.
Join Stack Overflow to learn, share knowledge, and build your career.
I have a command, for example: echo "word1 word2"
. I want to put a pipe (|
) and get word1 from the command.
echo "word1 word2" | ....
I don't know what to put after the pipe.
Awk is a good option if you have to deal with trailing whitespace because it'll take care of it for you:
echo " word1 word2 " | awk '{print $1;}' # Prints "word1"
Cut won't take care of this though:
echo " word1 word2 " | cut -f 1 -d " " # Prints nothing/whitespace
'cut' here prints nothing/whitespace, because the first thing before a space was another space.
-F","
to override the default field separator (a space) with a comma.
– pjd
Feb 27 '20 at 1:07
no need to use external commands. Bash itself can do the job. Assuming "word1 word2" you got from somewhere and stored in a variable, eg
$ string="word1 word2"
$ set -- $string
$ echo $1
word1
$ echo $2
word2
now you can assign $1, or $2 etc to another variable if you like.
stdin
. @Matt M. --
means stdin
, so $string
is being passed in as stdin
. stdin
is whitespace-separated into arguments $1
, $2
, $3
, etc. - just like when a Bash program evaluates arguments (e.g. check $1
, $2
, etc.), this approach takes advantage of the shell's tendency to split the stdin
into arguments automatically, removing the need for awk
or cut
.
– Caleb Xu
Apr 11 '14 at 1:38
word1=$(IFS=" " ; set -- $string ; echo $1)
Set IFS to correctly recognize the space between the words. Wrap in parentheses to avoid clobbering the original content of $1.
– Steve Pitchers
May 15 '15 at 10:27
string="*"
. Surprise.
– gniourf_gniourf
Mar 14 '18 at 17:55
I think one efficient way is the use of bash arrays:
array=( $string ) # do not use quotes in order to allow word expansion
echo ${array[0]} # You can retrieve any word. Index runs from 0 to length-1
Also, you can directly read arrays in a pipe-line:
echo "word1 word2" | while read -a array; do echo "${array[0]}" ; done
string="*"
. Surprise.
– gniourf_gniourf
Mar 14 '18 at 17:56
while
syntax to retrieve every first word at each line. Otherwise, use Boontawee Home approach. Also, please note that echo "${array[0]}"
has been quoted to prevent expansion as noticed by gniourf-gniourf.
– Isaías
Apr 9 '18 at 23:28
echo "word1 word2 word3" | { read first rest ; echo $first ; }
This has the advantage that is not using external commands and leaves the $1, $2, etc. variables intact.
$1, $2, …
intact is an extremely useful feature for script writing!
– Serge Stroobandt
Oct 21 '16 at 22:59
If you are sure there are no leading spaces, you can use bash parameter substitution:
$ string="word1 word2"
$ echo ${string/%\ */}
word1
Watch out for escaping the single space. See here for more examples of substitution patterns. If you have bash > 3.0, you could also use regular expression matching to cope with leading spaces - see here:
$ string=" word1 word2"
$ [[ ${string} =~ \ *([^\ ]*) ]]
$ echo ${BASH_REMATCH[1]}
word1
%% *
Here is another solution using shell parameter expansion. It takes care of multiple spaces after the first word. Handling spaces in front of the first word requires one additional expansion.
string='word1 word2'
echo ${string%% *}
word1
string='word1 word2 '
echo ${string%% *}
word1
The %%
signifies deleting the longest possible match of *
(a space followed by any number of whatever other characters) in the trailing part of string
.
You could try awk
echo "word1 word2" | awk '{ print $1 }'
With awk it is really easy to pick any word you like ($1, $2, ...)
I wondered how several of the top answers measured up in terms of speed. I tested the following:
1 @mattbh's
echo "..." | awk '{print $1;}'
2 @ghostdog74's
string="..."; set -- $string; echo $1
3 @boontawee-home's
echo "..." | { read -a array ; echo ${array[0]} ; }
and 4 @boontawee-home's
echo "..." | { read first _ ; echo $first ; }
I measured them with Python's timeit in a Bash script in a Zsh terminal on macOS, using a test string with 215 5-letter words. Did each measurement five times (the results were all for 100 loops, best of 3), and averaged the results:
method time
--------------------------------
1. awk 9.2ms
2. set 11.6ms (1.26 * "1")
3. read -a 11.7ms (1.27 * "1")
4. read 13.6ms (1.48 * "1")
Nice job, voters 👏 The votes (as of this writing) match the solutions' speed!
read -a
is invalid in dash).
– gniourf_gniourf
Mar 14 '18 at 17:58
echo "word1 word2" | cut -f 1 -d " "
cut cuts the 1st field (-f 1) from a list of fields delimited by the string " " (-d " ")
read
is your friend:
If string is in a variable:
string="word1 word2"
read -r first _ <<< "$string"
printf '%s\n' "$first"
If you're working in a pipe: first case: you only want the first word of the first line:
printf '%s\n' "word1 word2" "line2" | { read -r first _; printf '%s\n' "$first"; }
second case: you want the first word of each line:
printf '%s\n' "word1 word2" "worda wordb" | while read -r first _; do printf '%s\n' "$first"; done
These work if there are leading spaces:
printf '%s\n' " word1 word2" | { read -r first _; printf '%s\n' "$first"; }
As perl incorporates awk's functionality this can be solved with perl too:
echo " word1 word2" | perl -lane 'print $F[0]'
I was working with a embedded device which had neither perl, awk or python and did it with sed instead. It supports multiple spaces before the first word (which the cut
and bash
solutions did not handle).
VARIABLE=" first_word_with_spaces_before_and_after another_word "
echo $VARIABLE | sed 's/ *\([^ ]*\).*/\1/'
This was very useful when grepping ps
for process IDs since the other solutions here using only bash was not able to remove the first spaces which ps
uses to align.