awk compare two files and print first field in file 1Merging 2 files with based on field matchComparing two...

Was there ever a treaty between 2 entities with significantly different translations to the detriment of one party?

Avoiding racist tropes in fantasy

Why are non-collision-resistant hash functions considered insecure for signing self-generated information

How to determine car loan length as a function of how long I plan to keep a car

How many US airports have 4 or more parallel runways?

Why is there so little discussion / research on the philosophy of precision?

Prove your innocence

How to prevent clipped screen edges on my TV, HDMI-connected?

Handling Disruptive Student on the Autistic Spectrum

Disambiguation of "nobis vobis" and "nobis nobis"

Sending `C-c C-c` to the process window without swithcing to it

Strange-looking FM transmitter circuit

Does norwegian.no airline overbook flights?

Would it be possible to have a GMO that produces chocolate?

Examples of topos that are not ordinary spaces

An interview question: What's the number of String objects being created?

How would you identify when an object in a Lissajous orbit needs station keeping?

Non-visual Computers - thoughts?

Is it possible to generate a leveled character in borderlands 2?

Compelling story with the world as a villain

I don't have the theoretical background in my PhD topic. I can't justify getting the degree

“T” in subscript in formulas

How to make Ubuntu support single display 5120x1440 resolution?

Thank God it's Friday, tomorrow is THE weekend. Why the definite article?



awk compare two files and print first field in file 1


Merging 2 files with based on field matchComparing two files using awk languageCompare two file contents horizontally and verticallyCompare two files with awkCompare two files and print only the first word of the lines which don't match along with a stringcompare two files and print matches - large filescompare column of two files and print data accordinglyHow to compare two CSV files and display unique records?How to compare two different files line by line in unix?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







-1















I have two files like this.



file 1



1:apple
2:banana
3:pineapple
4:guava
5:orange


and file 2 like this



apple
guava
orange


i just want to compare file 2 with file 1 and print the 1st field for the lines missing in file 2.



Required output



3
4


Code I tried,




file1 <(cut -d: -f2 file2)




but got the output as below



1a2,3
> banana
> pineapple









share|improve this question



























  • please provide the full code of your diff and check your required output... it should be 2 and ` 3` ?!

    – pLumo
    yesterday


















-1















I have two files like this.



file 1



1:apple
2:banana
3:pineapple
4:guava
5:orange


and file 2 like this



apple
guava
orange


i just want to compare file 2 with file 1 and print the 1st field for the lines missing in file 2.



Required output



3
4


Code I tried,




file1 <(cut -d: -f2 file2)




but got the output as below



1a2,3
> banana
> pineapple









share|improve this question



























  • please provide the full code of your diff and check your required output... it should be 2 and ` 3` ?!

    – pLumo
    yesterday














-1












-1








-1








I have two files like this.



file 1



1:apple
2:banana
3:pineapple
4:guava
5:orange


and file 2 like this



apple
guava
orange


i just want to compare file 2 with file 1 and print the 1st field for the lines missing in file 2.



Required output



3
4


Code I tried,




file1 <(cut -d: -f2 file2)




but got the output as below



1a2,3
> banana
> pineapple









share|improve this question
















I have two files like this.



file 1



1:apple
2:banana
3:pineapple
4:guava
5:orange


and file 2 like this



apple
guava
orange


i just want to compare file 2 with file 1 and print the 1st field for the lines missing in file 2.



Required output



3
4


Code I tried,




file1 <(cut -d: -f2 file2)




but got the output as below



1a2,3
> banana
> pineapple






awk diff






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







upkar

















asked Dec 4 '18 at 2:42









upkarupkar

1771 silver badge10 bronze badges




1771 silver badge10 bronze badges
















  • please provide the full code of your diff and check your required output... it should be 2 and ` 3` ?!

    – pLumo
    yesterday



















  • please provide the full code of your diff and check your required output... it should be 2 and ` 3` ?!

    – pLumo
    yesterday

















please provide the full code of your diff and check your required output... it should be 2 and ` 3` ?!

– pLumo
yesterday





please provide the full code of your diff and check your required output... it should be 2 and ` 3` ?!

– pLumo
yesterday










2 Answers
2






active

oldest

votes


















1















How about an associative array lookup using Awk?



awk -F: 'NR==FNR {a[$1]; next} !($2 in a) {print $1}' file2 file1
2
3





share|improve this answer


























  • Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files.

    – upkar
    Dec 4 '18 at 3:35











  • @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues.

    – Kusalananda
    Dec 4 '18 at 7:39



















0















Use grep:



grep -vwf file2 file1 | cut -d: -f1
2
3





share|improve this answer




























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485806%2fawk-compare-two-files-and-print-first-field-in-file-1%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1















    How about an associative array lookup using Awk?



    awk -F: 'NR==FNR {a[$1]; next} !($2 in a) {print $1}' file2 file1
    2
    3





    share|improve this answer


























    • Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files.

      – upkar
      Dec 4 '18 at 3:35











    • @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues.

      – Kusalananda
      Dec 4 '18 at 7:39
















    1















    How about an associative array lookup using Awk?



    awk -F: 'NR==FNR {a[$1]; next} !($2 in a) {print $1}' file2 file1
    2
    3





    share|improve this answer


























    • Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files.

      – upkar
      Dec 4 '18 at 3:35











    • @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues.

      – Kusalananda
      Dec 4 '18 at 7:39














    1














    1










    1









    How about an associative array lookup using Awk?



    awk -F: 'NR==FNR {a[$1]; next} !($2 in a) {print $1}' file2 file1
    2
    3





    share|improve this answer













    How about an associative array lookup using Awk?



    awk -F: 'NR==FNR {a[$1]; next} !($2 in a) {print $1}' file2 file1
    2
    3






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 4 '18 at 2:57









    steeldriversteeldriver

    42.5k5 gold badges56 silver badges94 bronze badges




    42.5k5 gold badges56 silver badges94 bronze badges
















    • Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files.

      – upkar
      Dec 4 '18 at 3:35











    • @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues.

      – Kusalananda
      Dec 4 '18 at 7:39



















    • Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files.

      – upkar
      Dec 4 '18 at 3:35











    • @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues.

      – Kusalananda
      Dec 4 '18 at 7:39

















    Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files.

    – upkar
    Dec 4 '18 at 3:35





    Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files.

    – upkar
    Dec 4 '18 at 3:35













    @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues.

    – Kusalananda
    Dec 4 '18 at 7:39





    @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues.

    – Kusalananda
    Dec 4 '18 at 7:39













    0















    Use grep:



    grep -vwf file2 file1 | cut -d: -f1
    2
    3





    share|improve this answer






























      0















      Use grep:



      grep -vwf file2 file1 | cut -d: -f1
      2
      3





      share|improve this answer




























        0














        0










        0









        Use grep:



        grep -vwf file2 file1 | cut -d: -f1
        2
        3





        share|improve this answer













        Use grep:



        grep -vwf file2 file1 | cut -d: -f1
        2
        3






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        pLumopLumo

        7,46815 silver badges34 bronze badges




        7,46815 silver badges34 bronze badges

































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485806%2fawk-compare-two-files-and-print-first-field-in-file-1%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...