Solved

Processing a file in BASH

Posted on 2014-04-03
3
235 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
Comment Utility
awk '$2~/^D/{sum+=$1}END{print sum}'   sample.file
0
 

Author Closing Comment

by:klyles95
Comment Utility
Perfect!!
0
 

Author Comment

by:klyles95
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

762 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

10 Experts available now in Live!

Get 1:1 Help Now