Solved

Processing a file in BASH

Posted on 2014-04-03
3
236 Views
Last Modified: 2014-04-03
Here is a sample file which provides counts of how many times a record is duplicated in a file

      2 D1111111111     AAAAAA
      2 D2222222222     BB2222
      2 D3333333333     CC3333
     39 D4444444444     EE4444
      2 T5555555555     DD5555
      2 D6666666666     FF6666
      2 T7777777777     GG7777
      2 D8888888888     HH8888


The first field in the row is a count of how many times the record appeared in a file.  Everything after that is the duplicate row

I would like to do the following:
for every row where the 2nd field starts with D, sum the 1st field.

So what I am expecting is the sum= 2+2+2+39+2+2 = 49 (the 2 rows that commenced with T is excluded)

I don't suspect this file of duplicates to be long, however I have been trying to accomplish this using awk and grep as a one liner,  rather than a line by line reading of the file in a for loop

Any help is appreciated.
0
Comment
Question by:klyles95
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39974410
awk '$2~/^D/{sum+=$1}END{print sum}'   sample.file
0
 

Author Closing Comment

by:klyles95
ID: 39974424
Perfect!!
0
 

Author Comment

by:klyles95
ID: 39974442
Hi ozo...one more question.  I am trying to count how many rows where the S2 started with D
awk '$2~/^D/{print NR}'   sample.file

Open in new window

is returning the line number where it appears rather than the count of rows.

Any ideas

Dont worry.  I figured it out

awk '$2~/^D/ {count++} END {print count}'   sample.file

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now