Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about for loop 1

Status
Not open for further replies.

minus0

Programmer
Feb 20, 2003
73
US
I am trying to get a count of the number of characters per line in a file by name Application.log by doing a


#!/bin/ksh
for lines in $(cat Application.log)
do
count=$(echo $lines | wc -c)
echo $lines char count: $count >> WC.out
done

I am getting the character count and in addition to that, I also get the number of characters for each of the files present in the directory I am executing the script from, just so that its more clear, I am including the output of the script:

005-05-11 char count: 11
16:02:48,317 char count: 13
| char count: 2
ExecuteThread: char count: 15
'22' char count: 5
for char count: 4
queue: char count: 7
'weblogic.kernel.Default' char count: 26
| char count: 2
[DEBUG] char count: 8
| char count: 2
com.onstar.vehcomm.logging.LogHandler char count: 38
| char count: 2
- char count: 2
backup char count: 7
CountChars.sh char count: 14
Application.log char count: 21
Application.log.1 char count: 23
Application.log.10 char count: 24
Application.log.11 char count: 24
Application.log.2 char count: 23
Application.log.3 char count: 23
Application.log.4 char count: 23
Application.log.5 char count: 23
Application.log.6 char count: 23
Application.log.7 char count: 23
Application.log.8 char count: 23
Application.log.9 char count: 23

Questions -
* Is there a better way of getting the count?
* Why am I getting the character count for file names though I am targeting Application.log in my script? (Application.log doesnt have any lines like Application.log.9 etc)

Appreciate any help
 
Seems that Application.log have some * character.
Another way:
awk '{print $0 " char count: " length($0)}' Application.log

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
or just put quotes around your echo's

Code:
#!/bin/ksh
for lines in $(cat Application.log)
do
 count=$(echo "$lines" | wc -c)
 echo "$lines" char count: $count >> WC.out
done

HTH,

p5wizard
 
or

vi countit
------------------------
#!/bin/ksh

while read line
do
echo $line| wc -m
done
-------------------------

./countit < filename

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
same problem if the file contains '*' characters I assume, plus contents of line needs to be shown also...

ergo:

vi countit
------------------------
#!/bin/ksh

while read line
do
echo "$line char count: \c"|tee /dev/tty|wc -m
done
------------------------

./countit < filename



HTH,

p5wizard
 
Thanks for the inputs provided and I realized after posting that I havent checked the logs for any wild cards (*) and today after coming in to work when I looked at my logs, there was a ***** response ends ***** which was causing all the trouble. When I removed these charachters, my output matched with what I was expecting.

One thing to note though is, having quotes around $lines like "$lines" or braces like ${lines} wont help.

Thanks once again
-0
 
like "$lines" or braces like ${lines} wont help
And the awk way ?
 
The "quotes" solution does work fine on AIX. I guess not all echo's behave the same

----------
#!/bin/ksh

while read line
do
nc=$(echo "$line"|wc -m)
echo "${line} char count: ${nc}"
done
----------

# cat file
line without asterisk
next line: just 1 asterisk
*

# countit <file
line without asterisk char count: 22
next line: just 1 asterisk char count: 27
* char count: 2


HTH,

p5wizard
 
How about...


#!/bin/ksh
while read lines
do
echo $lines char count: ${#lines} >> WC.out
done < Application.log


Frank
 
PHV (MIS) 12 May 05 15:19
like "$lines" or braces like ${lines} wont help
And the awk way ?

PHV,

I am sorry for not noticing your post in the first place. I invoked the script the awk way and gave me the exact result I wanted, though not knowing squat about awk confused me a little about the usage. Thanks for the excellent tip - any web sites with awk tutorials?
 
any web sites with awk tutorials?
man awk

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
To get a count of characters per line, simply in the Korn Shell do:

Code:
#!/bin/ksh

while read line
do
  print ${#line}
done < /yourfile

The key is the ${#variable} syntax.
 
Hi,

Take care when using read because it ignores leading tab and white space charceters, so the nuber of charcters will not be exact for these lines.
It's better to use awk or perl like this :

awk '{printf "%5d:%s\n", length($0),$0}' /path/to/your/file

If you want your characters count with leading zeroes, use %.5d format in printf.

Ali
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top