how to check if the first line of file contain a specific string?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I need to write a shell script that find and print all files in a directory which starts with the string: #include
Now, I know how to check if a string is in the file, by using:



for f in `ls`; do
if grep -q 'MyString' $f; then:
#DO SOMETHING
fi


but how can I apply this to the first line?
I thought maybe create a variable of the first line and check if it starts with #include, but i'm not sure how to do this, I tried the read command but I fail to read into a variable.



Beside I'd like to here other approaches to this problem, maybe awk?
Anyway remember I need to check if the first line starts with #include, not if it contain that string.
That's why I found those questions: How to print file content only if the first line matches a certain pattern?
https://stackoverflow.com/questions/5536018/how-to-print-matched-regex-pattern-using-awk
they are not completely helping.










share|improve this question



















  • 1




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    2 hours ago










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    50 mins ago














up vote
2
down vote

favorite












I need to write a shell script that find and print all files in a directory which starts with the string: #include
Now, I know how to check if a string is in the file, by using:



for f in `ls`; do
if grep -q 'MyString' $f; then:
#DO SOMETHING
fi


but how can I apply this to the first line?
I thought maybe create a variable of the first line and check if it starts with #include, but i'm not sure how to do this, I tried the read command but I fail to read into a variable.



Beside I'd like to here other approaches to this problem, maybe awk?
Anyway remember I need to check if the first line starts with #include, not if it contain that string.
That's why I found those questions: How to print file content only if the first line matches a certain pattern?
https://stackoverflow.com/questions/5536018/how-to-print-matched-regex-pattern-using-awk
they are not completely helping.










share|improve this question



















  • 1




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    2 hours ago










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    50 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I need to write a shell script that find and print all files in a directory which starts with the string: #include
Now, I know how to check if a string is in the file, by using:



for f in `ls`; do
if grep -q 'MyString' $f; then:
#DO SOMETHING
fi


but how can I apply this to the first line?
I thought maybe create a variable of the first line and check if it starts with #include, but i'm not sure how to do this, I tried the read command but I fail to read into a variable.



Beside I'd like to here other approaches to this problem, maybe awk?
Anyway remember I need to check if the first line starts with #include, not if it contain that string.
That's why I found those questions: How to print file content only if the first line matches a certain pattern?
https://stackoverflow.com/questions/5536018/how-to-print-matched-regex-pattern-using-awk
they are not completely helping.










share|improve this question















I need to write a shell script that find and print all files in a directory which starts with the string: #include
Now, I know how to check if a string is in the file, by using:



for f in `ls`; do
if grep -q 'MyString' $f; then:
#DO SOMETHING
fi


but how can I apply this to the first line?
I thought maybe create a variable of the first line and check if it starts with #include, but i'm not sure how to do this, I tried the read command but I fail to read into a variable.



Beside I'd like to here other approaches to this problem, maybe awk?
Anyway remember I need to check if the first line starts with #include, not if it contain that string.
That's why I found those questions: How to print file content only if the first line matches a certain pattern?
https://stackoverflow.com/questions/5536018/how-to-print-matched-regex-pattern-using-awk
they are not completely helping.







shell grep read






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Rui F Ribeiro

38k1475123




38k1475123










asked 2 hours ago









Z E Nir

195




195







  • 1




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    2 hours ago










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    50 mins ago












  • 1




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    2 hours ago










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    50 mins ago







1




1




Tip: don't use ls in scripts, it invariably leads to problems.
– ctrl-alt-delor
2 hours ago




Tip: don't use ls in scripts, it invariably leads to problems.
– ctrl-alt-delor
2 hours ago












I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
– Jeff Schaller
50 mins ago




I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
– Jeff Schaller
50 mins ago










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










It is easy to check if the first line starts with #include in sed:



sed -n '1/^#include/p;q' file


That will have an output only if the file contains #include in the first line. That only needs to read the first line to make the check, so it will be very fast.



So, a shell loop for all files (with sed) should be like this:



for file in *
do
[ "$(sed -n '1/^#include/p;q' "$file")" ] && printf '%sn' "$file"
done


If there are only files (not directories) in the pwd.






share|improve this answer






















  • it prints the name of the file/s, not its content... I just replaced the 'printf' with "cat $file" and it worked! Thanks!
    – Z E Nir
    2 hours ago











  • @ZENir good idea, have you tried it?
    – ctrl-alt-delor
    2 hours ago










  • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
    – Isaac
    1 hour ago










  • Yeah i tried this, thank you both!
    – Z E Nir
    1 hour ago










  • sorry my bad, I miss-read it as a question.
    – ctrl-alt-delor
    1 hour ago

















up vote
2
down vote













for file in *; do
[ -f "$file" ] || continue
IFS= read -r line < "$file" || [ -n "$line" ] || continue
case $line in
("#include"*) printf '%sn' "$file"
esac
done


To print the content of the file instead of its name, replace the printf command with cat < "$file".



If your awk supports the nextfile extension, and you don't care about the potential side effects of opening non-regular files:



awk '/^#include/print substr(FILENAME, 3); nextfile' ./*


With zsh, you can replace ./* with ./*(-.) to only pass regular files (or symlinks to regular files like for the [ -f ... ] approach above) to awk.



Or to print the file contents instead of name:



awk 'FNR == 1 found = /^#include/; found' ./*


(that one is portable).






share|improve this answer






















  • I tried this script, for some reason it does nothing, I have a file starts with '#include' and still nothing printes, If I press 'Enter' nine times i'm returning to the bash
    – Z E Nir
    2 hours ago










  • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
    – Stéphane Chazelas
    1 hour ago

















up vote
1
down vote













for file in *
do
[ -f "$file" ] && head -n 1 < "$file" | grep -q '^#include' && cat < "$file"
done


Beware of the fact that, with -q option enabled, grep will exit with a zero status even if an error occurred.






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',
    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%2f480732%2fhow-to-check-if-the-first-line-of-file-contain-a-specific-string%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    It is easy to check if the first line starts with #include in sed:



    sed -n '1/^#include/p;q' file


    That will have an output only if the file contains #include in the first line. That only needs to read the first line to make the check, so it will be very fast.



    So, a shell loop for all files (with sed) should be like this:



    for file in *
    do
    [ "$(sed -n '1/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


    If there are only files (not directories) in the pwd.






    share|improve this answer






















    • it prints the name of the file/s, not its content... I just replaced the 'printf' with "cat $file" and it worked! Thanks!
      – Z E Nir
      2 hours ago











    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      2 hours ago










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      1 hour ago










    • Yeah i tried this, thank you both!
      – Z E Nir
      1 hour ago










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      1 hour ago














    up vote
    3
    down vote



    accepted










    It is easy to check if the first line starts with #include in sed:



    sed -n '1/^#include/p;q' file


    That will have an output only if the file contains #include in the first line. That only needs to read the first line to make the check, so it will be very fast.



    So, a shell loop for all files (with sed) should be like this:



    for file in *
    do
    [ "$(sed -n '1/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


    If there are only files (not directories) in the pwd.






    share|improve this answer






















    • it prints the name of the file/s, not its content... I just replaced the 'printf' with "cat $file" and it worked! Thanks!
      – Z E Nir
      2 hours ago











    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      2 hours ago










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      1 hour ago










    • Yeah i tried this, thank you both!
      – Z E Nir
      1 hour ago










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      1 hour ago












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    It is easy to check if the first line starts with #include in sed:



    sed -n '1/^#include/p;q' file


    That will have an output only if the file contains #include in the first line. That only needs to read the first line to make the check, so it will be very fast.



    So, a shell loop for all files (with sed) should be like this:



    for file in *
    do
    [ "$(sed -n '1/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


    If there are only files (not directories) in the pwd.






    share|improve this answer














    It is easy to check if the first line starts with #include in sed:



    sed -n '1/^#include/p;q' file


    That will have an output only if the file contains #include in the first line. That only needs to read the first line to make the check, so it will be very fast.



    So, a shell loop for all files (with sed) should be like this:



    for file in *
    do
    [ "$(sed -n '1/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


    If there are only files (not directories) in the pwd.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 2 hours ago









    Isaac

    9,17911342




    9,17911342











    • it prints the name of the file/s, not its content... I just replaced the 'printf' with "cat $file" and it worked! Thanks!
      – Z E Nir
      2 hours ago











    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      2 hours ago










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      1 hour ago










    • Yeah i tried this, thank you both!
      – Z E Nir
      1 hour ago










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      1 hour ago
















    • it prints the name of the file/s, not its content... I just replaced the 'printf' with "cat $file" and it worked! Thanks!
      – Z E Nir
      2 hours ago











    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      2 hours ago










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      1 hour ago










    • Yeah i tried this, thank you both!
      – Z E Nir
      1 hour ago










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      1 hour ago















    it prints the name of the file/s, not its content... I just replaced the 'printf' with "cat $file" and it worked! Thanks!
    – Z E Nir
    2 hours ago





    it prints the name of the file/s, not its content... I just replaced the 'printf' with "cat $file" and it worked! Thanks!
    – Z E Nir
    2 hours ago













    @ZENir good idea, have you tried it?
    – ctrl-alt-delor
    2 hours ago




    @ZENir good idea, have you tried it?
    – ctrl-alt-delor
    2 hours ago












    @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
    – Isaac
    1 hour ago




    @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
    – Isaac
    1 hour ago












    Yeah i tried this, thank you both!
    – Z E Nir
    1 hour ago




    Yeah i tried this, thank you both!
    – Z E Nir
    1 hour ago












    sorry my bad, I miss-read it as a question.
    – ctrl-alt-delor
    1 hour ago




    sorry my bad, I miss-read it as a question.
    – ctrl-alt-delor
    1 hour ago












    up vote
    2
    down vote













    for file in *; do
    [ -f "$file" ] || continue
    IFS= read -r line < "$file" || [ -n "$line" ] || continue
    case $line in
    ("#include"*) printf '%sn' "$file"
    esac
    done


    To print the content of the file instead of its name, replace the printf command with cat < "$file".



    If your awk supports the nextfile extension, and you don't care about the potential side effects of opening non-regular files:



    awk '/^#include/print substr(FILENAME, 3); nextfile' ./*


    With zsh, you can replace ./* with ./*(-.) to only pass regular files (or symlinks to regular files like for the [ -f ... ] approach above) to awk.



    Or to print the file contents instead of name:



    awk 'FNR == 1 found = /^#include/; found' ./*


    (that one is portable).






    share|improve this answer






















    • I tried this script, for some reason it does nothing, I have a file starts with '#include' and still nothing printes, If I press 'Enter' nine times i'm returning to the bash
      – Z E Nir
      2 hours ago










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      1 hour ago














    up vote
    2
    down vote













    for file in *; do
    [ -f "$file" ] || continue
    IFS= read -r line < "$file" || [ -n "$line" ] || continue
    case $line in
    ("#include"*) printf '%sn' "$file"
    esac
    done


    To print the content of the file instead of its name, replace the printf command with cat < "$file".



    If your awk supports the nextfile extension, and you don't care about the potential side effects of opening non-regular files:



    awk '/^#include/print substr(FILENAME, 3); nextfile' ./*


    With zsh, you can replace ./* with ./*(-.) to only pass regular files (or symlinks to regular files like for the [ -f ... ] approach above) to awk.



    Or to print the file contents instead of name:



    awk 'FNR == 1 found = /^#include/; found' ./*


    (that one is portable).






    share|improve this answer






















    • I tried this script, for some reason it does nothing, I have a file starts with '#include' and still nothing printes, If I press 'Enter' nine times i'm returning to the bash
      – Z E Nir
      2 hours ago










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      1 hour ago












    up vote
    2
    down vote










    up vote
    2
    down vote









    for file in *; do
    [ -f "$file" ] || continue
    IFS= read -r line < "$file" || [ -n "$line" ] || continue
    case $line in
    ("#include"*) printf '%sn' "$file"
    esac
    done


    To print the content of the file instead of its name, replace the printf command with cat < "$file".



    If your awk supports the nextfile extension, and you don't care about the potential side effects of opening non-regular files:



    awk '/^#include/print substr(FILENAME, 3); nextfile' ./*


    With zsh, you can replace ./* with ./*(-.) to only pass regular files (or symlinks to regular files like for the [ -f ... ] approach above) to awk.



    Or to print the file contents instead of name:



    awk 'FNR == 1 found = /^#include/; found' ./*


    (that one is portable).






    share|improve this answer














    for file in *; do
    [ -f "$file" ] || continue
    IFS= read -r line < "$file" || [ -n "$line" ] || continue
    case $line in
    ("#include"*) printf '%sn' "$file"
    esac
    done


    To print the content of the file instead of its name, replace the printf command with cat < "$file".



    If your awk supports the nextfile extension, and you don't care about the potential side effects of opening non-regular files:



    awk '/^#include/print substr(FILENAME, 3); nextfile' ./*


    With zsh, you can replace ./* with ./*(-.) to only pass regular files (or symlinks to regular files like for the [ -f ... ] approach above) to awk.



    Or to print the file contents instead of name:



    awk 'FNR == 1 found = /^#include/; found' ./*


    (that one is portable).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 2 hours ago









    Stéphane Chazelas

    292k54544884




    292k54544884











    • I tried this script, for some reason it does nothing, I have a file starts with '#include' and still nothing printes, If I press 'Enter' nine times i'm returning to the bash
      – Z E Nir
      2 hours ago










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      1 hour ago
















    • I tried this script, for some reason it does nothing, I have a file starts with '#include' and still nothing printes, If I press 'Enter' nine times i'm returning to the bash
      – Z E Nir
      2 hours ago










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      1 hour ago















    I tried this script, for some reason it does nothing, I have a file starts with '#include' and still nothing printes, If I press 'Enter' nine times i'm returning to the bash
    – Z E Nir
    2 hours ago




    I tried this script, for some reason it does nothing, I have a file starts with '#include' and still nothing printes, If I press 'Enter' nine times i'm returning to the bash
    – Z E Nir
    2 hours ago












    @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
    – Stéphane Chazelas
    1 hour ago




    @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
    – Stéphane Chazelas
    1 hour ago










    up vote
    1
    down vote













    for file in *
    do
    [ -f "$file" ] && head -n 1 < "$file" | grep -q '^#include' && cat < "$file"
    done


    Beware of the fact that, with -q option enabled, grep will exit with a zero status even if an error occurred.






    share|improve this answer


























      up vote
      1
      down vote













      for file in *
      do
      [ -f "$file" ] && head -n 1 < "$file" | grep -q '^#include' && cat < "$file"
      done


      Beware of the fact that, with -q option enabled, grep will exit with a zero status even if an error occurred.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        for file in *
        do
        [ -f "$file" ] && head -n 1 < "$file" | grep -q '^#include' && cat < "$file"
        done


        Beware of the fact that, with -q option enabled, grep will exit with a zero status even if an error occurred.






        share|improve this answer














        for file in *
        do
        [ -f "$file" ] && head -n 1 < "$file" | grep -q '^#include' && cat < "$file"
        done


        Beware of the fact that, with -q option enabled, grep will exit with a zero status even if an error occurred.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 13 mins ago









        Stéphane Chazelas

        292k54544884




        292k54544884










        answered 2 hours ago









        francescop21

        18319




        18319



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480732%2fhow-to-check-if-the-first-line-of-file-contain-a-specific-string%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery