LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   check if line containing string is commented (https://www.linuxquestions.org/questions/linux-software-2/check-if-line-containing-string-is-commented-4175593396/)

aristosv 11-12-2016 11:54 AM

check if line containing string is commented
 
I need to create a bash script that searches through a file, finds a word, and if that line containing this word is commented, echoes it.

I imagine it would be something like this, but I don't know exactly how to do it.

Quote:

grep /home/user/filename.txt
if (line containing string = commented)
then
echo line containing string is commented
else
echo line containing string is not commented
fi

pan64 11-12-2016 12:17 PM

what do you mean by "commented"?

aristosv 11-12-2016 12:19 PM

Sorry I wasn't clear. By commented I mean to have a # at the start.

pan64 11-12-2016 01:06 PM

so you need to find lines beginning with # and containing a specific word?
A single grep command can be do that, you just need to be familiar with it:
Code:

grep <pattern> filename
is the basic syntax you need to know. Filename is known, so only pattern should be constructed.
^ means the beginning of the line, so: grep ^# filename will look for lines beginning with #. grep word filename will look for lines containing word. You may need to combine this to get the required result and also may take into account if word contains special chars. See man grep about regular expressions understood...

aristosv 11-12-2016 01:43 PM

this helped. Thanks


All times are GMT -5. The time now is 02:38 AM.