LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-02-2014, 01:00 PM   #1
jayadhanesh
Member
 
Registered: Feb 2009
Location: Bangalore
Posts: 61

Rep: Reputation: 15
Adding a comment in front of a line and before the line


Hi,

I have a line in a file, which I want to comment by adding # in front of it and also add a comment before the line. Its a bash env and want a script to do it.

Thanks,
D
 
Old 04-02-2014, 01:10 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
What exactly you want to do? If you will a # before a line then that line will not be executed by bash. Comments should be added after the line by adding a # before the line. Meanwhile, you can add as many as comments by adding a # at starting of the line. For example,
Code:
#!/bin/bash
# This is my script
# and it will list out
# all files present 
# in the working directory.
command1
command2
....
 
Old 04-02-2014, 10:20 PM   #3
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mint, OpenBSD
Posts: 11,340
Blog Entries: 12

Rep: Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730Reputation: 2730
You can also add comments at the end of the line:

Code:
command1  # this command does foo
command2  # this command moves the output of foo to bar
You cannot have comments at the beginning of a line and have the line execute.
 
Old 04-03-2014, 04:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Without an example we will all just be guessing. I believe a before and after would aide everyone in answering your question much better.
 
Old 04-03-2014, 04:14 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
A tool to edit the file from the command line or in a script is sed. For example you can add something at the beginning of a line using
Code:
sed 's/^/# /' file
or you can insert something before a specific line given the line number by means of
Code:
sed '3i# new comment line' file
Two options that come in handy are -i to actually change the content of the file (otherwise the result of the changes is sent to the standard output) and -r to use extended regular expressions. Knowledge about how regular expressions work is required and reading a good sed tutorial is strongly advised

See: http://www.grymoire.com/Unix/sed.html
 
Old 04-03-2014, 04:15 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 804

Rep: Reputation: 186Reputation: 186
I would use awk.

Case_1. Write a comment character (#) at the start of the line.
If any portion of the line ($0 in awk) matches what you want (enclose in // pair), then write # and then the line.
Else write the line.

Case_2. Write a comment line(s) before the line.
If any portion of the line ($0 in awk) has what you want (enclose in // pair), then write the comment line(s) (starting with #) and then the line.
Else write the line.

I refer you to:
http://www.gnu.org/software/gawk/manual/gawk.html and in particular.
http://www.gnu.org/software/gawk/man...wk.html#Regexp


OK

Last edited by AnanthaP; 04-03-2014 at 04:22 AM.
 
  


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] Adding a character in each line depending upon number of such characters present in a line. ls_milkyway Linux - Newbie 13 08-24-2013 04:19 AM
[SOLVED] adding new line after each line in perl KManepalli Linux - Newbie 9 04-28-2011 09:02 AM
[SOLVED] adding line from file1 into a line of another file based on maching IDs rossk Programming 6 01-06-2011 01:06 AM
search for a pattern and comment the line swaroop.tata Programming 2 09-04-2009 08:23 PM
how to recognize a multiple line comment ? cyu021 Programming 2 04-07-2005 10:44 AM


All times are GMT -5. The time now is 10:33 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration