Solved

Processing a file in BASH

Posted on 2014-04-03
3
237 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

808 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