Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 562451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:33:22+00:00 2026-05-13T12:33:22+00:00

I’m stumbling over myself trying to get a seemingly simple thing accomplished. I have

  • 0

I’m stumbling over myself trying to get a seemingly simple thing accomplished. I have one file, and one newline delimited string list.

File:

Dat1 Loc1

Dat2 Loc1

Dat3 Loc1

Dat4 Loc2

Dat5 Loc2

My list is something like this:

Dat1

Dat2

Dat3

Dat4

What I am trying to do is compare the list with the data file and count the number of unique Locs that appear. I am interested only in the largest count. In the example above, when comparing the list to the file, I want essentially:

Dat1 MATCHED Loc1Count = 1

Dat2 MATCHED Loc1Count = 2

Dat3 MATCHED Loc1Count = 3

Dat4 MATCHED Loc2Count = 1

Return: Loc1 if Loc1Count/Length of List > 50%

Now,

I know that awk 1 file will read a file line by line. Furthermore I know that "echo "$LIST" | awk ‘/search for a line that contains this/" will return the line that matches that internal string. I haven’t been able to combine these ideas successfully though as nested awks, much less how to count "loc1" vs "loc2" (which, by the way, are going to be random strings, and not form-standard)

I feel like this is simple, but I’m banging my head against a wall. Any ideas? Is this sufficiently clear?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:33:22+00:00Added an answer on May 13, 2026 at 12:33 pm
    list="Dat1 Dat2 Dat3 Dat4"
    awk -vli="$list" 'BEGIN{
       # here list from shell is converted to awk array "list". 
       m=split(li,list," ") 
    }
    {
        # go through the list 
        for(i=1;i<=m;i++){
            if($1 == list[i]){
                # if Dat? is found in list, print , at the same time
                print $1" matched Locount="$2" "++data[$2]   # increment the count for $2 and store in loc array
                loc[$2]++ 
            }
        }
    } 
    END{
        # here returns loc1 count
        loc1count=loc["Loc1"]
        if(( loc1count / m *100 ) > 50) {
            print "Loc1 count: "loc1count
        }
    } ' file
    

    output

    $ ./shell.sh
    Dat1 matched Locount=Loc1 1
    Dat2 matched Locount=Loc1 2
    Dat3 matched Locount=Loc1 3
    Dat4 matched Locount=Loc2 1
    Loc1 count: 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.