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.
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 '18 at 19:31