Help on counting number of bytes in a field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on counting number of bytes in a field
# 1  
Old 10-13-2008
Help on counting number of bytes in a field

Hi,

I have a file that has 300 records with a load of fields. two of them are:

field_1 has between 8-9 bytes i.e. 012345678, 0123456789
field_2 has 10 bytes i.e. 01234567890

I want to be able to echo out the total of each of these fields i.e.

200 (have 8 - 9 bytes)
100 (have 10 bytes)

not sure how to do this, I have tried wc -l :

RR="wc -l customer directory/file in.dat"

the file in .dat has a space in the file name? not sure if it makes a difference?

I cannot get this to work?
any help would be appreceiated.
# 2  
Old 10-13-2008
Code:
awk '{ arr[length($1)]++; arr[length($2)]++; }
       END {print "8 - 9 bytes", arr[8]+arr[9], " 10 bytes ", arr[10] } ' file

# 3  
Old 10-14-2008
Question

Do not use awk at all so not sure how to write this??

I need two variables that will output the 8 -9 byte total and 10 byte total i.e.

8-9=awk '{ arr[length($1)]++ }
END {print "8 - 9 bytes", arr[8]+arr[9] } ' path for file?
10=awk '{ arr[length($2)]++; }
END {print " 10 bytes ", arr[10] } ' path for file?


Is this how you would use the solution by Jim???
Smilie
# 4  
Old 10-14-2008
It uses associative arrays, to understand how associative arrays work in awk, see this post https://www.unix.com/unix-dummies-que...wk-arrays.html

About running this awk script, you have to use it in the same way as he coded, its as per your requirements, if you don't know how to run it at command line, then put it in some shell-script and run that file. Something like this:
Code:
#! /bin/ksh
awk '{ arr[length($1)]++; arr[length($2)]++; }
       END {print "8 - 9 bytes", arr[8]+arr[9], " 10 bytes ", arr[10] } ' file

Save this shell-script as "test" then do the following:
Code:
chmod +x test
./test

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

X bytes of 0, Y bytes of random data, Z bytes of 5, T bytes of 1. ??

Hello guys. I really hope someone will help me with this one.. So, I have to write this script who: - creates a file home/student/vmdisk of 10 mb - formats that file to ext3 - mounts that partition to /mnt/partition - creates a file /mnt/partition/data. In this file, there will... (1 Reply)
Discussion started by: razolo13
1 Replies

2. Shell Programming and Scripting

[Solved] Counting specific characters within each field

Hello, I have a file like following: ALB_13554 1 1 1 ALB_13554 1 2 1 ALB_18544 2 0 2 ALB_18544 1 0 1 This is a sample of my file, my real file has 441845 number of fields. What I want to do is to calculate the number of 1 and 2 in each column using AWK, so, the output file looks like... (5 Replies)
Discussion started by: Homa
5 Replies

3. Shell Programming and Scripting

Bash script to find the number of files and identify which ones are 0 bytes.

I am writing a bash script to find out all the files in a directory which are empty. I am running into multiple issues. I will really appreciate if someone can please help me. #!/bin/bash DATE=$(date +%m%d%y) TIME=$(date +%H%M) DIR="/home/statsetl/input/civil/test" ... (1 Reply)
Discussion started by: monasharma13
1 Replies

4. Programming

How to get the number of bytes parsed in libxml2

Hi, I am using the libxml2 sax parser to parse a in memory xml string along with validating it against a schema. I am using the following code: xmlSAXHandlerPtr sax_ = new xmlSAXHandler(); sax_->initialized = XML_SAX2_MAGIC; sax_->startElementNs =... (0 Replies)
Discussion started by: Sam Krishna
0 Replies

5. Shell Programming and Scripting

count of files and number of bytes

1) I need a shell code to count the number of files ( without directories or sub-directories ) in a directory given as arguments I tried this code but it didn't work , maybe I tried the wrong one: numOfFiles=`find $1 -type f -maxdepth 1 | wc -l` I found it in another thread in this site.. ... (17 Replies)
Discussion started by: jack1985
17 Replies

6. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

7. UNIX for Dummies Questions & Answers

Awk counting lines with field match

Hi, Im trying to create a script that reads throught every line in a file and then counts how many lines there with a certain field that matches a input, and also ausing another awk it has to do the same as the above but to then use sort anduniq to get rid of all the unique lines with another... (8 Replies)
Discussion started by: fredted40x
8 Replies

8. Programming

Number of bytes in terminal input queue w/o blocking and consuming?

Hello, everyone. Could someone, please, tell me how to get the number of bytes in the terminal input queue without blocking and without consuming these bytes? I guess it could be called the peek functionality. I've looked at termio tcgetattr() and tcsetattr() functions but could not find... (4 Replies)
Discussion started by: Lucy.Garfeld
4 Replies

9. Shell Programming and Scripting

I need help counting the fields and field separators using Nawk

I need help counting the fields and field separators using Nawk. I have a file that has multiple lines on it and I need to read the file 1 at a time and then count the fields and field separators and then store those numbers in variables. I then need to delete the first 5 fields and the blank... (3 Replies)
Discussion started by: scrappycc
3 Replies
Login or Register to Ask a Question