Linux filter commented file content

In the Linux system, you can use the grep command combined with regular expressions to filter out the commented out parts of the file content.
The specific method is as follows:

1. Use the "-v" parameter of grep to reverse the selection and filter out the comment lines.

For example to filter comments in python files:

grep -v "^#" python_file.py

2. Use regular expressions to match comment symbols, and then filter.

Filter comments in C language files:

grep -v "^/\*" file.c | grep -v "\*/"

or:

grep -v "\/\*" file.c | grep -v "\*\/" 

3. For comments in shell scripts, you can filter to include "#":

grep -v "^#" shell.sh

4. For sql statement comments starting with ";", you can use:

grep -v "^;" sql.txt

5. You can also use the delete function of the sed editor:

sed '/^\/\*/,/\*\//d' file.c 
sed '/^--/d' file

Guess you like

Origin blog.csdn.net/qq_44534541/article/details/131873965