LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-12-2016, 11:54 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 215

Rep: Reputation: 3
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
 
Old 11-12-2016, 12:17 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,500

Rep: Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535
what do you mean by "commented"?
 
Old 11-12-2016, 12:19 PM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 215

Original Poster
Rep: Reputation: 3
Sorry I wasn't clear. By commented I mean to have a # at the start.
 
Old 11-12-2016, 01:06 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,500

Rep: Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535
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...
 
1 members found this post helpful.
Old 11-12-2016, 01:43 PM   #5
aristosv
Member
 
Registered: Dec 2014
Posts: 215

Original Poster
Rep: Reputation: 3
this helped. Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Check Multiple line string exists in a file Arun Shankar Programming 12 09-30-2013 07:27 AM
read line from a file and check the string rajachan Programming 2 01-18-2013 03:23 AM
src2pkg fails, no install line given even though fake_install is commented out agentc0re Slackware 2 02-13-2008 05:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration