Linux 如何使用 cut 拆分字符串并打印所有子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11325068/
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-08-06 13:42:40  来源:igfitidea点击:

How to Split a string and print all the substrings using cut

linuxbashtextcut

提问by Hakim

I have some comma separated strings and want to split them using cut command in bash:

我有一些逗号分隔的字符串,想在 bash 中使用 cut 命令将它们拆分:

This, is a, sample input.
This, is, another string, which could, appear, in my text, file.

I also want to print all the substrings in the output. The desired output of the first string is:

我还想打印输出中的所有子字符串。第一个字符串的期望输出是:

This
is a
sample input.

and this is the desired output of the second string:

这是第二个字符串的所需输出:

This
is
another string
which could
appear
in my text
file.

but since the number of commas(substrings) is not fixed in all of the strings, I don't know how to tell the cut command to show all the substrings. (for example there 2 commas in the first string and 6 in the second). Is there any way to do this in bash (with cut or other commands)?

但是由于逗号(子字符串)的数量在所有字符串中都不是固定的,我不知道如何告诉 cut 命令显示所有子字符串。(例如,第一个字符串中有 2 个逗号,第二个字符串中有 6 个)。有没有办法在 bash 中做到这一点(使用 cut 或其他命令)?

I have to add that although my examples in this post are in English, My actual strings are in Arabic language. I mean the command which I want to use, have to be able to work with Unicode characters.

我必须补充一点,虽然我在这篇文章中的例子是英文的,但我的实际字符串是阿拉伯语。我的意思是我想使用的命令必须能够使用 Unicode 字符。

采纳答案by perreal

Just because you wanted cut:

只是因为你想削减:

line='This, is a, sample input.'
for i in $(seq 1 $(echo "$line," | tr -dc ',' | wc -c)); do
  echo $line | cut -d, -f$i; 
done

回答by Patapoom

You can use cutand awktoo.

您也可以使用cutawk

This exampleshows how to use them.

这个例子展示了如何使用它们。

回答by Sunil Chavan

You can use tr command

您可以使用 tr 命令

 IN="This, is a, sample input."

arr=$(echo $IN | tr "," "\n")

for x in $arr
do
    echo "$x"
done

回答by c00kiemon5ter

The way I see it the problem can be approached in two ways.

在我看来,问题可以通过两种方式解决。

  1. read strings in chunks until you get a comma. Use readand a bash array
  2. replace commas (,) with newlines (\n). Use tr.
  1. 分块读取字符串,直到得到逗号。使用read和一个bash 数组
  2. ,用换行符 ( \n)替换逗号( )。使用tr.

IIRC, trshould be unicode safe, while seddepends on the implementation. I am not 100% sure about this, I will have to look it up.

IIRC,tr应该是unicode安全的,而sed取决于实现。我对此不是 100% 确定,我将不得不查找它。



using a Bash array:

使用Bash 数组

$ IFS=, read -ra arr <<< "μ?λο, πορτοκ?λι μπαν?να, αχλ?δι"
$ printf "%s\n" "${arr[@]# }"
μ?λο
πορτοκ?λι μπαν?να
αχλ?δι


using tr

使用 tr

$ echo "μ?λο, πορτοκ?λι μπαν?να, αχλ?δι" | tr ',' '\n'
μ?λο
 πορτοκ?λι μπαν?να
 αχλ?δι

notice that this substitution will leave a leading space, because words have a space after comma.

请注意,此替换将留下一个前导空格,因为单词在逗号后有一个空格。

you can get away with that by reprocessing the output to get rid of leading spaces,
or preprocessing the string to supress spaces after commas

你可以通过重新处理输出来摆脱前导空格,
或预处理字符串以在逗号后抑制空格来摆脱它



回答by cdarke

Or, if you are adverse to child processes, use Bash:

或者,如果您对子进程不利,请使用 Bash:

str='This, is a, sample input.'

IFS=','
set $str
# Remove leading spaces
while (( $# > 0 ))
do   
    echo "${1# }"    
    shift
done

EDIT: and here is a Perl solution:

编辑:这是一个 Perl 解决方案:

use warnings;
use strict;

my $str = 'This, is a, sample input.';
my @subs = split(', ',$str);

local $" = "\n";
print "@subs\n";

回答by C0de_Hard

I would ask you to use awkfor this!!

我会要求你为此使用awk!!

$ echo "μ?λο, πορτοκ?λι, αχλ?δι" | awk '{FS=", "}{for (i=1; i<=NF; i++) print $i}'

this should give

这应该给

μ?λο
πορτοκ?λι
αχλ?δι

回答by Jonyx4

This should work:

这应该有效:

aa="This, is a, sample input."
bb="This, is, another string, which could, appear, in my text, file."

echo $aa|tr ',' '\n'

echo $bb|tr ',' '\n'

Regards.

问候。