The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Special Forums > UNIX Desktop for Dummies Questions & Answers
.
google unix.com



UNIX Desktop for Dummies Questions & Answers Discuss UNIX and Linux user interfaces like GNOME, KDE, CDE, and Open Office here. All UNIX and Linux Newbies Welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to prevent overwrite particular config file when using rpm --replacefile option amitpansuria UNIX for Advanced & Expert Users 0 07-17-2008 05:36 AM
overwrite specific lines in a file csecnarf Shell Programming and Scripting 7 05-13-2008 11:01 AM
Overwrite & Delete in Text File 33junaid Shell Programming and Scripting 11 08-16-2007 05:28 AM
Shell Script to read specific lines in a file varshanswamy Shell Programming and Scripting 5 08-22-2005 07:12 PM
Overwrite Duckman UNIX for Dummies Questions & Answers 9 03-27-2001 01:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-27-2008
ACTGADE ACTGADE is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 6
shell overwrite lines in a file

Hello,

I have an external file (file.txt). This shall be a kind of a config file for my shell script. I get the line numbers as to a corresponding entry by grep. Now I would like to substitute the corresponding line from the shell script back to the file and exactly on the same line number.

e.g. if line number 20 is the place in file.txt where the name of the project is stored. And the user can change the name of his project via a kdialog template, this changes shall be overwrite in the file.txt, line 20.

tks for any reply
  #2 (permalink)  
Old 10-27-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Do you mean that you want a history of changes in the script to be shown (somehow) in file.txt

An actual example of what you mean would help a lot.
  #3 (permalink)  
Old 10-27-2008
ACTGADE ACTGADE is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 6
Tks for your replay. Here is the explanantion:

I am writing a script to install a embedded mini-linux via gentoo. The script is communicating with the user via kdialog.
I would have two chances - 1st. fix all parameter (e.g. projectname, path to project, selected libs (uclibc, glibs etc.) etc.) or 2nd. put all those parameter in a separate file.
This file is my config file file.txt it looks like this:

#### REM
#
#

# <1> Projectname
ProjectOne

#REM
# <2> Path to Project
/mnt/ProjectOne

# ....

So once I need a parameter e.g. the projectname the script can read it from the config file file.txt by find the contant starting with <...> add one line and read the result into a var.

But the user shall be able to change the parameters as well. E.g. he can offer a kdialog to define the project path. If this differ from the default, I would like to overwrite the choice e.g. /tmp/myproject/new in the config file by searching the # <2> ... constant add one line and overwrite the actual value.

My problem is that I do not know a tool in shell which is able to write into files as to a line order. SED maybe can but I do not have any experience with it.

Tks for your reply in advance
  #4 (permalink)  
Old 10-28-2008
ACTGADE ACTGADE is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 6
Found a SED solution which looks:

# getCounter
var="# <Path to Project>"
var1="/root/Desktop/Textdatei.txt"
Counter=`sed -n -e "/${var}/=" $var1`
FinalCounter=`expr $Counter + 1`

# lineSubstitution
varneu="/tmp/noch/watdazu/gesch/"
sed -e "${FinalCounter}c\\${varneu}" $var1 > xxx.txt # Problem: described below
cat xxx.txt > $var1 # Bad solution
rm xxx.txt

The remaining problem is that I wished to write the whole samplestorage back to the original file:

1st solution: sed -e "${FinalCounter}c\\${varneu}" $var1 > $var1
2nd solution sed -e "${FinalCounter}c\\${varneu}w $var1" $var1

in solution no. 1 the file (Textdatei.txt) is always empty. I do not understand for what reason the file will be overwritten with empty lines?

in solution no 2. I have the problem with the syntax. the w $var1 is always just added to the $varneu in the substituted line. I tried with several charcters before w like (/, \, etc. ) but I did'nt hit the right one.

Can anybody advice how to go along with this?

tks in advance
  #5 (permalink)  
Old 10-28-2008
ACTGADE ACTGADE is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 6
Found a SED solution which looks:

# getCounter
var="# <Path to Project>"
var1="/root/Desktop/Textdatei.txt"
Counter=`sed -n -e "/${var}/=" $var1`
FinalCounter=`expr $Counter + 1`

# lineSubstitution
varneu="/tmp/noch/watdazu/gesch/"
sed -e "${FinalCounter}c\\${varneu}" $var1 > xxx.txt # Problem: described below
cat xxx.txt > $var1 # Bad solution
rm xxx.txt

The remaining problem is that I wished to write the whole samplestorage back to the original file:

1st solution: sed -e "${FinalCounter}c\\${varneu}" $var1 > $var1
2nd solution sed -e "${FinalCounter}c\\${varneu}w $var1" $var1

in solution no. 1 the file (Textdatei.txt) is always empty. I do not understand for what reason the file will be overwritten with empty lines?

in solution no 2. I have the problem with the syntax. the w $var1 is always just added to the $varneu in the substituted line. I tried with several charcters before w like (/, \, etc. ) but I did'nt hit the right one.

Can you advice how to go along with this?

tks in advance

p.s. this is my Textdatei.txt:

#############################################
#***************Use this File **************#
#############################################
#
#

# <ProjectName>
AlterName

#
#
#
# <Path to Project>
/tmp/noch/watdazu/gesch/

#
#
#

Edit/Delete Message Reply With Quote Multi-Quote This Message Quick reply to this message
Closed Thread

Bookmarks

Tags
external file, grep, sed, shell, write in lines

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 09:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0