LinuxQuestions.org
Help answer threads with 0 replies.
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 06-22-2004, 03:19 PM   #1
stakhous
Member
 
Registered: May 2003
Location: PA
Posts: 82

Rep: Reputation: 15
modifying files with "echo"


Is there a way to modify selected lines in a file with the echo command??

For instance i'm trying to comment out a number of lines in my twpol.txt file, and it would be easier just to run echo appropriately instead of looking through the file for the designated lines.

I tried commands such as,

cat twpol.txt | grep /bin/ksh | echo # >> *

With no avail. I'm sure i'm way off w/ the above command.

Thanks
 
Old 06-22-2004, 03:25 PM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
Read man echo and info coreutils echo. Echo is used to simply display text not to make changes.
 
Old 06-22-2004, 03:49 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Re: modifying files with "echo"

Quote:
Originally posted by stakhous
Is there a way to modify selected lines in a file with the echo command??

For instance i'm trying to comment out a number of lines in my twpol.txt file, and it would be easier just to run echo appropriately instead of looking through the file for the designated lines.

I tried commands such as,

cat twpol.txt | grep /bin/ksh | echo # >> *

With no avail. I'm sure i'm way off w/ the above command.

Thanks
The tool you're after is sed...



Cheers,
Tink
 
Old 06-22-2004, 04:03 PM   #4
stakhous
Member
 
Registered: May 2003
Location: PA
Posts: 82

Original Poster
Rep: Reputation: 15
alright thanks tinkster.

So...

echo "Test" > file.txt or echo "" > /var/log/message

cant modify files? Im thinking it can


I just checked the sed manual pages, well...too intricate for such a simple task if you ask me. But i'll keep reading. Any other ways though?

Last edited by stakhous; 06-22-2004 at 04:05 PM.
 
Old 06-22-2004, 04:04 PM   #5
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
Try it and see what happens.

Then read the 2 pages i gave you and then read man sed.
 
Old 06-22-2004, 04:06 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally posted by stakhous

So...

echo "Test" > file.txt or echo "" > /var/log/message

cant modify files? Im thinking it can
It will modify it to the extent that you will have a empty file,
or one with the word Test in it. The thing is that you can't
point echo to overwrite a specific line.

If you wish you can APPEND a line using
echo "" >> /var/log/messages



Cheers,
Tink

Last edited by Tinkster; 06-22-2004 at 04:08 PM.
 
Old 06-22-2004, 04:10 PM   #7
stakhous
Member
 
Registered: May 2003
Location: PA
Posts: 82

Original Poster
Rep: Reputation: 15
Alright it seems like coreutils might work, but i have a lot of reading to do it seems. (What else is new)

I have another question.

I want to add /usr/sbin to certain user's path variables.

/etc/profile doesnt do the job

/home/<USER>/.bash-profile will not work


Any ideas? Should i just re set it myself w/ "set $PATH=blahblahblah:/sbin" in the appropriate file???

thanks again fellas


Last edited by stakhous; 06-22-2004 at 04:11 PM.
 
Old 06-22-2004, 04:13 PM   #8
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
Hi!

In order to modify lines in a file, you should really use sed. As XavierP said, echo is not the command you want to use here.

Example:
Code:
sed -e 's/^\/bin\/ksh/#&/' file > newfile
In this example,
^ represents beginning of line
\/bin\/ksh is the pattern you are looking for (making the / litteral)
# is the character you want to include in front of the line
& is your previously matched pattern

All these commands are done on file and redirected to newfile
 
Old 06-22-2004, 04:15 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally posted by stakhous
/home/<USER>/.bash-profile will not work
Try
Code:
/home/<USER>/.bash_profile
or, if the shell is not a login-shell
try
Code:
/home/<USER>/.bashrc

Cheers,
Tink
 
Old 06-22-2004, 04:18 PM   #10
stakhous
Member
 
Registered: May 2003
Location: PA
Posts: 82

Original Poster
Rep: Reputation: 15
Alright thanks for the help everyone. And the second one worked Tink. And arobic you saved me alot of reading

Thank you everyone for such quick replies and patience for my newb questions.

Cheers
 
  


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
What does echo alias "char-major-10-224 off" >> /etc/modules.conf do? abefroman Linux - Software 2 09-16-2009 11:10 PM
simple "echo > qbc" cron jobs doesn't run shole Linux - Newbie 3 09-23-2006 03:09 PM
(echo -n "standby" > /sys/power/state) only allowed for root? brynjarh Debian 5 02-14-2005 10:08 AM
want full description of "echo num > / path/...." zameer_india Linux - Newbie 5 01-08-2005 04:10 AM
"X-MS" cant open because "x-Multimedia System" cant access files at "smb&qu ponchy5 Linux - Networking 0 03-30-2004 12:18 AM


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