17

I need to write a script to count the number of tabs in each line of a file and print the output to a text file (e.g., output.txt).

How do I do this?

4

4 回答 4

26
awk '{print gsub(/\t/,"")}' inputfile > output.txt
于 2013-03-20T08:13:22.743 回答
12

如果您将\t其视为字段分隔符,则每行将少一个\t字段:

awk -F'\t' '{ print NF-1 }' input.txt > output.txt
于 2013-03-20T12:42:42.870 回答
2

sed 's/[^\t]//g' input.txt | awk '{ print length }' > output.txt

基于这个答案

于 2013-03-20T08:11:21.203 回答
2

这将给出文件中的选项卡总数:

od -c infile | grep -o "\t" | wc -l > output.txt

这将逐行为您提供选项卡数量:

awk '{print gsub(/\t/,"")}' infile > output.txt
于 2013-03-20T08:15:00.993 回答