Character count per record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Character count per record
# 8  
Old 04-12-2010
Quote:
Originally Posted by anchal_khare
also,
Code:
while read line ; do echo $line | tr -cd '[:alnum:]' | wc -c; done < file

As far as we know, the only thing to be excluded is a space (and linefeed). The complement of the alnum class will exclude a lot more than that.

Regards,
Alister

Last edited by alister; 04-12-2010 at 11:34 AM..
# 9  
Old 04-12-2010
Cant test it right now but this should work:
Code:
while read line; do line2=${line// /}; echo ${#line2}; done < file

Edit: heres a test
Code:
$ echo "123 45 678 90" | while read line ; do line=${line// /}; echo ${#line}; done
10
$


Last edited by fubaya; 04-12-2010 at 12:34 PM..
# 10  
Old 04-12-2010
Quote:
Originally Posted by ygemici
you can try this
Code:
i=0;while read line; do let i+=1; a=` echo $line | sed 's/ //g'`  ; echo "${#a}"; done <TestFile

Oh! So close to a pure sh solution, if not for that sed Smilie We can use the shell's field splitting to get rid of the spaces for us. My attempt at a posix-compliant sh solution:
Code:
#!/bin/sh

oifs=$IFS
IFS=' '

i=0
while read -r s; do
    set -- $s
    s=''
    for w; do
        s="$s$w"
    done
    echo $((++i)) - ${#s}
done < "$1"

IFS=$oifs
unset -v i oifs s w

Sample run:
Code:
$ cat data
100 NEWYORK SALES APPR
200 LA MARKETING TRRPSS
300 ROME RECEP TTLS

$ ./count.sh data
1 - 19
2 - 20
3 - 16



Sourcing Tangent


If your shell's source/dot/. command assigns positional parameters, then a subshell is not necessary. Before finishing, the script will restore IFS and unset any variables it has set (although if they [i,oifs,s,w] existed prior to the script's execution, they will have been stomped and unset).

bash 2.05b assigns the positional parameters, but its source command will stomp the current shell's positional parameters.

ksh93 also assigns the positional parameters, but its source command does not stomp the current shell's positional parameters (in this respect, it's more function-like, as the original values are still there when the sourced script has completed).

In either shell, if no argument is passed to the sourced script, the current shell's $1 is visible and used (unlike a function invocation, where it would be unset).


Bash 2.05 source run:
Code:
$ set dontexist

$ echo $1
dontexist

$ . count.sh 
-bash: dontexist: No such file or directory

$ . count.sh data
1 - 19
2 - 20
3 - 16

$ echo $1
300

300 is the first word in the final line, the last value assigned to $1 before the script finished.


ksh93 source run:
Code:
$ set dontexist

$ echo $1
dontexist

$ . count.sh              
ksh: .: count.sh: cannot open [No such file or directory]

$ . ./count.sh
ksh: .: line 13: dontexist: cannot open [No such file or directory]

$ . ./count.sh data
1 - 19
2 - 20
3 - 16

$ echo $1
dontexist

Aside from the preservation of $1, mentioned earlier, note that ksh will not source code that is not in $PATH unless the command name contains a forward slash. bash 2.05b did (perhaps the current version no longer does), which isn't a good thing.

Before I finish, I just want to say that I am not disparaging the bash shell. I am simply pointing out some of the differences between the source implementation of two shells.

Regards,
Alister

Last edited by alister; 04-12-2010 at 12:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need record count on every 30 minute

We have the below records where we need record count of every 30 minute like 00:01 to 00:30 so in that we will have 48 record count in 24 hrs , and also we need sum of record count from 00:01 to 23:30. Please find sample data as well. 00:01 21 00:02 23 00:03 34 00:04 34 00:05 30... (10 Replies)
Discussion started by: nadeemrafikhan
10 Replies

2. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

3. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

4. Shell Programming and Scripting

count of record in files

Hi all, I have written a scripts which count number of lines in all the files in a directory and write in a text file. I don't know how to format it while writing. OS suns solaris 10 my scripts is as below for i in /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/ *.LOG do echo... (11 Replies)
Discussion started by: guddu_12
11 Replies

5. UNIX for Dummies Questions & Answers

Reduce the count on the 98 record by 2,

recod,amount,noofaccount,count 98,+00000187865779787,00319,000000640/ 99,+00000187865779787,00001,000000642/ thsi is my input file my question is 1) Reduce the count on the 98 record by 2 (6 Replies)
Discussion started by: sgoud
6 Replies

6. UNIX for Advanced & Expert Users

character count per record

Hello can someone please advise. I need to send records in a file that are over 10,000 characters long to a seperate file. Any ideas? Thanks (2 Replies)
Discussion started by: Dolph
2 Replies

7. Shell Programming and Scripting

replaying a record count with another record count

i use unix command to take the record count for a file1 awk 'END{print NR}' filename i already have a file2 which conatin the count like ... .. rec_cnt=100 .. .. I want to replace the record in the file2 using the record i take from file1. suggest me some simple ways of doing it... (2 Replies)
Discussion started by: er_zeeshan05
2 Replies

8. Shell Programming and Scripting

record count

i have a file named file_names.dat where there are several files along with their path. exp: /data1/dir1/CTA_ACD_20071208.dat /data1/dir1/CTA_DFG_20071208.dat i want to write a script which will create a csv file with the name of the file and record count of that file the output file... (4 Replies)
Discussion started by: dr46014
4 Replies

9. Shell Programming and Scripting

Need help with Isql record count

What I am trying to do is check if the database query returned any records. If no records returned then output a message else output results to a file. Right now if I take out the if and else statements the code runs fine and sends the email. If no records returned the email sends the column... (4 Replies)
Discussion started by: johnu122
4 Replies

10. UNIX for Dummies Questions & Answers

How to count the record count in an EBCDIC file.

How do I get the record count in an EBCDIC file on a Linux Box. :confused: (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question