No, you cannot. If you want to be able to use more than one character (or even a regex) for the delimiter then use awk instead.
cut: can we set multiple spaces as the delimiter?
Domanda
I have text like this:
word1 word2 word3 word4
There may be more than one space between a pair of words and I want to get some columns of words from each line . When I use cat file | cut -d ' ' -f1,2,4
it seems that some fields are space which is not what I expected. I know awk
can achieve this. The question is can we do this with cut
only? i.e., can we set multiple spaces as the delimiter in cut
, yet the number varies?
Soluzione 2
Altri suggerimenti
As others have stated, cut
can't do it alone (and awk
is the best choice, because it's the only tool required). If you still want to use cut
, you can combine it with tr
, however:
tr -s ' ' <<<"word1 word2 word3 word4" | cut -d ' ' -f1,2,4
tr -s ' '
folds each span of multiple spaces into one space each.
use awk
, bro: awk '{print $1, $2, $4;}' file
Thank you @fedorqui for the sugesstion
Also, you can use the positional parameters
line="word1 word2 word3 word4"
set -- $line # no quotes here!
echo $1 $2 $4
word1 word2 word4
Assuming you have a reasonable IFS (e.g. IFS=$' \n\t'
) Use word-splitting before passing to cut
$ var="word1 word2 word3 word4"; echo $var
word1 word2 word3 word4
$ var="word1 word2 word3 word4"; echo $var | cut -d ' ' -f1,2,4
word1 word2 word4
So for you
$ var=$(cat "file"); echo $var | cut -d ' ' -f1,2,4
No, I'm sorry, delimiter in cut is always only one character. But you can use read instead
while read col1 col2 col3 col4
do
echo "$col1 $col2 $col4"
done < file
Since BASH's default Internal Field Separator is whitespace, you can avoid using cut
or any other external tools by using the BASH read
builtin command.
while read f1 f2 _ f4; do echo "$f1 $f2 $f4"; done < file
Here, the _
simply acts as a junk variable for the third field.