search a string and print the string and it's header












-1















I need to search for a string (number here), and print it's header or title using grep or awk or anything.



please check this example:



Input file content:



#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656


I want to search for the number "2515", and it gives me below output:



IGHTY_BBBT:
2515


and if i searched for "5179", the output should be as below:



Loyalty:
5179









share|improve this question

























  • This is really not that hard. What have you tried?

    – Tigger
    May 15 '17 at 12:01











  • I tried this command after putting source file "out" and required numbers in file list "for i in cat list;do if [[ grep -w $i out ]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.

    – Fady.Fouad
    May 15 '17 at 12:03


















-1















I need to search for a string (number here), and print it's header or title using grep or awk or anything.



please check this example:



Input file content:



#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656


I want to search for the number "2515", and it gives me below output:



IGHTY_BBBT:
2515


and if i searched for "5179", the output should be as below:



Loyalty:
5179









share|improve this question

























  • This is really not that hard. What have you tried?

    – Tigger
    May 15 '17 at 12:01











  • I tried this command after putting source file "out" and required numbers in file list "for i in cat list;do if [[ grep -w $i out ]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.

    – Fady.Fouad
    May 15 '17 at 12:03
















-1












-1








-1








I need to search for a string (number here), and print it's header or title using grep or awk or anything.



please check this example:



Input file content:



#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656


I want to search for the number "2515", and it gives me below output:



IGHTY_BBBT:
2515


and if i searched for "5179", the output should be as below:



Loyalty:
5179









share|improve this question
















I need to search for a string (number here), and print it's header or title using grep or awk or anything.



please check this example:



Input file content:



#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656


I want to search for the number "2515", and it gives me below output:



IGHTY_BBBT:
2515


and if i searched for "5179", the output should be as below:



Loyalty:
5179






linux shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 34 mins ago









Rui F Ribeiro

40.1k1479135




40.1k1479135










asked May 15 '17 at 11:56









Fady.FouadFady.Fouad

61




61













  • This is really not that hard. What have you tried?

    – Tigger
    May 15 '17 at 12:01











  • I tried this command after putting source file "out" and required numbers in file list "for i in cat list;do if [[ grep -w $i out ]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.

    – Fady.Fouad
    May 15 '17 at 12:03





















  • This is really not that hard. What have you tried?

    – Tigger
    May 15 '17 at 12:01











  • I tried this command after putting source file "out" and required numbers in file list "for i in cat list;do if [[ grep -w $i out ]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.

    – Fady.Fouad
    May 15 '17 at 12:03



















This is really not that hard. What have you tried?

– Tigger
May 15 '17 at 12:01





This is really not that hard. What have you tried?

– Tigger
May 15 '17 at 12:01













I tried this command after putting source file "out" and required numbers in file list "for i in cat list;do if [[ grep -w $i out ]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.

– Fady.Fouad
May 15 '17 at 12:03







I tried this command after putting source file "out" and required numbers in file list "for i in cat list;do if [[ grep -w $i out ]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.

– Fady.Fouad
May 15 '17 at 12:03












3 Answers
3






active

oldest

votes


















0














If headers are identified by those ##### lines, and the list of IDs to search for is in a ids.txt file (one per line), you could do:



awk '
!ids_processed{ids[$0]; next}
$0 == "#####" {getline header; getline; next}
$0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt


For instance, if ids.txt contains



2515
544
577890


On your sample, that would give:



Production_Broad:
544
IGHTY_BBBT:
2515
MC_Addr:
544
MC_Addr:
577890





share|improve this answer

































    2














    You can try something like this:



    awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt


    Where "2516" is the number you are searching for.



    obs: I'm assuming that all the labels ends with ":".



    I hope this helps.






    share|improve this answer
























    • Can i put the numbers i am searching for in a file (not only 2516)??

      – Fady.Fouad
      May 15 '17 at 12:27



















    0














    awk approach:



    awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{ 
    getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file


    The output:



    IGHTY_BBBT:
    2515




    RS="#####" - treating ##### as record separator



    -v num=2515 - a variable containing search number



    $0~/[0-9a-zA-Z_]+:/ - capturing header line



    getline nl - get next record containing numbers



    if (nl~num) - if search number is matched within a line of numbers






    share|improve this answer
























    • Can i put the numbers i am searching for in a file (not only 2515) ??

      – Fady.Fouad
      May 15 '17 at 12:32











    • @Fady.Fouad, do you want to search by multiple numbers?

      – RomanPerekhrest
      May 15 '17 at 12:34











    • Yes by multiple numbers in a file

      – Fady.Fouad
      May 15 '17 at 13:30











    • @Fady.Fouad, and how they will be separated, by space or by newline within a file?

      – RomanPerekhrest
      May 15 '17 at 13:32













    • By new line within file.

      – Fady.Fouad
      May 15 '17 at 14:40











    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%2f365160%2fsearch-a-string-and-print-the-string-and-its-header%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If headers are identified by those ##### lines, and the list of IDs to search for is in a ids.txt file (one per line), you could do:



    awk '
    !ids_processed{ids[$0]; next}
    $0 == "#####" {getline header; getline; next}
    $0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt


    For instance, if ids.txt contains



    2515
    544
    577890


    On your sample, that would give:



    Production_Broad:
    544
    IGHTY_BBBT:
    2515
    MC_Addr:
    544
    MC_Addr:
    577890





    share|improve this answer






























      0














      If headers are identified by those ##### lines, and the list of IDs to search for is in a ids.txt file (one per line), you could do:



      awk '
      !ids_processed{ids[$0]; next}
      $0 == "#####" {getline header; getline; next}
      $0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt


      For instance, if ids.txt contains



      2515
      544
      577890


      On your sample, that would give:



      Production_Broad:
      544
      IGHTY_BBBT:
      2515
      MC_Addr:
      544
      MC_Addr:
      577890





      share|improve this answer




























        0












        0








        0







        If headers are identified by those ##### lines, and the list of IDs to search for is in a ids.txt file (one per line), you could do:



        awk '
        !ids_processed{ids[$0]; next}
        $0 == "#####" {getline header; getline; next}
        $0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt


        For instance, if ids.txt contains



        2515
        544
        577890


        On your sample, that would give:



        Production_Broad:
        544
        IGHTY_BBBT:
        2515
        MC_Addr:
        544
        MC_Addr:
        577890





        share|improve this answer















        If headers are identified by those ##### lines, and the list of IDs to search for is in a ids.txt file (one per line), you could do:



        awk '
        !ids_processed{ids[$0]; next}
        $0 == "#####" {getline header; getline; next}
        $0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt


        For instance, if ids.txt contains



        2515
        544
        577890


        On your sample, that would give:



        Production_Broad:
        544
        IGHTY_BBBT:
        2515
        MC_Addr:
        544
        MC_Addr:
        577890






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 15 '17 at 15:38

























        answered May 15 '17 at 12:38









        Stéphane ChazelasStéphane Chazelas

        305k57576930




        305k57576930

























            2














            You can try something like this:



            awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt


            Where "2516" is the number you are searching for.



            obs: I'm assuming that all the labels ends with ":".



            I hope this helps.






            share|improve this answer
























            • Can i put the numbers i am searching for in a file (not only 2516)??

              – Fady.Fouad
              May 15 '17 at 12:27
















            2














            You can try something like this:



            awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt


            Where "2516" is the number you are searching for.



            obs: I'm assuming that all the labels ends with ":".



            I hope this helps.






            share|improve this answer
























            • Can i put the numbers i am searching for in a file (not only 2516)??

              – Fady.Fouad
              May 15 '17 at 12:27














            2












            2








            2







            You can try something like this:



            awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt


            Where "2516" is the number you are searching for.



            obs: I'm assuming that all the labels ends with ":".



            I hope this helps.






            share|improve this answer













            You can try something like this:



            awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt


            Where "2516" is the number you are searching for.



            obs: I'm assuming that all the labels ends with ":".



            I hope this helps.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 15 '17 at 12:08









            Daniel VasconcelosDaniel Vasconcelos

            1665




            1665













            • Can i put the numbers i am searching for in a file (not only 2516)??

              – Fady.Fouad
              May 15 '17 at 12:27



















            • Can i put the numbers i am searching for in a file (not only 2516)??

              – Fady.Fouad
              May 15 '17 at 12:27

















            Can i put the numbers i am searching for in a file (not only 2516)??

            – Fady.Fouad
            May 15 '17 at 12:27





            Can i put the numbers i am searching for in a file (not only 2516)??

            – Fady.Fouad
            May 15 '17 at 12:27











            0














            awk approach:



            awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{ 
            getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file


            The output:



            IGHTY_BBBT:
            2515




            RS="#####" - treating ##### as record separator



            -v num=2515 - a variable containing search number



            $0~/[0-9a-zA-Z_]+:/ - capturing header line



            getline nl - get next record containing numbers



            if (nl~num) - if search number is matched within a line of numbers






            share|improve this answer
























            • Can i put the numbers i am searching for in a file (not only 2515) ??

              – Fady.Fouad
              May 15 '17 at 12:32











            • @Fady.Fouad, do you want to search by multiple numbers?

              – RomanPerekhrest
              May 15 '17 at 12:34











            • Yes by multiple numbers in a file

              – Fady.Fouad
              May 15 '17 at 13:30











            • @Fady.Fouad, and how they will be separated, by space or by newline within a file?

              – RomanPerekhrest
              May 15 '17 at 13:32













            • By new line within file.

              – Fady.Fouad
              May 15 '17 at 14:40
















            0














            awk approach:



            awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{ 
            getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file


            The output:



            IGHTY_BBBT:
            2515




            RS="#####" - treating ##### as record separator



            -v num=2515 - a variable containing search number



            $0~/[0-9a-zA-Z_]+:/ - capturing header line



            getline nl - get next record containing numbers



            if (nl~num) - if search number is matched within a line of numbers






            share|improve this answer
























            • Can i put the numbers i am searching for in a file (not only 2515) ??

              – Fady.Fouad
              May 15 '17 at 12:32











            • @Fady.Fouad, do you want to search by multiple numbers?

              – RomanPerekhrest
              May 15 '17 at 12:34











            • Yes by multiple numbers in a file

              – Fady.Fouad
              May 15 '17 at 13:30











            • @Fady.Fouad, and how they will be separated, by space or by newline within a file?

              – RomanPerekhrest
              May 15 '17 at 13:32













            • By new line within file.

              – Fady.Fouad
              May 15 '17 at 14:40














            0












            0








            0







            awk approach:



            awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{ 
            getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file


            The output:



            IGHTY_BBBT:
            2515




            RS="#####" - treating ##### as record separator



            -v num=2515 - a variable containing search number



            $0~/[0-9a-zA-Z_]+:/ - capturing header line



            getline nl - get next record containing numbers



            if (nl~num) - if search number is matched within a line of numbers






            share|improve this answer













            awk approach:



            awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{ 
            getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file


            The output:



            IGHTY_BBBT:
            2515




            RS="#####" - treating ##### as record separator



            -v num=2515 - a variable containing search number



            $0~/[0-9a-zA-Z_]+:/ - capturing header line



            getline nl - get next record containing numbers



            if (nl~num) - if search number is matched within a line of numbers







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 15 '17 at 12:19









            RomanPerekhrestRomanPerekhrest

            23k12447




            23k12447













            • Can i put the numbers i am searching for in a file (not only 2515) ??

              – Fady.Fouad
              May 15 '17 at 12:32











            • @Fady.Fouad, do you want to search by multiple numbers?

              – RomanPerekhrest
              May 15 '17 at 12:34











            • Yes by multiple numbers in a file

              – Fady.Fouad
              May 15 '17 at 13:30











            • @Fady.Fouad, and how they will be separated, by space or by newline within a file?

              – RomanPerekhrest
              May 15 '17 at 13:32













            • By new line within file.

              – Fady.Fouad
              May 15 '17 at 14:40



















            • Can i put the numbers i am searching for in a file (not only 2515) ??

              – Fady.Fouad
              May 15 '17 at 12:32











            • @Fady.Fouad, do you want to search by multiple numbers?

              – RomanPerekhrest
              May 15 '17 at 12:34











            • Yes by multiple numbers in a file

              – Fady.Fouad
              May 15 '17 at 13:30











            • @Fady.Fouad, and how they will be separated, by space or by newline within a file?

              – RomanPerekhrest
              May 15 '17 at 13:32













            • By new line within file.

              – Fady.Fouad
              May 15 '17 at 14:40

















            Can i put the numbers i am searching for in a file (not only 2515) ??

            – Fady.Fouad
            May 15 '17 at 12:32





            Can i put the numbers i am searching for in a file (not only 2515) ??

            – Fady.Fouad
            May 15 '17 at 12:32













            @Fady.Fouad, do you want to search by multiple numbers?

            – RomanPerekhrest
            May 15 '17 at 12:34





            @Fady.Fouad, do you want to search by multiple numbers?

            – RomanPerekhrest
            May 15 '17 at 12:34













            Yes by multiple numbers in a file

            – Fady.Fouad
            May 15 '17 at 13:30





            Yes by multiple numbers in a file

            – Fady.Fouad
            May 15 '17 at 13:30













            @Fady.Fouad, and how they will be separated, by space or by newline within a file?

            – RomanPerekhrest
            May 15 '17 at 13:32







            @Fady.Fouad, and how they will be separated, by space or by newline within a file?

            – RomanPerekhrest
            May 15 '17 at 13:32















            By new line within file.

            – Fady.Fouad
            May 15 '17 at 14:40





            By new line within file.

            – Fady.Fouad
            May 15 '17 at 14:40


















            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%2f365160%2fsearch-a-string-and-print-the-string-and-its-header%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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt