你如何检查一个文本文件是否有制表符作为它在 bash 中的分隔符?

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

How do you check if a text file had tab as its delimiter in bash?

bashshellawksedgrep

提问by Redson

So I have a text file and it may have a tab as its field separator (delimiter) or it may have a space as a field separator. I would like to check if that text file is tabulated otherwise I will do something else with the file. I am using a bash script. So i'm open to anything with pure bash, sed, awk, grep, etc. (NOTE: that they are all GNU). So I am thinking of a structure like this:

所以我有一个文本文件,它可能有一个制表符作为它的字段分隔符(分隔符),或者它可能有一个空格作为字段分隔符。我想检查该文本文件是否已制成表格,否则我将对文件执行其他操作。我正在使用 bash 脚本。所以我对任何纯 bash、sed、awk、grep 等都持开放态度(注意:它们都是 GNU)。所以我在考虑这样的结构:

if [if delimiter is tab]; then
    #do soemthing
elif [if delimiter is space]; then
    #do something else
fi

Any suggestions? Let me know if further explanation is required. Thanks!

有什么建议?如果需要进一步解释,请告诉我。谢谢!

Here is an explanation updateon what the text file looks like:

以下是有关文本文件外观的说明更新

If the text file has a tab as delimiter, then it delimited on every line. If the text file has a space as delimiter, then it is NOT delimited every line.

如果文本文件有一个制表符作为分隔符,那么它会在每一行上分隔。如果文本文件有一个空格作为分隔符,那么它不会每行分隔。

Here are examples of possible text files that I might be facing:

以下是我可能面临的可能文本文件的示例:

Delimiter is tab:

分隔符是制表符

col1   col2   col3
-------
1   2   3
4   5   6

Delimiter is space: (the space is between 12 and 3 && 4 and 56)

分隔符是空格:(空格在 12 和 3 && 4 和 56 之间)

col1col2col3
-----------
12 3
4 56

回答by Etan Reisner

Assuming a tab will only exist on the first line when the file is tab delimited then this

假设当文件以制表符分隔时,制表符仅存在于第一行,则此

if awk '{exit !/\t/}' "$file"; then
    : # tab file
else
    : # space file
fi

should do what you want.

应该做你想做的。

Also:

还:

if [ -n "$(sed -n '/\t/p;q' "$file")" ]; then
    : # tab file
else
    : # space file
fi

回答by Dylan

The above solutions only check that there are tabs somewhere, not that the file is correctly formatted, i.e. that each line has 3 tab-separated columns.

上述解决方案只检查某处是否有制表符,而不是检查文件格式是否正确,即每行有 3 个制表符分隔的列。

I'd use something like the following, which checks that each line has the correct number of tabs:

我会使用类似以下的东西,它检查每一行是否有正确数量的选项卡:

no_cols=3
no_lines=$(cat "${file}" | wc -l)
no_tab_lines=$(cat "${file}" | cut -f${no_cols} | sed '/^$/d' | wc -l)
if [[ ${no_lines} -eq ${no_tab_lines} ]]; then
    echo "tabs"
else
    echo "not all tabs"
fi

回答by jaybee

If it is enough to test whether we have a tab on the first line, I would go without calling awk:

如果足以测试我们是否在第一行有一个选项卡,我将不调用awk

if `head -1 $file | grep -q '   '`; then
    : # tab file
else
    : # space file
fi

where the "spaces" between the two single quotes are made with Ctrl-V followed by hitting the tab key.

其中两个单引号之间的“空格”是用 Ctrl-V 然后按 Tab 键。