bash cut:我们可以设置多个空格作为分隔符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21322968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:21:24  来源:igfitidea点击:

cut: can we set multiple spaces as the delimiter?

bashshellcut

提问by Zhiwen Fang

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,4it seems that some fields are space which is not what I expected. I know awkcan achieve this. The question is can we do this with cutonly? i.e., can we set multiple spaces as the delimiter in cut, yet the number varies?

一对单词之间可能有多个空格,我想从每一行中获取一些单词列。当我使用时cat file | cut -d ' ' -f1,2,4,似乎有些字段不是我所期望的空间。我知道awk可以做到这一点。问题是我们能做到这一点cut吗?即,我们可以在 中设置多个空格作为分隔符cut,但数量却有所不同吗?

采纳答案by Ignacio Vazquez-Abrams

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.

你不能。如果您希望能够使用多个字符(甚至是正则表达式)作为分隔符,请改用 awk。

回答by mklement0

As others have stated, cutcan't do it alone (and awkis the best choice, because it's the only tool required). If you still want to use cut, you can combine it with tr, however:

正如其他人所说,cut不能单独完成(并且awk是最好的选择,因为它是唯一需要的工具)。如果您仍想使用cut,则可以将其与 结合使用tr,但是:

tr -s ' ' <<<"word1 word2   word3  word4" | cut -d ' ' -f1,2,4

tr -s ' 'folds each span of multiple spaces into one space each.

tr -s ' '将多个空间的每个跨度折叠成一个空间。

回答by glenn Hymanman

Also, you can use the positional parameters

此外,您可以使用位置参数

line="word1 word2   word3  word4"
set -- $line                       # no quotes here!
echo   
word1 word2 word4

回答by Reinstate Monica Please

Assuming you have a reasonable IFS (e.g. IFS=$' \n\t') Use word-splitting before passing to cut

假设您有一个合理的 IFS(例如IFS=$' \n\t')在传递给之前使用分词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

回答by user3159253

use awk, bro: awk '{print $1, $2, $4;}' file

使用awk,兄弟:awk '{print $1, $2, $4;}' file

Thank you @fedorqui for the sugesstion

谢谢@fedorqui 的建议

回答by thom

No, I'm sorry, delimiter in cutis always only one character. But you can use readinstead

不,对不起,cut 中的分隔符始终只有一个字符。但是你可以使用read代替

while read col1 col2 col3 col4
do
    echo "$col1 $col2 $col4"
done < file

回答by John B

Since BASH's default Internal Field Separator is whitespace, you can avoid using cutor any other external tools by using the BASH readbuiltin command.

由于 BASH 的默认内部字段分隔符是空格,因此您可以cut通过使用 BASHread内置命令来避免使用或任何其他外部工具。

while read f1 f2 _ f4; do echo "$f1 $f2 $f4"; done < file

Here, the _simply acts as a junk variable for the third field.

在这里,_只是作为第三个字段的垃圾变量。