how to check if the first line of file contain a specific string?
Clash 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.
shell grep read
add a comment |
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.
shell grep read
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
add a comment |
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.
shell grep read
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
shell grep read
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
add a comment |
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
add a comment |
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.
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
add a comment |
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).
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
add a comment |
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.
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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).
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
add a comment |
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).
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
add a comment |
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).
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).
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 13 mins ago


Stéphane Chazelas
292k54544884
292k54544884
answered 2 hours ago
francescop21
18319
18319
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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