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











up vote
4
down vote

favorite













This question already has an answer here:




  • How to print file content only if the first line matches a certain pattern?

    5 answers




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 to 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.



I'd like to hear other approaches to this problem; maybe awk?
Anyway, remember, I need to check if the first line starts with #include, not if it contains 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















marked as duplicate by don_crissti, elbarna, RalfFriedl, mosvy, G-Man Nov 11 at 23:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 5




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    Nov 9 at 10:02










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    Nov 9 at 11:28










  • @ctrl-alt-delor so I shall use "in *" instead? no problem... but what kind of problem 'ls' can cause?
    – Z E Nir
    Nov 10 at 15:55










  • It is just that ls is hard to parse, it was not designed for the computer to read, but for humans. It is often used when not needed. There are other tools: just that are designed for the job: *, find, …
    – ctrl-alt-delor
    Nov 10 at 19:31












  • @ctrl-alt-delor Thanks!
    – Z E Nir
    Nov 11 at 9:12















up vote
4
down vote

favorite













This question already has an answer here:




  • How to print file content only if the first line matches a certain pattern?

    5 answers




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 to 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.



I'd like to hear other approaches to this problem; maybe awk?
Anyway, remember, I need to check if the first line starts with #include, not if it contains 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















marked as duplicate by don_crissti, elbarna, RalfFriedl, mosvy, G-Man Nov 11 at 23:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 5




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    Nov 9 at 10:02










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    Nov 9 at 11:28










  • @ctrl-alt-delor so I shall use "in *" instead? no problem... but what kind of problem 'ls' can cause?
    – Z E Nir
    Nov 10 at 15:55










  • It is just that ls is hard to parse, it was not designed for the computer to read, but for humans. It is often used when not needed. There are other tools: just that are designed for the job: *, find, …
    – ctrl-alt-delor
    Nov 10 at 19:31












  • @ctrl-alt-delor Thanks!
    – Z E Nir
    Nov 11 at 9:12













up vote
4
down vote

favorite









up vote
4
down vote

favorite












This question already has an answer here:




  • How to print file content only if the first line matches a certain pattern?

    5 answers




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 to 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.



I'd like to hear other approaches to this problem; maybe awk?
Anyway, remember, I need to check if the first line starts with #include, not if it contains 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
















This question already has an answer here:




  • How to print file content only if the first line matches a certain pattern?

    5 answers




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 to 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.



I'd like to hear other approaches to this problem; maybe awk?
Anyway, remember, I need to check if the first line starts with #include, not if it contains 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.





This question already has an answer here:




  • How to print file content only if the first line matches a certain pattern?

    5 answers








shell grep read






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 17:16









Jeff Schaller

36.3k952120




36.3k952120










asked Nov 9 at 9:33









Z E Nir

2915




2915




marked as duplicate by don_crissti, elbarna, RalfFriedl, mosvy, G-Man Nov 11 at 23:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by don_crissti, elbarna, RalfFriedl, mosvy, G-Man Nov 11 at 23:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 5




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    Nov 9 at 10:02










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    Nov 9 at 11:28










  • @ctrl-alt-delor so I shall use "in *" instead? no problem... but what kind of problem 'ls' can cause?
    – Z E Nir
    Nov 10 at 15:55










  • It is just that ls is hard to parse, it was not designed for the computer to read, but for humans. It is often used when not needed. There are other tools: just that are designed for the job: *, find, …
    – ctrl-alt-delor
    Nov 10 at 19:31












  • @ctrl-alt-delor Thanks!
    – Z E Nir
    Nov 11 at 9:12














  • 5




    Tip: don't use ls in scripts, it invariably leads to problems.
    – ctrl-alt-delor
    Nov 9 at 10:02










  • I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
    – Jeff Schaller
    Nov 9 at 11:28










  • @ctrl-alt-delor so I shall use "in *" instead? no problem... but what kind of problem 'ls' can cause?
    – Z E Nir
    Nov 10 at 15:55










  • It is just that ls is hard to parse, it was not designed for the computer to read, but for humans. It is often used when not needed. There are other tools: just that are designed for the job: *, find, …
    – ctrl-alt-delor
    Nov 10 at 19:31












  • @ctrl-alt-delor Thanks!
    – Z E Nir
    Nov 11 at 9:12








5




5




Tip: don't use ls in scripts, it invariably leads to problems.
– ctrl-alt-delor
Nov 9 at 10:02




Tip: don't use ls in scripts, it invariably leads to problems.
– ctrl-alt-delor
Nov 9 at 10:02












I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
– Jeff Schaller
Nov 9 at 11:28




I think unix.stackexchange.com/a/232655/117549 is reasonably close -- just anchor the pattern.
– Jeff Schaller
Nov 9 at 11:28












@ctrl-alt-delor so I shall use "in *" instead? no problem... but what kind of problem 'ls' can cause?
– Z E Nir
Nov 10 at 15:55




@ctrl-alt-delor so I shall use "in *" instead? no problem... but what kind of problem 'ls' can cause?
– Z E Nir
Nov 10 at 15:55












It is just that ls is hard to parse, it was not designed for the computer to read, but for humans. It is often used when not needed. There are other tools: just that are designed for the job: *, find, …
– ctrl-alt-delor
Nov 10 at 19:31






It is just that ls is hard to parse, it was not designed for the computer to read, but for humans. It is often used when not needed. There are other tools: just that are designed for the job: *, find, …
– ctrl-alt-delor
Nov 10 at 19:31














@ctrl-alt-delor Thanks!
– Z E Nir
Nov 11 at 9:12




@ctrl-alt-delor Thanks!
– Z E Nir
Nov 11 at 9:12










4 Answers
4






active

oldest

votes

















up vote
7
down vote



accepted










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



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


Or simplified:



sed -n '/^#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 '/^#include/p;q' "$file")" ] && printf '%sn' "$file"
done


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



If what you need is to print all lines of the file, a solution similar to the first code posted will work:



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





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
    Nov 9 at 9:58












  • @ZENir good idea, have you tried it?
    – ctrl-alt-delor
    Nov 9 at 10:04










  • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
    – Isaac
    Nov 9 at 10:20










  • Yeah i tried this, thank you both!
    – Z E Nir
    Nov 9 at 10:24










  • sorry my bad, I miss-read it as a question.
    – ctrl-alt-delor
    Nov 9 at 10:25


















up vote
5
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
    Nov 9 at 9:53










  • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
    – Stéphane Chazelas
    Nov 9 at 10:44


















