list file and using awk to get comment

0

I want to get the all comment of all files in a folder.

And what I have tried is

ls -R | awk '/<!--/,/-->/' >> result

But it shows nothing in the result file.

But if I use the awk '/<!--/,/-->/' >> result to a file it works.

The html comment tag is <!-- some text here -->.

How should I fix it.

Jason

Posted 2015-03-27T19:04:25.880

Reputation: 105

Answers

1

Your awk is looking at the file names and not at the file contents. Here is a better approach:

find ./ -type f -execdir grep "<!--.*-->" "{}" +

This uses find to get a list of all the files and then feeds these into grep which searches for the text you are seeking. It will then output the name of any matching file followed by a colon (:) and the text of the matched line.

gogoud

Posted 2015-03-27T19:04:25.880

Reputation: 1 068

1

ls -R will list only the file name not a file contents. You can do this in a more simple way using grep command.

grep -rn '<!--.*-->'

Or if want to do this using awk then you can use cat * to list all file content in a directory.

cat * | awk '/<!--/,/-->' 

Note: This approach works only if your directory contains files.

If you want to list all content in all directory. You can try this

find . -type f | xargs cat | awk '/<!--/,/-->/'

Vengat

Posted 2015-03-27T19:04:25.880

Reputation: 17