shell - 如何在 bash 中按制表符拆分字符串

标签 shell cut

test1="one  two three   four    five"
echo $test1 | cut -d $'\t' -f2

我有一个由 TAB 分隔的字符串,我想通过 cut 命令获取第二个单词。

我找到了问题 How to split a string in bash delimited by tab .但该解决方案未与 cut 一起使用。

最佳答案

发生这种情况是因为您需要在 echo 时引用 $test1:

echo "$test1" | cut -d$'\t' -f2

否则,格式消失,制表符转换为空格:

$ s="hello      bye     ciao"
$ echo "$s"              <--- quoting
hello   bye ciao
$ echo $s                <--- without quotes
hello bye ciao

关于shell - 如何在 bash 中按制表符拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26379243/

相关文章:

bash - 创建带有校验和验证的文件下载脚本

shell - Makefile 中的 for 循环无效

linux - 为什么我无法在 sed 替换中转义单引号但使用\x27 有效?

linux - 如何从ifconfig中仅提取wlan0的MAC

shell - Cut 命令指定制表符作为分隔符

linux - 将变量放入其中的剪切串联

linux - 在 bash 脚本中扩展变量以在单引号中传递其值

awk - 粘贴命令留下空行

c - 如何将图像切片/切割成碎片

linux - Ubuntu下打开 "sh"的可执行文件