Solved

Processing a file in BASH

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 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.:
Suggested Courses

631 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