The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2008
Pablo_beezo Pablo_beezo is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 77
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 (permalink)  
Old 10-13-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Code:
awk '{ arr[length($1)]++; arr[length($2)]++; }
       END {print "8 - 9 bytes", arr[8]+arr[9], " 10 bytes ", arr[10] } ' file
  #3 (permalink)  
Old 10-14-2008
Pablo_beezo Pablo_beezo is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 77
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???
  #4 (permalink)  
Old 10-14-2008
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
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
Save this shell-script as "test" then do the following:
Code:
chmod +x test
./test
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0