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?
awk '{print gsub(/\t/,"")}' inputfile > output.txt
如果您将\t
其视为字段分隔符,则每行将少一个\t
字段:
awk -F'\t' '{ print NF-1 }' input.txt > output.txt
sed 's/[^\t]//g' input.txt | awk '{ print length }' > output.txt
基于这个答案。
这将给出文件中的选项卡总数:
od -c infile | grep -o "\t" | wc -l > output.txt
这将逐行为您提供选项卡数量:
awk '{print gsub(/\t/,"")}' infile > output.txt