LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-16-2006, 05:08 PM   #1
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Rep: Reputation: 15
Post shell scripting: "formatting" issue with text files


Hello!

I'm trying to replace a value in a file, and to do this, I'm reading in line-by-line. If the line doesn't match I copy it over to a temporary file, and if it does match, I make my change and copy that over to the temp file. This works great, but I'm losing white spaces in the line (tabs and indents only). So it's basically pushing everything along the left edge, which is horrid. How can I go about fixing this? I tried using "sed" to change the value, but it doesn't work well when you include variables. Here's the current code that I'm using just in case someone is interested.


while read line
do
if echo $line | grep -q "^default.*" ; then
echo "default=${myval}" >> /tmp/lilo-copy.conf
else
echo $line >> /tmp/lilo-copy.conf
fi
done < /etc/lilo.conf
mv /tmp/lilo-copy.conf /etc/lilo.conf



original sed command I used:
sed -e "/^default/ s/^default=.*/default=$myval/" /etc/lilo.conf

Last edited by eur0dad; 08-16-2006 at 05:09 PM.
 
Old 08-16-2006, 05:36 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

You don't need the first part of the sed command:

sed "s/^default=.*/default=$myval/" /etc/lilo.conf

Should replace all instances of default=.*, at the beginning of the line with default= and the content of $myval.

If you have sed 4.x (sed --version) you can use the -i flag to instantly edit the infile (man sed for details).

Hope this helps.
 
Old 08-17-2006, 03:51 AM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You probably loose that whitespace because of
Code:
echo $line
Replace it with
Code:
echo "$line"
in both instances, as echo will interpret $line as a collection of separate arguments instead of a single argument that contains space characters.
But then, the sed command should work fine unless $myval contains "/". Note that you may use any other character for that purpose in sed, as in
Code:
sed "s|^default=.*|default=$myval|" /etc/lilo.conf > /tmp/lilo-copy.conf
 
Old 08-17-2006, 10:50 AM   #4
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Original Poster
Rep: Reputation: 15
Thanks for the help guys! I ended up using your sed command (so much prettier).
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Strange Problem with Text Files moo-cow Programming 17 04-02-2006 01:14 AM
shell scripting - using ${variable:=","} Steve Riley Linux - General 7 02-08-2005 05:07 AM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM
"how do I extract a number from a text file using shell command?" sdandeker Linux - Networking 3 02-12-2004 08:54 AM
Shell Scripting: Users "**Never logged in**" cparker15 Linux - General 1 10-02-2003 03:48 PM


All times are GMT -5. The time now is 07:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration