I need to write a script to count the number of tabs in each line of the file and print the output to a text file (e.g. output.txt).
How can I do it?
awk '{print gsub(/\t/,"")}' inputfile > output.txt
If you treat \t as a field separator, there will be less \t in each line than there are fields:
\t
awk -F'\t' '{ print NF-1 }' input.txt > output.txt
This will give the total number of tabs in the file:
od -c infile | grep -o "\t" | wc -l > output.txt
This will give you the number of tabs per row:
awk '{print gsub(/\t/,"")}' infile > output.txt
sed 's/[^\t]//g' input.txt | awk '{ print length }' > output.txt
Based on this answer .