LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-31-2012, 12:02 AM   #1
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 171

Rep: Reputation: 15
commenting a line with echo command


Hi Friends and Gurus,

I want to edit a crontab file with the help echo command.

we are using a tool called HP Opsware
opsware is a GUI tool which connects to UNIX servers with root permissions.

here my query is just assume i have a file called test.txt
here i want to comment the 9th line, i mean put a hash on 9th line with the help of echo command.


host1#cat -n test
cat -n test
1 This is a test file
2 This is a test file
3 This is a test file
4 This is a test file
5 This is a test file
6 This is a test guava
7 This is a test apple
8 This is a test mango
9 This is a test orange
10 This is a test anar.


any help is appreciated....


Thanks & Regards
JNReddy
 
Old 03-31-2012, 12:35 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Why do you want to do it with echo when sed is ideally suited to the task?
 
Old 03-31-2012, 01:37 AM   #3
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 171

Original Poster
Rep: Reputation: 15
Hi Catkin

hi Catkin

Thank you for your reply i dont know how to use the sed.can you please provide me command to fullfill my task.

Thanks & Regards
JNReddy
 
Old 03-31-2012, 02:56 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Assuming you know which line you need to comment out (line 9 in your example:
Code:
sed '9s/^/#/' infile
The 9 tells sed which line to work on. The s/^/#/ tells sed to (re)place a # at the beginning (the ^) of the line.

After you check if this is correct use sed's -i option to chance the infile:
Code:
sed -i '9s/^/#/' infile
Also have a look here: GNU sed, a stream editor and USEFUL ONE-LINE SCRIPTS FOR SED

Hope this helps.
 
1 members found this post helpful.
Old 03-31-2012, 03:03 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
You will learn much more by looking at the references offered (at the end) than the answer.

Last edited by syg00; 03-31-2012 at 03:04 AM.
 
Old 03-31-2012, 04:18 AM   #6
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 171

Original Poster
Rep: Reputation: 15
Thank you Druuna

I can able to comment the 9th line, but its not making changes in the original file

$sed '9s/^/#/' test
This is a test apple
This is a test banana
This is a test orange
This is a test apple
This is a test apple
This is a test apple
This is a test apple
This is a test apple
#This is a test apple
This is a test apple

orly$cat test
This is a test apple
This is a test banana
This is a test orange
This is a test apple
This is a test apple
This is a test apple
This is a test apple
This is a test apple
This is a test apple
This is a test apple

I redirected the output to another file, it would be great if it make changes to original file..sed '9s/^/#/' test > test1
orly$sed '9s/^/#/' test > test1
1 This is a test apple
2 This is a test banana
3 This is a test orange
4 This is a test apple
5 This is a test apple
6 This is a test apple
7 This is a test apple
8 This is a test apple
9 #This is a test apple
10 This is a test apple


Thanks & Regards
JNReddy
 
Old 03-31-2012, 04:23 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by jnreddy View Post
I can able to comment the 9th line, but its not making changes in the original file ......
That is why I included the comment about using the -i option....
 
Old 03-31-2012, 01:13 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
For the record, the vast majority of command line tools are unable to alter the original input, and only print the modified results to stdout. You must use a temporary file in most cases.

Even the -i option above is just an extension added to the gnu version of sed, and not available in other implementations. And all it does is use a temporary file in the background anyway.


If you really want to do script-based direct editing of files, check out ed.

http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed
 
Old 04-03-2012, 12:19 PM   #9
ae.vasconcelos
LQ Newbie
 
Registered: Mar 2012
Location: Portugal
Distribution: Fedora / CentOS
Posts: 8

Rep: Reputation: Disabled
There is nothing wrong in using the mighty vi to edit a file

$ echo ":9^Mi#^[ZZ" | vi file.txt

This edit file file.txt and add a # to the beginning of line 9 every time it's run.

Note that ^M is typed as [CTRL-V ENTER] and ^[ as [CTRL-V ESQ]
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo two command output in the same line chuikingman Linux - Software 11 05-06-2019 10:44 AM
[SOLVED] how to echo an '-n' without a new line? paliga Linux - Newbie 8 10-20-2010 07:44 AM
Command Line Expansion & echo Merrida Fedora 4 05-26-2009 12:30 AM
commenting more than one line in VI editor newbloggy Linux - Software 1 04-18-2006 07:09 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:42 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