LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 01-15-2014, 01:05 AM   #1
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Rep: Reputation: Disabled
edit file using echo command


i wanna edit a file using echo command.
suppose i wanna edit ntp.conf file and i wanna put # in front of some consicutive lines and wanna edit some line.
let say i wanna edit

Code:
server ipaddress
to

Code:
server some_other_ipaddress
 
Old 01-15-2014, 01:13 AM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Why do you need to use the echo command? Wouldn't it be simpler to use a terminal editor like emacs or nano? A command like this should do the job.
Code:
sudo nano /etc/ntp.conf
Have fun.
jdk
 
Old 01-15-2014, 01:18 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

IMHO, echo is not the tool for the job. You can rewrite the file using echo but you can't really edit it. Can you explain why you want to use echo in this way?

Evo2.

PS. Best use "want to" instead of "wanna". It's not actually a word and comes across as whining - especially when used repeatedly.
 
Old 01-15-2014, 01:20 AM   #4
dob12460
Red Hat Content Author
 
Registered: Nov 2007
Location: Brisbane, Australia
Distribution: RHEL, Fedora
Posts: 32

Rep: Reputation: 4
I'm no expert, but echo is not an editor.

Have you tried using sed?

sed -i -e s/ipaddress/otheripaddress/g <filename>
 
2 members found this post helpful.
Old 01-15-2014, 01:21 AM   #5
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,

IMHO, echo is not the tool for the job. You can rewrite the file using echo but you can't really edit it. Can you explain why you want to use echo in this way?

Evo2.

PS. Best use "want to" instead of "wanna". It's not actually a word and comes across as whining - especially when used repeatedly.
Actually i wanna write post script in kickstart file and there i wanna edit ntp.conf file and wanna write our ntp server ip in server directive and i wanna comment other server directive. how this can be achived using post script in kickstart?
 
Old 01-15-2014, 01:35 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

sorry I'm not familiar with "post script". Can you use sed as suggested by dob12460?

Evo2.

PS. I was not joking about using "wanna".
 
Old 01-15-2014, 01:48 AM   #7
dob12460
Red Hat Content Author
 
Registered: Nov 2007
Location: Brisbane, Australia
Distribution: RHEL, Fedora
Posts: 32

Rep: Reputation: 4
If you're referring to the %post section of a .ks file you can use sed, as I mentioned.

And as evo2 has mentioned twice, it is best not to use "wanna" repeatedly. It comes across very poorly. Definitely recommend using "want to".

David
 
Old 01-15-2014, 02:11 AM   #8
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dob12460 View Post
If you're referring to the %post section of a .ks file you can use sed, as I mentioned.

And as evo2 has mentioned twice, it is best not to use "wanna" repeatedly. It comes across very poorly. Definitely recommend using "want to".

David
but i don't know how to use sed. could you give me any suggestion.
 
Old 01-15-2014, 02:18 AM   #9
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Quote:
Originally Posted by sachin.davra View Post
but i don't know how to use sed. could you give me any suggestion.
Try typing this command in a terminal window:
Code:
man sed
That should get you started.
jdk
 
Old 01-15-2014, 02:22 AM   #10
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jdkaye View Post
Try typing this command in a terminal window:
Code:
man sed
That should get you started.
jdk
Thanks guys for helping me out.
 
Old 01-15-2014, 02:23 AM   #11
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by sachin.davra View Post
but i don't know how to use sed. could you give me any suggestion.
see post #4 above. Also search online for something like "basic sed tutorial". Ask back here if you get stuck.

Evo2.
 
Old 01-15-2014, 02:55 PM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
To answer the original question, yes it is possible to edit a file using nothing more than the shell's builtin read and echo commands together with the shell's ability to test and modify variables. At its most basic:
Code:
IFS=
while read -r Line; do
    case "$Line" in
    "server ipaddress")
        echo "server otheripaddress";;
    *)
        echo "$Line";;
    esac
done <file >file.new
while read -r Line; do
    echo "$Line"
done <file.new >file
I actually had to do something like that a long time ago in a SunOS standalone shell when a bad /etc/fstab left me with nothing but shell builtins to work with.
 
Old 01-15-2014, 11:45 PM   #13
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
To answer the original question, yes it is possible to edit a file using nothing more than the shell's builtin read and echo commands together with the shell's ability to test and modify variables. At its most basic:
Code:
IFS=
while read -r Line; do
    case "$Line" in
    "server ipaddress")
        echo "server otheripaddress";;
    *)
        echo "$Line";;
    esac
done <file >file.new
while read -r Line; do
    echo "$Line"
done <file.new >file
I actually had to do something like that a long time ago in a SunOS standalone shell when a bad /etc/fstab left me with nothing but shell builtins to work with.

Thanks for giving the answer of my original question even i marked it as solved.

you have written a good script but i am not good at writing these kind of scripts. could you please gimme a link from where i can learn about this. because i am not able to understand most of the things in the script.
 
Old 01-16-2014, 03:01 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Learn bash from these links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

In the same vein as previous comments, use proper English ie use 'give me' not 'gimme' ... it really does come across very poorly
 
Old 01-17-2014, 09:39 AM   #15
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Many posters here do not have English as their native language. They do their best to communicate using English, and usually better than most native English speakers would do in another language. Disparaging the English usage of posters is not, IMO, a good thing.
 
  


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 command error "file exits" muby Linux - Newbie 4 03-17-2010 07:34 AM
Trying to edit a file via Command line or script seefor Linux - General 4 10-21-2008 12:39 PM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM
how do i edit my echo$PATH Laptop2250 Linux - Newbie 6 11-15-2003 11:33 AM
how to edit a file when not in X (ie from the command line) ludwig W Linux - Newbie 12 04-22-2003 04:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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