LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-15-2016, 03:47 AM   #1
zeusys
Member
 
Registered: Jun 2006
Posts: 31

Rep: Reputation: 0
Question Comment all lines start with a specific pattern


Hi.
Assume you have a file like this :
Code:
#!/bin/bash
echo "<HTML>"
echo "<HEAD>"
echo "  <TITLE>"
echo "  The title of your page"
echo "  </TITLE>"
echo "</HEAD>"
echo ""
echo "<BODY>"
echo "  Contents"
echo "</BODY>"
echo "</HTML>"
Then you want to comment all of the lines which start with a specific pattern, in this example how to comment all lines start with echo command? In Vi,using sed,awk or how to ?

Please guide me
Thanks
 
Old 02-15-2016, 03:55 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
have you tried sed or awk already? Where did you stuck?
 
Old 02-15-2016, 04:46 AM   #3
zeusys
Member
 
Registered: Jun 2006
Posts: 31

Original Poster
Rep: Reputation: 0
I don't know who should I do that? either using awk or sed?
 
Old 02-15-2016, 05:00 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
In Vim:
Code:
:%s/^echo/#echo/g
The string you would use for sed should look pretty similar.
 
Old 02-15-2016, 05:53 AM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
Using sed, you specify that you only wish to change lines starting with echo by using the regular expression "^echo", where the circumflex character matches the start of a line.
This will output to your screen.
Code:
sed 's/^echo/#echo/' <your file>
To redirect the output to a new file.
Code:
sed 's/^echo/#echo/' <your file> > <new file>
Adding the -i option will change the original file.
Code:
sed -i 's/^echo/#echo/' <your file>
An alternative is to use a backreference, which is trivial here, but often powerful.
Code:
sed 's/^\(echo\)/#\1/' <your file>

Last edited by allend; 02-15-2016 at 06:25 AM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help inserting 2 lines into a file following a specific pattern rmori Linux - Newbie 6 10-06-2014 08:23 PM
Printing $ Lines After Finding Specific Pattern pravin.bhande Programming 6 04-12-2012 10:20 AM
Find pattern and comment out 2 lines after it ariszlo Linux - Newbie 3 10-14-2010 05:37 AM
deleting lines from a file with specific pattern using AWK gandhigaurav1986 Programming 12 06-08-2010 02:08 AM
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 01:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:01 AM.

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