LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-18-2009, 03:02 AM   #1
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Rep: Reputation: 0
New line character Problem in shell script


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
Any help is much appreciated.
 
Old 11-18-2009, 03:53 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Code:
cat file | tr -dc '\t' | wc -c
 
Old 11-18-2009, 04:02 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Not sure if this is what you want, but it will print the number of chars, lines and tabs:
Code:
#!/bin/bash
nc="`wc -m $1 | awk '{ print $1 }'`"
nl="`awk 'END{print NR}' $1`"
nt="`awk 'BEGIN { RS = "\t" } END { print NR -1 }' $1`"

echo "nc : $nc"
echo "nl : $nl"
echo "nt : $nt"
Hope this helps.
 
Old 11-18-2009, 04:21 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
gawk
Code:
$ awk '{ m=gsub(/\t/,""); total+=m }END{ print "Total tabs: "total}' file
$ awk -F"\t" 'NF>1{total+=NF-1}END{ print "Total tabs: "total}' file
total number of characters, tabs, newlines in one gawk script
Code:
gawk  '{    
    m=gsub(/\t/,""); tabs+=m    
    n=gsub("\n",""); nl+=n    
    char+=length
}
END{ 
    print "Total tabs: "tabs
    print "Total newlines: "nl
    print "Total chars: "char
}' file
 
Old 11-18-2009, 06:37 AM   #5
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Original Poster
Rep: Reputation: 0
No awk please ...

Sir,

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
 
Old 11-18-2009, 07:30 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by mangatmodi View Post
Here $char is the character read from the file
Code:
if [[ $char = '\n' ]]; then
doing homework right? anyway, the issues is, you are using -n1, this option doesn't read new lines.
 
Old 11-18-2009, 07:44 AM   #7
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Original Poster
Rep: Reputation: 0
Thanks

@ghostdog74

Quote:
you are using -n1, this option doesn't read new lines.
Thanks for your help.
That really solved my problem.
 
  


Reply

Tags
newline, shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
csh Shell Script: String Concatenation, how do i add a new line character? vxc69 Programming 1 05-04-2009 07:51 PM
(Korn Shell/POSIX) want to make sure last character in line is a space quadmore Programming 2 09-08-2008 09:57 AM
multiple character replacement by shell script mauran Programming 14 07-13-2007 02:46 AM
Escape character not working in shell script philipz Programming 1 04-29-2004 09:58 AM


All times are GMT -5. The time now is 11:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration