I want to go to a file and pick up a values based on file name and keyword present in my file?












0















I'm new to shell scripting, so i need all your help to achieve my goal. I have a file named input.csv which consists of data like shown below,



G22822 277 OR2
G22822 329 OR9
G22940 286 OR3
G22940 412 OR7
G25365 289 OR3


Now, OR2, OR9, OR3, OR7 and OR3 are also different files with the name as al_or2.cap, al_or9.cap, al_or3.cap, al_or7.cap and al_or3.cap in different path. The data present in these cap files are like shown below,



In al_or2.cap:



277 ASCII GRP 184578924
102 ASCII GRP 754815923


In al_or9.cap:



981 ASCII GRP 542189353
329 ASCII GRP 158203981


Like wise data present in all cap files.



If you see first line in input.csv(G22822 277 OR2) as data mentioned i need to go to al_or2.cap and search for keyword 277 and print G22822 and 542189353(G22822 present in input.csv and 542189353 present in al_or2.cap).



Then, for this G22822 329 OR9 i need to go to al_or9.cap and print G22822 and 158203981.



My required output like:



G22822 184578924
G22822 158203981 and so on..


Please help help me to achieve my goal.










share|improve this question

























  • G22822 277 OR2 is not .csv (comma-separated-values). Did you mean space-separated-values (or did you remove the commas?) And does what you are calling csv always have 3-fields?

    – David C. Rankin
    Nov 23 '18 at 8:39













  • What if the keyword isn't present in the second file?

    – Shawn
    Nov 23 '18 at 8:51











  • Yes they are space separated values, and always having 3 fields in it. @david

    – karthik
    Nov 23 '18 at 8:56











  • If keyword not present then i don't want to print it @ shawn

    – karthik
    Nov 23 '18 at 8:57
















0















I'm new to shell scripting, so i need all your help to achieve my goal. I have a file named input.csv which consists of data like shown below,



G22822 277 OR2
G22822 329 OR9
G22940 286 OR3
G22940 412 OR7
G25365 289 OR3


Now, OR2, OR9, OR3, OR7 and OR3 are also different files with the name as al_or2.cap, al_or9.cap, al_or3.cap, al_or7.cap and al_or3.cap in different path. The data present in these cap files are like shown below,



In al_or2.cap:



277 ASCII GRP 184578924
102 ASCII GRP 754815923


In al_or9.cap:



981 ASCII GRP 542189353
329 ASCII GRP 158203981


Like wise data present in all cap files.



If you see first line in input.csv(G22822 277 OR2) as data mentioned i need to go to al_or2.cap and search for keyword 277 and print G22822 and 542189353(G22822 present in input.csv and 542189353 present in al_or2.cap).



Then, for this G22822 329 OR9 i need to go to al_or9.cap and print G22822 and 158203981.



My required output like:



G22822 184578924
G22822 158203981 and so on..


Please help help me to achieve my goal.










share|improve this question

























  • G22822 277 OR2 is not .csv (comma-separated-values). Did you mean space-separated-values (or did you remove the commas?) And does what you are calling csv always have 3-fields?

    – David C. Rankin
    Nov 23 '18 at 8:39













  • What if the keyword isn't present in the second file?

    – Shawn
    Nov 23 '18 at 8:51











  • Yes they are space separated values, and always having 3 fields in it. @david

    – karthik
    Nov 23 '18 at 8:56











  • If keyword not present then i don't want to print it @ shawn

    – karthik
    Nov 23 '18 at 8:57














0












0








0








I'm new to shell scripting, so i need all your help to achieve my goal. I have a file named input.csv which consists of data like shown below,



G22822 277 OR2
G22822 329 OR9
G22940 286 OR3
G22940 412 OR7
G25365 289 OR3


Now, OR2, OR9, OR3, OR7 and OR3 are also different files with the name as al_or2.cap, al_or9.cap, al_or3.cap, al_or7.cap and al_or3.cap in different path. The data present in these cap files are like shown below,



In al_or2.cap:



277 ASCII GRP 184578924
102 ASCII GRP 754815923


In al_or9.cap:



981 ASCII GRP 542189353
329 ASCII GRP 158203981


Like wise data present in all cap files.



If you see first line in input.csv(G22822 277 OR2) as data mentioned i need to go to al_or2.cap and search for keyword 277 and print G22822 and 542189353(G22822 present in input.csv and 542189353 present in al_or2.cap).



Then, for this G22822 329 OR9 i need to go to al_or9.cap and print G22822 and 158203981.



My required output like:



G22822 184578924
G22822 158203981 and so on..


Please help help me to achieve my goal.










share|improve this question
















I'm new to shell scripting, so i need all your help to achieve my goal. I have a file named input.csv which consists of data like shown below,



G22822 277 OR2
G22822 329 OR9
G22940 286 OR3
G22940 412 OR7
G25365 289 OR3


Now, OR2, OR9, OR3, OR7 and OR3 are also different files with the name as al_or2.cap, al_or9.cap, al_or3.cap, al_or7.cap and al_or3.cap in different path. The data present in these cap files are like shown below,



In al_or2.cap:



277 ASCII GRP 184578924
102 ASCII GRP 754815923


In al_or9.cap:



981 ASCII GRP 542189353
329 ASCII GRP 158203981


Like wise data present in all cap files.



If you see first line in input.csv(G22822 277 OR2) as data mentioned i need to go to al_or2.cap and search for keyword 277 and print G22822 and 542189353(G22822 present in input.csv and 542189353 present in al_or2.cap).



Then, for this G22822 329 OR9 i need to go to al_or9.cap and print G22822 and 158203981.



My required output like:



G22822 184578924
G22822 158203981 and so on..


Please help help me to achieve my goal.







linux shell unix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:30









Sebastien Kerroue

13214




13214










asked Nov 23 '18 at 8:30









karthikkarthik

64




64













  • G22822 277 OR2 is not .csv (comma-separated-values). Did you mean space-separated-values (or did you remove the commas?) And does what you are calling csv always have 3-fields?

    – David C. Rankin
    Nov 23 '18 at 8:39













  • What if the keyword isn't present in the second file?

    – Shawn
    Nov 23 '18 at 8:51











  • Yes they are space separated values, and always having 3 fields in it. @david

    – karthik
    Nov 23 '18 at 8:56











  • If keyword not present then i don't want to print it @ shawn

    – karthik
    Nov 23 '18 at 8:57



















  • G22822 277 OR2 is not .csv (comma-separated-values). Did you mean space-separated-values (or did you remove the commas?) And does what you are calling csv always have 3-fields?

    – David C. Rankin
    Nov 23 '18 at 8:39













  • What if the keyword isn't present in the second file?

    – Shawn
    Nov 23 '18 at 8:51











  • Yes they are space separated values, and always having 3 fields in it. @david

    – karthik
    Nov 23 '18 at 8:56











  • If keyword not present then i don't want to print it @ shawn

    – karthik
    Nov 23 '18 at 8:57

















G22822 277 OR2 is not .csv (comma-separated-values). Did you mean space-separated-values (or did you remove the commas?) And does what you are calling csv always have 3-fields?

– David C. Rankin
Nov 23 '18 at 8:39







G22822 277 OR2 is not .csv (comma-separated-values). Did you mean space-separated-values (or did you remove the commas?) And does what you are calling csv always have 3-fields?

– David C. Rankin
Nov 23 '18 at 8:39















What if the keyword isn't present in the second file?

– Shawn
Nov 23 '18 at 8:51





What if the keyword isn't present in the second file?

– Shawn
Nov 23 '18 at 8:51













Yes they are space separated values, and always having 3 fields in it. @david

– karthik
Nov 23 '18 at 8:56





Yes they are space separated values, and always having 3 fields in it. @david

– karthik
Nov 23 '18 at 8:56













If keyword not present then i don't want to print it @ shawn

– karthik
Nov 23 '18 at 8:57





If keyword not present then i don't want to print it @ shawn

– karthik
Nov 23 '18 at 8:57












2 Answers
2






active

oldest

votes


















2














Assuming that the data you presented is accurate, and not knowing your directory structure, here's a little something



#!/bin/bash
while read a b c
do
awk -v a=$a -v b=$b 'b==$1{printf "%s %sn", a, $4}' tmp/al_$(echo $c | tr '[A-Z]' '[a-z]').cap
done < input.csv


Saving that as karthik.sh, having saved the first two lines of your input.csv, having created a subdirectory tmp with the files al_or2.cap and al_or9.cap in it, and running ./karthik.sh I get the following output:



./karthik.sh 
G22822 184578924
G22822 158203981





share|improve this answer





















  • 1





    If he is not running bash (as the question is tagged "shell", then there will be problems with ${c,,} which is a bashism. (better to use tr until you know for sure)

    – David C. Rankin
    Nov 23 '18 at 9:08













  • Fair comment, modifying post accordingly; thanks! :)

    – tink
    Nov 23 '18 at 9:28











  • Sure -- I've been caught by that more than once :)

    – David C. Rankin
    Nov 23 '18 at 9:58











  • Thank you very much...worked perfectly like how i wanted...

    – karthik
    Nov 23 '18 at 10:58











  • Dude, One more question what if i need to print 277 and OR2 as well. i.e, (G22822 184578924 277 OR2)

    – karthik
    Nov 26 '18 at 15:29



















1














If your original file has 3-fields and your data files (e.g. al_or2.cap, etc..) all have 4-fields, you can use a single call to awk and the builtin functions tolower, sprintf, getline and split to form the filename, read the corresponding file and output the results in your desired order as follows:



awk '
{ lwr = tolower($3)
str = sprintf ("al_%s.cap", lwr)
while (getline line < str > 0) {
split (line, a, " ", seps)
if (a[1] == $2)
print $1,a[4]
}
}' file


Files Present



$ cat file
G22822 277 OR2
G22822 329 OR9
G22940 286 OR3
G22940 412 OR7
G25365 289 OR3

$ cat al_or2.cap
277 ASCII GRP 184578924
102 ASCII GRP 754815923

$ cat al_or9.cap
981 ASCII GRP 542189353
329 ASCII GRP 158203981


Example Use/Output



$ awk '
> { lwr = tolower($3)
> str = sprintf ("al_%s.cap", lwr)
> while (getline line < str > 0) {
> split (line, a, " ", seps)
> if (a[1] == $2)
> print $1,a[4]
> }
> }' file
G22822 184578924
G22822 158203981


Creating a Simple awk Script



If you would like to create an awk-script from the above, you can do the following:



#!/usr/bin/awk -f
{
lwr = tolower($3)
str = sprintf ("al_%s.cap", lwr)
while (getline line < str > 0) {
split (line, a, " ", seps)
if (a[1] == $2)
print $1,a[4]
}
}


Now all you need do is to save the file and make it executable (e.g. chmod +x myscript.awk, and then



Example Use/Output



$ ./myscript.awk file
G22822 184578924
G22822 158203981





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53443069%2fi-want-to-go-to-a-file-and-pick-up-a-values-based-on-file-name-and-keyword-prese%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









    2














    Assuming that the data you presented is accurate, and not knowing your directory structure, here's a little something



    #!/bin/bash
    while read a b c
    do
    awk -v a=$a -v b=$b 'b==$1{printf "%s %sn", a, $4}' tmp/al_$(echo $c | tr '[A-Z]' '[a-z]').cap
    done < input.csv


    Saving that as karthik.sh, having saved the first two lines of your input.csv, having created a subdirectory tmp with the files al_or2.cap and al_or9.cap in it, and running ./karthik.sh I get the following output:



    ./karthik.sh 
    G22822 184578924
    G22822 158203981





    share|improve this answer





















    • 1





      If he is not running bash (as the question is tagged "shell", then there will be problems with ${c,,} which is a bashism. (better to use tr until you know for sure)

      – David C. Rankin
      Nov 23 '18 at 9:08













    • Fair comment, modifying post accordingly; thanks! :)

      – tink
      Nov 23 '18 at 9:28











    • Sure -- I've been caught by that more than once :)

      – David C. Rankin
      Nov 23 '18 at 9:58











    • Thank you very much...worked perfectly like how i wanted...

      – karthik
      Nov 23 '18 at 10:58











    • Dude, One more question what if i need to print 277 and OR2 as well. i.e, (G22822 184578924 277 OR2)

      – karthik
      Nov 26 '18 at 15:29
















    2














    Assuming that the data you presented is accurate, and not knowing your directory structure, here's a little something



    #!/bin/bash
    while read a b c
    do
    awk -v a=$a -v b=$b 'b==$1{printf "%s %sn", a, $4}' tmp/al_$(echo $c | tr '[A-Z]' '[a-z]').cap
    done < input.csv


    Saving that as karthik.sh, having saved the first two lines of your input.csv, having created a subdirectory tmp with the files al_or2.cap and al_or9.cap in it, and running ./karthik.sh I get the following output:



    ./karthik.sh 
    G22822 184578924
    G22822 158203981





    share|improve this answer





















    • 1





      If he is not running bash (as the question is tagged "shell", then there will be problems with ${c,,} which is a bashism. (better to use tr until you know for sure)

      – David C. Rankin
      Nov 23 '18 at 9:08













    • Fair comment, modifying post accordingly; thanks! :)

      – tink
      Nov 23 '18 at 9:28











    • Sure -- I've been caught by that more than once :)

      – David C. Rankin
      Nov 23 '18 at 9:58











    • Thank you very much...worked perfectly like how i wanted...

      – karthik
      Nov 23 '18 at 10:58











    • Dude, One more question what if i need to print 277 and OR2 as well. i.e, (G22822 184578924 277 OR2)

      – karthik
      Nov 26 '18 at 15:29














    2












    2








    2







    Assuming that the data you presented is accurate, and not knowing your directory structure, here's a little something



    #!/bin/bash
    while read a b c
    do
    awk -v a=$a -v b=$b 'b==$1{printf "%s %sn", a, $4}' tmp/al_$(echo $c | tr '[A-Z]' '[a-z]').cap
    done < input.csv


    Saving that as karthik.sh, having saved the first two lines of your input.csv, having created a subdirectory tmp with the files al_or2.cap and al_or9.cap in it, and running ./karthik.sh I get the following output:



    ./karthik.sh 
    G22822 184578924
    G22822 158203981





    share|improve this answer















    Assuming that the data you presented is accurate, and not knowing your directory structure, here's a little something



    #!/bin/bash
    while read a b c
    do
    awk -v a=$a -v b=$b 'b==$1{printf "%s %sn", a, $4}' tmp/al_$(echo $c | tr '[A-Z]' '[a-z]').cap
    done < input.csv


    Saving that as karthik.sh, having saved the first two lines of your input.csv, having created a subdirectory tmp with the files al_or2.cap and al_or9.cap in it, and running ./karthik.sh I get the following output:



    ./karthik.sh 
    G22822 184578924
    G22822 158203981






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 9:30

























    answered Nov 23 '18 at 8:51









    tinktink

    6,89232835




    6,89232835








    • 1





      If he is not running bash (as the question is tagged "shell", then there will be problems with ${c,,} which is a bashism. (better to use tr until you know for sure)

      – David C. Rankin
      Nov 23 '18 at 9:08













    • Fair comment, modifying post accordingly; thanks! :)

      – tink
      Nov 23 '18 at 9:28











    • Sure -- I've been caught by that more than once :)

      – David C. Rankin
      Nov 23 '18 at 9:58











    • Thank you very much...worked perfectly like how i wanted...

      – karthik
      Nov 23 '18 at 10:58











    • Dude, One more question what if i need to print 277 and OR2 as well. i.e, (G22822 184578924 277 OR2)

      – karthik
      Nov 26 '18 at 15:29














    • 1





      If he is not running bash (as the question is tagged "shell", then there will be problems with ${c,,} which is a bashism. (better to use tr until you know for sure)

      – David C. Rankin
      Nov 23 '18 at 9:08













    • Fair comment, modifying post accordingly; thanks! :)

      – tink
      Nov 23 '18 at 9:28











    • Sure -- I've been caught by that more than once :)

      – David C. Rankin
      Nov 23 '18 at 9:58











    • Thank you very much...worked perfectly like how i wanted...

      – karthik
      Nov 23 '18 at 10:58











    • Dude, One more question what if i need to print 277 and OR2 as well. i.e, (G22822 184578924 277 OR2)

      – karthik
      Nov 26 '18 at 15:29








    1




    1





    If he is not running bash (as the question is tagged "shell", then there will be problems with ${c,,} which is a bashism. (better to use tr until you know for sure)

    – David C. Rankin
    Nov 23 '18 at 9:08







    If he is not running bash (as the question is tagged "shell", then there will be problems with ${c,,} which is a bashism. (better to use tr until you know for sure)

    – David C. Rankin
    Nov 23 '18 at 9:08















    Fair comment, modifying post accordingly; thanks! :)

    – tink
    Nov 23 '18 at 9:28





    Fair comment, modifying post accordingly; thanks! :)

    – tink
    Nov 23 '18 at 9:28













    Sure -- I've been caught by that more than once :)

    – David C. Rankin
    Nov 23 '18 at 9:58





    Sure -- I've been caught by that more than once :)

    – David C. Rankin
    Nov 23 '18 at 9:58













    Thank you very much...worked perfectly like how i wanted...

    – karthik
    Nov 23 '18 at 10:58





    Thank you very much...worked perfectly like how i wanted...

    – karthik
    Nov 23 '18 at 10:58













    Dude, One more question what if i need to print 277 and OR2 as well. i.e, (G22822 184578924 277 OR2)

    – karthik
    Nov 26 '18 at 15:29





    Dude, One more question what if i need to print 277 and OR2 as well. i.e, (G22822 184578924 277 OR2)

    – karthik
    Nov 26 '18 at 15:29













    1














    If your original file has 3-fields and your data files (e.g. al_or2.cap, etc..) all have 4-fields, you can use a single call to awk and the builtin functions tolower, sprintf, getline and split to form the filename, read the corresponding file and output the results in your desired order as follows:



    awk '
    { lwr = tolower($3)
    str = sprintf ("al_%s.cap", lwr)
    while (getline line < str > 0) {
    split (line, a, " ", seps)
    if (a[1] == $2)
    print $1,a[4]
    }
    }' file


    Files Present



    $ cat file
    G22822 277 OR2
    G22822 329 OR9
    G22940 286 OR3
    G22940 412 OR7
    G25365 289 OR3

    $ cat al_or2.cap
    277 ASCII GRP 184578924
    102 ASCII GRP 754815923

    $ cat al_or9.cap
    981 ASCII GRP 542189353
    329 ASCII GRP 158203981


    Example Use/Output



    $ awk '
    > { lwr = tolower($3)
    > str = sprintf ("al_%s.cap", lwr)
    > while (getline line < str > 0) {
    > split (line, a, " ", seps)
    > if (a[1] == $2)
    > print $1,a[4]
    > }
    > }' file
    G22822 184578924
    G22822 158203981


    Creating a Simple awk Script



    If you would like to create an awk-script from the above, you can do the following:



    #!/usr/bin/awk -f
    {
    lwr = tolower($3)
    str = sprintf ("al_%s.cap", lwr)
    while (getline line < str > 0) {
    split (line, a, " ", seps)
    if (a[1] == $2)
    print $1,a[4]
    }
    }


    Now all you need do is to save the file and make it executable (e.g. chmod +x myscript.awk, and then



    Example Use/Output



    $ ./myscript.awk file
    G22822 184578924
    G22822 158203981





    share|improve this answer






























      1














      If your original file has 3-fields and your data files (e.g. al_or2.cap, etc..) all have 4-fields, you can use a single call to awk and the builtin functions tolower, sprintf, getline and split to form the filename, read the corresponding file and output the results in your desired order as follows:



      awk '
      { lwr = tolower($3)
      str = sprintf ("al_%s.cap", lwr)
      while (getline line < str > 0) {
      split (line, a, " ", seps)
      if (a[1] == $2)
      print $1,a[4]
      }
      }' file


      Files Present



      $ cat file
      G22822 277 OR2
      G22822 329 OR9
      G22940 286 OR3
      G22940 412 OR7
      G25365 289 OR3

      $ cat al_or2.cap
      277 ASCII GRP 184578924
      102 ASCII GRP 754815923

      $ cat al_or9.cap
      981 ASCII GRP 542189353
      329 ASCII GRP 158203981


      Example Use/Output



      $ awk '
      > { lwr = tolower($3)
      > str = sprintf ("al_%s.cap", lwr)
      > while (getline line < str > 0) {
      > split (line, a, " ", seps)
      > if (a[1] == $2)
      > print $1,a[4]
      > }
      > }' file
      G22822 184578924
      G22822 158203981


      Creating a Simple awk Script



      If you would like to create an awk-script from the above, you can do the following:



      #!/usr/bin/awk -f
      {
      lwr = tolower($3)
      str = sprintf ("al_%s.cap", lwr)
      while (getline line < str > 0) {
      split (line, a, " ", seps)
      if (a[1] == $2)
      print $1,a[4]
      }
      }


      Now all you need do is to save the file and make it executable (e.g. chmod +x myscript.awk, and then



      Example Use/Output



      $ ./myscript.awk file
      G22822 184578924
      G22822 158203981





      share|improve this answer




























        1












        1








        1







        If your original file has 3-fields and your data files (e.g. al_or2.cap, etc..) all have 4-fields, you can use a single call to awk and the builtin functions tolower, sprintf, getline and split to form the filename, read the corresponding file and output the results in your desired order as follows:



        awk '
        { lwr = tolower($3)
        str = sprintf ("al_%s.cap", lwr)
        while (getline line < str > 0) {
        split (line, a, " ", seps)
        if (a[1] == $2)
        print $1,a[4]
        }
        }' file


        Files Present



        $ cat file
        G22822 277 OR2
        G22822 329 OR9
        G22940 286 OR3
        G22940 412 OR7
        G25365 289 OR3

        $ cat al_or2.cap
        277 ASCII GRP 184578924
        102 ASCII GRP 754815923

        $ cat al_or9.cap
        981 ASCII GRP 542189353
        329 ASCII GRP 158203981


        Example Use/Output



        $ awk '
        > { lwr = tolower($3)
        > str = sprintf ("al_%s.cap", lwr)
        > while (getline line < str > 0) {
        > split (line, a, " ", seps)
        > if (a[1] == $2)
        > print $1,a[4]
        > }
        > }' file
        G22822 184578924
        G22822 158203981


        Creating a Simple awk Script



        If you would like to create an awk-script from the above, you can do the following:



        #!/usr/bin/awk -f
        {
        lwr = tolower($3)
        str = sprintf ("al_%s.cap", lwr)
        while (getline line < str > 0) {
        split (line, a, " ", seps)
        if (a[1] == $2)
        print $1,a[4]
        }
        }


        Now all you need do is to save the file and make it executable (e.g. chmod +x myscript.awk, and then



        Example Use/Output



        $ ./myscript.awk file
        G22822 184578924
        G22822 158203981





        share|improve this answer















        If your original file has 3-fields and your data files (e.g. al_or2.cap, etc..) all have 4-fields, you can use a single call to awk and the builtin functions tolower, sprintf, getline and split to form the filename, read the corresponding file and output the results in your desired order as follows:



        awk '
        { lwr = tolower($3)
        str = sprintf ("al_%s.cap", lwr)
        while (getline line < str > 0) {
        split (line, a, " ", seps)
        if (a[1] == $2)
        print $1,a[4]
        }
        }' file


        Files Present



        $ cat file
        G22822 277 OR2
        G22822 329 OR9
        G22940 286 OR3
        G22940 412 OR7
        G25365 289 OR3

        $ cat al_or2.cap
        277 ASCII GRP 184578924
        102 ASCII GRP 754815923

        $ cat al_or9.cap
        981 ASCII GRP 542189353
        329 ASCII GRP 158203981


        Example Use/Output



        $ awk '
        > { lwr = tolower($3)
        > str = sprintf ("al_%s.cap", lwr)
        > while (getline line < str > 0) {
        > split (line, a, " ", seps)
        > if (a[1] == $2)
        > print $1,a[4]
        > }
        > }' file
        G22822 184578924
        G22822 158203981


        Creating a Simple awk Script



        If you would like to create an awk-script from the above, you can do the following:



        #!/usr/bin/awk -f
        {
        lwr = tolower($3)
        str = sprintf ("al_%s.cap", lwr)
        while (getline line < str > 0) {
        split (line, a, " ", seps)
        if (a[1] == $2)
        print $1,a[4]
        }
        }


        Now all you need do is to save the file and make it executable (e.g. chmod +x myscript.awk, and then



        Example Use/Output



        $ ./myscript.awk file
        G22822 184578924
        G22822 158203981






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 10:20

























        answered Nov 23 '18 at 9:57









        David C. RankinDavid C. Rankin

        43.2k33151




        43.2k33151






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53443069%2fi-want-to-go-to-a-file-and-pick-up-a-values-based-on-file-name-and-keyword-prese%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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]