如何使“cut"命令将相同的连续分隔符视为一个? [英] How to make the 'cut' command treat same sequental delimiters as one?
问题描述
我正在尝试从基于列的、空格"调整的文本流中提取某个(第四个)字段.我正在尝试以下列方式使用 cut
命令:
cat text.txt |cut -d " " -f 4
不幸的是,cut
没有将多个空格视为一个分隔符.我可以通过 awk 进行管道传输
awk '{ printf $4;}'
或sed
sed -E "s/[[:space:]]+//g"
折叠空格,但我想知道是否有任何方法可以本地处理 cut
和几个分隔符?
尝试:
tr -s ' '
来自 tr
手册页:
I'm trying to extract a certain (the fourth) field from the column-based, 'space'-adjusted text stream. I'm trying to use the cut
command in the following manner:
cat text.txt | cut -d " " -f 4
Unfortunately, cut
doesn't treat several spaces as one delimiter. I could have piped through awk
awk '{ printf $4; }'
or sed
sed -E "s/[[:space:]]+/ /g"
to collapse the spaces, but I'd like to know if there any way to deal with cut
and several delimiters natively?
Try:
tr -s ' ' <text.txt | cut -d ' ' -f4
From the tr
man page:
-s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
这篇关于如何使“cut"命令将相同的连续分隔符视为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!