如何使“cut"命令将相同的连续分隔符视为一个? [英] How to make the 'cut' command treat same sequental delimiters as one?

查看:21
本文介绍了如何使“cut"命令将相同的连续分隔符视为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从基于列的、空格"调整的文本流中提取某个(第四个)字段.我正在尝试以下列方式使用 cut 命令:

cat text.txt |cut -d " " -f 4

不幸的是,cut 没有将多个空格视为一个分隔符.我可以通过 awk 进行管道传输

awk '{ printf $4;}'

或sed

sed -E "s/[[:space:]]+//g"

折叠空格,但我想知道是否有任何方法可以本地处理 cut 和几个分隔符?

解决方案

尝试:

tr -s ' ' 

来自 tr 手册页:

<前>-s, --squeeze-repeas 替换重复字符的每个输入序列在 SET1 中列出的单次出现那个性格的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