Solved

Processing a file in BASH

Posted on 2014-04-03
3
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

734 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