LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
Search this Thread
Old 12-30-2005, 09:33 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
Writing to a specific line in a text file


I am trying to write an app that will read a text file, look for specific lines and when they are found....replace that line with something else leaving the rest of the file the same. The file order must stay in tact, that is I can not just append to the end or beginning. The change needs to be made exactly where the line occurs in the text file. Can anyone help me as to how this would be done?
 
Old 12-30-2005, 09:40 AM   #2
leadazide
Member
 
Registered: Apr 2004
Location: Germany
Distribution: SuSE 11.0, Ubuntu 7.10
Posts: 390

Rep: Reputation: 30
I think you don't need to write a program for that... try reading grep and sed manpages, I think those tools can do what you wish.
 
Old 12-30-2005, 10:04 AM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,483

Rep: Reputation: 66
yea what leadazide said would be eazier then writeing a program.

In a program you would have to look for the string then read the number of char in the found string rewind the file pointer back that number of charecters read the rest of the file and put that in temporary space then write the wanted charecters then write back overwriteing the remaining charecters in the file.


working with text files is a pain because when you write charecters to the file it doesent move the rest to accomidate. here is an example.


if i had a text file with the words "cat cat cat" and attempted to write cougar into the first cat then the resulting file would look like this "cougart cat"



This of course is from C programming I dunno if this is the case in others.
 
Old 12-30-2005, 10:12 AM   #4
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
I am programming in vb.net though
 
Old 12-30-2005, 01:30 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
>> I am programming in vb.net though

Thats the first problem..

Sorry cant be more helpful.. My choice would be perl..
 
Old 12-30-2005, 03:53 PM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
Quote:
I am programming in vb.net though
So how many megabytes of code would it take to do this in vb.net?

I don't know exactly what you are trying to do, but this little "1-liner" sed script will do what I think you are trying to do.

I'll use a little snippet from the /etc/services file as an example. Let's suppose we wanted to change any port described as Reserved to Do Not Use.

Code:
$ cat services
# Port Assignments:
#
# Keyword         Decimal    Description                     References
# -------         -------    -----------                     ----------
#                 0/tcp    Reserved
#                 0/udp    Reserved
#                          Jon Postel <postel@****.edu>
tcpmux            1/tcp    # TCP Port Service Multiplexer
tcpmux            1/udp    # TCP Port Service Multiplexer
#                          Mark Lottor <MKL@nisc.sri.com>
compressnet       2/tcp    # Management Utility
compressnet       2/udp    # Management Utility
compressnet       3/tcp    # Compression Process
compressnet       3/udp    # Compression Process
#                          Bernie Volz <volz@****.com>
#                 4/tcp    # Unassigned
#                 4/udp    # Unassigned

$ sed -i.bak 's/Reserved/Do Not Use/g' services

$ cat services
# Port Assignments:
#
# Keyword         Decimal    Description                     References
# -------         -------    -----------                     ----------
#                 0/tcp    Do Not Use
#                 0/udp    Do Not Use
#                          Jon Postel <postel@****.edu>
tcpmux            1/tcp    # TCP Port Service Multiplexer
tcpmux            1/udp    # TCP Port Service Multiplexer
#                          Mark Lottor <MKL@nisc.sri.com>
compressnet       2/tcp    # Management Utility
compressnet       2/udp    # Management Utility
compressnet       3/tcp    # Compression Process
compressnet       3/udp    # Compression Process
#                          Bernie Volz <volz@****.com>
#                 4/tcp    # Unassigned
#                 4/udp    # Unassigned
The -i switch tells sed to edit the file in place. The -i.bak will make a backup of the original file with the .bak extension.
 
Old 12-30-2005, 04:02 PM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
Suppose we want to replace a whole line like you mentioned in your post. We will replace the lines with the words Management Utility with a tag -- Management Utility --

Code:
sed -i.bak 's/^.*Management Utility.*$/ -- Management Utility -- /g' services
$ cat services
# Port Assignments:
#
# Keyword         Decimal    Description                     References
# -------         -------    -----------                     ----------
#                 0/tcp    Reserved
#                 0/udp    Reserved
#                          Jon Postel <postel@****.edu>
tcpmux            1/tcp    # TCP Port Service Multiplexer
tcpmux            1/udp    # TCP Port Service Multiplexer
#                          Mark Lottor <MKL@nisc.sri.com>
 -- Management Utility --
 -- Management Utility --
compressnet       3/tcp    # Compression Process
compressnet       3/udp    # Compression Process
#                          Bernie Volz <volz@****.com>
#                 4/tcp    # Unassigned
#                 4/udp    # Unassigned
 
  


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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Command line tools to Find files with specific text naps Linux - Software 5 11-15-2004 04:46 AM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM


All times are GMT -5. The time now is 11:15 PM.

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