Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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


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
if echo $line | grep -q "^default.*" ; then
echo "default=${myval}" >> /tmp/lilo-copy.conf
echo $line >> /tmp/lilo-copy.conf
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
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381

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
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You probably loose that whitespace because of
echo $line
Replace it with
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
sed "s|^default=.*|default=$myval|" /etc/lilo.conf > /tmp/lilo-copy.conf
Old 08-17-2006, 10:50 AM   #4
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).


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
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 04:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration