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 7034801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:13:46+00:00 2026-05-28T01:13:46+00:00

I have a file like this: ( data.dat ) 1 1 1 1 1

  • 0

I have a file like this: (data.dat)

 1  1            1  1            1  1            1  1            1  1            1  1            1  1            1  1
 2  2            2  2            2  2            2  2            2  2            2  2            2  2            2  2
 3  3            3  3            3  3            3  3            3  3            3  3            3  3            3  3
 4  4            4  4            4  4            4  4            4  4            4  4            4  4            4  7
 5  5            5  5            5  5            5  5            5  5            5  5            5  5            5  9
 6  6            6  6            6  6            6  6            6  6            6  7            6  7           
 7  9            7  7            7  7            7  7            7  7            7  8            7  9         
 8 10            8  9            8  9            8  8            8  8            8  9                       
                 9  11           9  10           9  9            9  9            9  10                      
                10  12          10  11                          10  10          10  11                    

The odd columns are simple line counters (NR), the even columns are simple values. I would like to get those values, in which the second (or even) colum values are the same in all even columns, i.e. I should get this output:

1
2
3
9

I have already tried to make this line, but something is wrong:

awk '{arr1[$1]=$2;arr2[$3]=$4;arr3[$5]=$6;arr4[$7]=$8;arr5[$9]=$10;arr6[$11]=$12;arr7[$13]=$14;arr8[$15]=$16;}END{for(x in arr1) if(x in arr2 && x in arr3 && x in arr4 && x in arr5 && x in arr6 && x in arr7 && x in arr8) print arr1[x];}' data.dat | sort -n

Is there a better way, by the way?

UPDATE: The real problem is that the array indices are different. So, the arr[…] method does not work… 🙁

  • 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-28T01:13:47+00:00Added an answer on May 28, 2026 at 1:13 am

    This would work –

    awk '
    BEGIN{x=0}
    {if (x<NF) x=NF;for (i=2;i<=NF;i+=2) a[$i]++} 
    END{x=x/2;for (y in a) if (x==a[y]) print y}' INPUT_FILE
    

    Explanation:

    1. We set a variable x=0 in the BEGIN statement.
    2. We use this variable to get to find out maximum number of fields (This is useful later).
    3. We store value of every second column to an array and get their number of occurrences.
    4. We divide the variable x by 2 to verify maximum number a value can occur in every second column.
    5. If the occurrences of numbers in an array matches this variable it means they are present in every second column.

    Test: with your sample file

    [jaypal:~/Temp] awk '
    BEGIN{x=0}
    {if (x<NF) x=NF;for (i=2;i<=NF;i+=2) a[$i]++} 
    END{x=x/2;for (y in a) if (x==a[y]) print y}' file
    2
    3
    9
    1
    

    You can either pipe the output to sort -n to get it in order or use this –

    awk '
    BEGIN{x=0}
    {if (x<NF) x=NF;for (i=2;i<=NF;i+=2) a[$i]++} 
    END{x=x/2;for (i=1;i<=length(a);i++) if (x==a[i]) print i}' INPUT_FILE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an file with a specific data I would like to pull. The
I have a file which looks like this (3 columns and n number of
I have a text file like this: settings:value data:integer:boolean data:integer:boolean data:integer:boolean data:integer:boolean ... I
Given a file with data like this (ie stores.dat file) sid|storeNo|latitude|longitude 2tt|1|-28.0372000t0|153.42921670 9|2t|-33tt.85t09t0000|15t1.03274200 Desired
I have a file like this. rm a.txt mkdir foo cp a.doc docs I
i have a file like this: term1 term2 term3 term4 term2 term1 term5 term3
Currently I just have a file like this that I manually parse into a
I'm creating a Shell Script, and I have a file like this called expressions.txt
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //

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.