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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:31:04+00:00 2026-06-18T22:31:04+00:00

Two questions concerning using uniq command, please help. First question Say I have two

  • 0

Two questions concerning using uniq command, please help.

First question

Say I have two files;

$ cat 1.dat
0.1 1.23
0.2 1.45
0.3 1.67

$ cat 2.dat
0.3 1.67
0.4 1.78
0.5 1.89

Using cat 1.dat 2.dat | sort -n | uniq > 3.dat, I am able to merge two files into one. results is:

0.1 1.23
0.2 1.45
0.3 1.67
0.4 1.78
0.5 1.89

But if I have a scientific notation in 1.dat file,

$ cat 1.dat
1e-1 1.23
0.2 1.45
0.3 1.67

the result would be:

0.2 1.45
0.3 1.67
0.4 1.78
0.5 1.89
1e-1 1.23

which is not what I want, how can I let uniq understand 1e-1 is a number, not a string.

Second question

Same as above, but this time, let the second file 2.dat‘s first row be slightly different (from 0.3 1.67 to 0.3 1.57)

$ cat 2.dat
0.3 1.57
0.4 1.78
0.5 1.89

Then the result would be:

0.1 1.23
0.2 1.45
0.3 1.67
0.3 1.57
0.4 1.78
0.5 1.89

My question is this, how could I use uniq just based on the value from the first file and find repetition only from the first column, so that the results is still:

0.1 1.23
0.2 1.45
0.3 1.67
0.4 1.78
0.5 1.89

Thanks

A more complex test cases

$ cat 1.dat
1e-6 -1.23
0.2 -1.45
110.7 1.55
0.3 1.67e-3
  • 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-06-18T22:31:06+00:00Added an answer on June 18, 2026 at 10:31 pm

    one awk (gnu awk) one-liner solves your two problems

      awk '{a[$1*1];b[$1*1]=$0}END{asorti(a);for(i=1;i<=length(a);i++)print b[a[i]];}' file2 file1
    

    test with data: Note, I made file1 unsorted and 1.57 in file2, as you wanted:

    kent$  head *
    ==> file1 <==
    0.3 1.67
    0.2 1.45
    1e-1 1.23
    
    ==> file2 <==
    0.3 1.57
    0.4 1.78
    0.5 1.89
    
    kent$  awk '{a[$1*1];b[$1*1]=$0}END{asorti(a);for(i=1;i<=length(a);i++)print b[a[i]];}' file2 file1
    1e-1 1.23
    0.2 1.45
    0.3 1.67
    0.4 1.78
    0.5 1.89
    

    edit

    display 0.1 instead of 1e-1:

    kent$  awk '{a[$1*1];b[$1*1]=$2}END{asorti(a);for(i=1;i<=length(a);i++)print a[i],b[a[i]];}' file2 file1
    0.1 1.23
    0.2 1.45
    0.3 1.67
    0.4 1.78
    0.5 1.89
    

    edit 2

    for the precision, awk default (OFMT) is %.6g you could change it. but if you want to display different precision by lines, we have to a bit trick:

    (I added 1e-9 in file1)

    kent$  awk '{id=sprintf("%.9f",$1*1);sub(/0*$/,"",id);a[id];b[id]=$2}END{asorti(a);for(i=1;i<=length(a);i++)print a[i],b[a[i]];}'  file2 file1 
    0.000000001 1.23
    0.2 1.45
    0.3 1.67
    0.4 1.78
    0.5 1.89
    

    if you want to display same number precision for all lines:

    kent$  awk '{id=sprintf("%.9f",$1*1);a[id];b[id]=$2}END{asorti(a);for(i=1;i<=length(a);i++)print a[i],b[a[i]];}'  file2 file1 
    0.000000001 1.23
    0.200000000 1.45
    0.300000000 1.67
    0.400000000 1.78
    0.500000000 1.89
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two part question concerning Android. The first, is if I have
I have a question about the singleton pattern. I saw two cases concerning the
I've got a question concerning the creation of two plots in a row, using
I have a question concerning the performance, reliability, and best practice method of using
I have two questions concerning the Django ChoiceField: This is my form: class ContactForm(forms.Form):
I have two questions concerning the sleep() function in PHP: Does the sleep time
Two questions only: I have a website including a section of Blog in wordpress.
Two questions here, second is a little more complicated than first. 1 - I
Two questions, really: Is there a standard/convention regarding the placement on configuration files? For
I have two questions here. How can I show the header of the pop

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.