如何在由制表符分隔的 bash 中拆分字符串

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

How to split a string in bash delimited by tab

bashstring-split

提问by chaimp

I'm trying to split a tab delimitted field in bash.

我正在尝试在 bash 中拆分制表符分隔的字段。

I am aware of this answer: how to split a string in shell and get the last field

我知道这个答案:how to split a string in shell and get the last field

But that does not answer for a tab character.

但这并不能回答制表符。

I want to do get the part of a string before the tab character, so I'm doing this:

我想在制表符之前获取字符串的一部分,所以我这样做:

x=`head -1 my-file.txt`
echo ${x%\t*}

But the \t is matching on the letter 't' and not on a tab. What is the best way to do this?

但是 \t 匹配字母 't' 而不是选项卡。做这个的最好方式是什么?

Thanks

谢谢

回答by Fredrik Pihl

If your file look something like this (with tab as separator):

如果您的文件看起来像这样(使用制表符作为分隔符):

1st-field   2nd-field

you can use cutto extract the first field (operates on tab by default):

您可以cut用来提取第一个字段(默认在选项卡上操作):

$ cut -f1 input
1st-field

If you're using awk, there is no need to use tailto get the last line, changing the input to:

如果您正在使用awk,则无需使用tail获取最后一行,将输入更改为:

1:1st-field     2nd-field
2:1st-field     2nd-field
3:1st-field     2nd-field
4:1st-field     2nd-field
5:1st-field     2nd-field
6:1st-field     2nd-field
7:1st-field     2nd-field
8:1st-field     2nd-field
9:1st-field     2nd-field
10:1st-field    2nd-field

Solution using awk:

使用awk的解决方法:

$ awk 'END {print }' input
10:1st-field

Pure bash-solution:

纯 bash 解决方案:

#!/bin/bash

while read a b;do last=$a; done < input
echo $last

outputs:

输出:

$ ./tab.sh 
10:1st-field

Lastly, a solution using sed

最后,解决方案使用 sed

$ sed '$s/\(^[^\t]*\).*$//' input
10:1st-field

here, $is the range operator; i.e. operate on the last line only.

这里,$是范围运算符;即只在最后一行操作。

For your original question, use a literal tab, i.e.

对于您的原始问题,请使用文字选项卡,即

x="1st-field    2nd-field"
echo ${x%   *}

outputs:

输出:

1st-field

回答by glenn Hymanman

Use $'ANSI-C'stringsin the parameter expansion:

在参数扩展中使用$'ANSI-C'字符串

$ x=$'abc\tdef\tghi'
$ echo "$s"
abc     def     ghi
$ echo ">>${x%%$'\t'*}<<"
>>abc<<

回答by Michael

Use awk.

使用awk。

echo $yourfield | awk '{print }'

or, in your case, for the first field from the the last line of a file

或者,在您的情况下,对于文件最后一行的第一个字段

tail yourfile | awk '{x=}END{print x}'

回答by user9118869

read field1 field2 <<< ${tabDelimitedField}

or

或者

read field1 field2 <<< $(command_producing_tab_delimited_output)

回答by Denis Capart

There is an easy way for a tab separated string : convert it to an array.

制表符分隔的字符串有一种简单的方法:将其转换为数组。

Create a string with tabs ($ added before for '\t' interpretation) :

创建一个带有制表符的字符串(在 '\t' 解释之前添加了 $):

AAA=$'ABC\tDEF\tGHI'

Split the string as an array using parenthesis :

使用括号将字符串拆分为数组:

BBB=($AAA) 

Get access to any element :

访问任何元素:

echo ${BBB[0]}
ABC
echo ${BBB[1]}
DEF
echo ${BBB[2]}
GHI

回答by Luchostein

x=first$'\t'second
echo "${x%$'\t'*}"

See QUOTINGin man bash

QUOTINGman bash