ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am new to bash shell programming. I need to make a program to count no. of tab characters and no. of new line characters from a file. I have made following script, which seems to be unable to count \n and \t. how to read such special characters from a file. it was too easy in C language.
I need to count character by character, so can't use sed, awk etc.
Code:
while read -n1 char; do
nc=$(($nc + 1)) # nc : no of characters
if [[ $char = '\n' ]]; then
nl=$(($nl + 1)) # nl : no. of lines
else if [[ $char = '\t' ]]; then
nt=$(($nt + 1)) # nt : no. of tabs
fi
done < $1
As I have said earlier, I don't want to use awk, sed. I have a file and I am reading each character from the file and I want to count no. of new line character (\n) in the file by using while loop. for that I'm using following test condition :-
Here $char is the character read from the file
Code:
if [[ $char = '\n' ]]; then
But shell is not considering \n as new line character. So how to read a new line character from a file?
Last edited by mangatmodi; 11-18-2009 at 06:45 AM.
Reason: Missing Information
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.