LinuxQuestions.org
Visit Jeremy's Blog.
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-19-2012, 11:52 AM   #1
rabir
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Rep: Reputation: Disabled
Question How To Edit a File Through Bash Script


Dear All

I wanna try to write a bash script that automatically configure my linux server. But i need to know the commands that "disable all lines of that file or specific file that i mension it." as well as want to input my desired lies through bash commands.

As cat>> myfile can append my file but it does not work in bash.

Help anyone.
 
Old 04-19-2012, 12:08 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
For that specific requirement I'd probably use:
Code:
perl -ni -e 'print unless /<regex>/' /path/to/file
For editing a specific line in a file:
Code:
perl -pi -e 's/<regex>/<replacement>/g' /path/to/file
To create a file:
Code:
cat <<EOF > /path/to/file
content
$myvar
content
EOF
To create a file without shell interpretation (handy when generating a script):
Code:
cat <<'EOF' > /path/to/file
content
PATH=$HOME/bin:$PATH
content
EOF
 
Old 04-19-2012, 12:09 PM   #3
dayid
Member
 
Registered: Apr 2012
Location: Austin, TX
Posts: 44

Rep: Reputation: Disabled
Quote:
Originally Posted by rabir View Post
Dear All

I wanna try to write a bash script that automatically configure my linux server. But i need to know the commands that "disable all lines of that file or specific file that i mension it." as well as want to input my desired lies through bash commands.
To "disable all lines" you would simple comment out the lines (or blank them)

To blank the file, simple:
Code:
: > file
To comment out all lines:
Code:
sed -e 's/^/#/g' file > tmp && mv tmp file
To add your own text to something you most likely want "echo" along with "read":
Code:
echo -n "What is your hostname? "
read ANSWER
echo "Hostname: $ANSWER" >> file
That's the very basics. For anything more complicated you'll need to give more context to work with.
 
Old 04-24-2012, 04:09 AM   #4
rabir
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thank you very much "dayid" for your help.

problem # 1
the problem is that -->in a file first i want to search specific line & make it disable
as grep -n "error" /etc/named.conf
then
change according to the result.

Problem # 2
How do i get my Lan card hardware address ?

Problem # 3
suppoese i take a value from user as "read variable1" & have to reverse it & as well as truncate according to my choice & again use it as a string


thanks again for help
 
Old 04-24-2012, 04:37 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031

Rep: Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202
Show us what you have tried regarding each of the questions and we can help guide you the answer.

For your first question, have a look at each of the sed answers provided already.
 
  


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] bash script to edit file seeberg Programming 4 04-05-2012 12:17 PM
[SOLVED] Bash Script. How to edit a file? y0_gesh Programming 3 02-28-2012 06:52 AM
[SOLVED] bash script to dynamically edit an html file melee Programming 24 04-17-2010 02:53 PM
Force Script (Bash) to Edit File? carlosinfl Linux - General 3 10-02-2008 11:57 AM
Bash script to edit text file snowman81 Linux - Desktop 2 01-10-2007 04:33 PM

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

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