Reputation: 49
I'd like to calculate the number of characters in a line, but allow for the value of a tab character to change. I've been working on a bash script that prints out the lines that have >80 characters (within the given files):
grep -r '.\{81,\}' $args
I guess I'm looking for a way to do something like this:
# pseudocode
TAB_LENGTH = 4
LINE_MAX = 80
if ( (number of non-tab characters) + TAB_LENGTH*(number of tab characters) > LINE_MAX)
print out the file, line number, and line.
fi
Any hints? (I'm quite new to bash scripting).
Upvotes: 0
Views: 199
Reputation: 165
If you want to calculate for the number of characters in a line you would treat tab as a single character ('\t'). The width of a tab is set by the shell that you are using.
so you would just need
if ( (number of characters) > LINE_MAX)
print out the file, line number, and line.
fi
if you want to have control over fixed widths you can use printf to control the minimum field width for a given string.
printf "|%-5s|" "ABC"
which would have an output like this:
|ABC··|
(the · characters are placeholders for spaces in this example)
A very useful page for this can be found here (the syntax is for c++ but the explanations translate over to bash): http://wpollock.com/CPlus/PrintfRef.htm
Upvotes: 1