![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk - Counting number of similar lines | dhanamurthy | Shell Programming and Scripting | 8 | 05-16-2008 06:00 AM |
| Counting number of occurences | kingofprussia | UNIX for Dummies Questions & Answers | 1 | 04-01-2008 07:11 PM |
| I need help counting the fields and field separators using Nawk | scrappycc | Shell Programming and Scripting | 3 | 02-06-2008 11:47 PM |
| Counting number of files in a directory | iamalex | UNIX for Dummies Questions & Answers | 2 | 09-05-2005 10:13 AM |
| counting the number of lines | nayeemmz | Linux | 6 | 01-19-2005 12:37 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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. |
|
||||
|
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??? ![]() |
|
|||||
|
It uses associative arrays, to understand how associative arrays work in awk, see this post Unable to understand associative nature of awk arrays
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
Code:
chmod +x test ./test |
| Sponsored Links | ||
|
|