up vote
2
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
    0
    down vote













    This question is a perfect example where bash and sed solutions are quite complex, but the task can be much simpler with (GNU) awk:



    gawk 'FNR==1 && /^#include/{print FILENAME}{nextfile}' *





    share|improve this answer





















    • A similar solution has already been given. Yours has issues with file names that contain = characters. Note that a few other awk implementations beside GNU awk support nextfile these days. Here, because you're using FNR == 1, it would also work in implementations that don't support nextfile.
      – Stéphane Chazelas
      Nov 9 at 17:14












    • @StéphaneChazelas You're right, I read your answer too fast and didn't notice your awk examples, sorry about that
      – user000001
      Nov 9 at 18:29


















    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote



    accepted










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



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


    Or simplified:



    sed -n '/^#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 '/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


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



    If what you need is to print all lines of the file, a solution similar to the first code posted will work:



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





    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
      Nov 9 at 9:58












    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      Nov 9 at 10:04










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      Nov 9 at 10:20










    • Yeah i tried this, thank you both!
      – Z E Nir
      Nov 9 at 10:24










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      Nov 9 at 10:25















    up vote
    7
    down vote



    accepted










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



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


    Or simplified:



    sed -n '/^#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 '/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


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



    If what you need is to print all lines of the file, a solution similar to the first code posted will work:



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





    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
      Nov 9 at 9:58












    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      Nov 9 at 10:04










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      Nov 9 at 10:20










    • Yeah i tried this, thank you both!
      – Z E Nir
      Nov 9 at 10:24










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      Nov 9 at 10:25













    up vote
    7
    down vote



    accepted







    up vote
    7
    down vote



    accepted






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



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


    Or simplified:



    sed -n '/^#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 '/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


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



    If what you need is to print all lines of the file, a solution similar to the first code posted will work:



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





    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


    Or simplified:



    sed -n '/^#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 '/^#include/p;q' "$file")" ] && printf '%sn' "$file"
    done


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



    If what you need is to print all lines of the file, a solution similar to the first code posted will work:



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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 1:54

























    answered Nov 9 at 9:53









    Isaac

    9,68611445




    9,68611445












    • 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
      Nov 9 at 9:58












    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      Nov 9 at 10:04










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      Nov 9 at 10:20










    • Yeah i tried this, thank you both!
      – Z E Nir
      Nov 9 at 10:24










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      Nov 9 at 10:25


















    • 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
      Nov 9 at 9:58












    • @ZENir good idea, have you tried it?
      – ctrl-alt-delor
      Nov 9 at 10:04










    • @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
      – Isaac
      Nov 9 at 10:20










    • Yeah i tried this, thank you both!
      – Z E Nir
      Nov 9 at 10:24










    • sorry my bad, I miss-read it as a question.
      – ctrl-alt-delor
      Nov 9 at 10:25
















    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
    Nov 9 at 9:58






    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
    Nov 9 at 9:58














    @ZENir good idea, have you tried it?
    – ctrl-alt-delor
    Nov 9 at 10:04




    @ZENir good idea, have you tried it?
    – ctrl-alt-delor
    Nov 9 at 10:04












    @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
    – Isaac
    Nov 9 at 10:20




    @ctrl-alt-delor Yes, it seems that he tried it: He have just said: …and it worked!.
    – Isaac
    Nov 9 at 10:20












    Yeah i tried this, thank you both!
    – Z E Nir
    Nov 9 at 10:24




    Yeah i tried this, thank you both!
    – Z E Nir
    Nov 9 at 10:24












    sorry my bad, I miss-read it as a question.
    – ctrl-alt-delor
    Nov 9 at 10:25




    sorry my bad, I miss-read it as a question.
    – ctrl-alt-delor
    Nov 9 at 10:25












    up vote
    5
    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
      Nov 9 at 9:53










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      Nov 9 at 10:44















    up vote
    5
    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
      Nov 9 at 9:53










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      Nov 9 at 10:44













    up vote
    5
    down vote










    up vote
    5
    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 Nov 9 at 10:47

























    answered Nov 9 at 9:41









    Stéphane Chazelas

    294k54554897




    294k54554897












    • 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
      Nov 9 at 9:53










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      Nov 9 at 10:44


















    • 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
      Nov 9 at 9:53










    • @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
      – Stéphane Chazelas
      Nov 9 at 10:44
















    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
    Nov 9 at 9:53




    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
    Nov 9 at 9:53












    @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
    – Stéphane Chazelas
    Nov 9 at 10:44




    @ZENir, in my initial version of the script, I had forgotten the < "$file". Try reloading the page.
    – Stéphane Chazelas
    Nov 9 at 10:44










    up vote
    2
    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
      2
      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
        2
        down vote










        up vote
        2
        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 Nov 9 at 12:05









        Stéphane Chazelas

        294k54554897




        294k54554897










        answered Nov 9 at 9:56









        francescop21

        213111




        213111






















            up vote
            0
            down vote













            This question is a perfect example where bash and sed solutions are quite complex, but the task can be much simpler with (GNU) awk:



            gawk 'FNR==1 && /^#include/{print FILENAME}{nextfile}' *





            share|improve this answer





















            • A similar solution has already been given. Yours has issues with file names that contain = characters. Note that a few other awk implementations beside GNU awk support nextfile these days. Here, because you're using FNR == 1, it would also work in implementations that don't support nextfile.
              – Stéphane Chazelas
              Nov 9 at 17:14












            • @StéphaneChazelas You're right, I read your answer too fast and didn't notice your awk examples, sorry about that
              – user000001
              Nov 9 at 18:29















            up vote
            0
            down vote













            This question is a perfect example where bash and sed solutions are quite complex, but the task can be much simpler with (GNU) awk:



            gawk 'FNR==1 && /^#include/{print FILENAME}{nextfile}' *





            share|improve this answer





















            • A similar solution has already been given. Yours has issues with file names that contain = characters. Note that a few other awk implementations beside GNU awk support nextfile these days. Here, because you're using FNR == 1, it would also work in implementations that don't support nextfile.
              – Stéphane Chazelas
              Nov 9 at 17:14












            • @StéphaneChazelas You're right, I read your answer too fast and didn't notice your awk examples, sorry about that
              – user000001
              Nov 9 at 18:29













            up vote
            0
            down vote










            up vote
            0
            down vote









            This question is a perfect example where bash and sed solutions are quite complex, but the task can be much simpler with (GNU) awk:



            gawk 'FNR==1 && /^#include/{print FILENAME}{nextfile}' *





            share|improve this answer












            This question is a perfect example where bash and sed solutions are quite complex, but the task can be much simpler with (GNU) awk:



            gawk 'FNR==1 && /^#include/{print FILENAME}{nextfile}' *






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 17:04









            user000001

            842613




            842613












            • A similar solution has already been given. Yours has issues with file names that contain = characters. Note that a few other awk implementations beside GNU awk support nextfile these days. Here, because you're using FNR == 1, it would also work in implementations that don't support nextfile.
              – Stéphane Chazelas
              Nov 9 at 17:14












            • @StéphaneChazelas You're right, I read your answer too fast and didn't notice your awk examples, sorry about that
              – user000001
              Nov 9 at 18:29


















            • A similar solution has already been given. Yours has issues with file names that contain = characters. Note that a few other awk implementations beside GNU awk support nextfile these days. Here, because you're using FNR == 1, it would also work in implementations that don't support nextfile.
              – Stéphane Chazelas
              Nov 9 at 17:14












            • @StéphaneChazelas You're right, I read your answer too fast and didn't notice your awk examples, sorry about that
              – user000001
              Nov 9 at 18:29
















            A similar solution has already been given. Yours has issues with file names that contain = characters. Note that a few other awk implementations beside GNU awk support nextfile these days. Here, because you're using FNR == 1, it would also work in implementations that don't support nextfile.
            – Stéphane Chazelas
            Nov 9 at 17:14






            A similar solution has already been given. Yours has issues with file names that contain = characters. Note that a few other awk implementations beside GNU awk support nextfile these days. Here, because you're using FNR == 1, it would also work in implementations that don't support nextfile.
            – Stéphane Chazelas
            Nov 9 at 17:14














            @StéphaneChazelas You're right, I read your answer too fast and didn't notice your awk examples, sorry about that
            – user000001
            Nov 9 at 18:29




            @StéphaneChazelas You're right, I read your answer too fast and didn't notice your awk examples, sorry about that
            – user000001
            Nov 9 at 18:29



            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?