LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Writing to a specific line in a text file (http://www.linuxquestions.org/questions/programming-9/writing-to-a-specific-line-in-a-text-file-397853/)

mrobertson 12-30-2005 09:33 AM

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?

leadazide 12-30-2005 09:40 AM

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.

exvor 12-30-2005 10:04 AM

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.

mrobertson 12-30-2005 10:12 AM

I am programming in vb.net though

xhi 12-30-2005 01:30 PM

>> I am programming in vb.net though

Thats the first problem.. :tisk:

Sorry cant be more helpful.. My choice would be perl..

/bin/bash 12-30-2005 03:53 PM

Quote:

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

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.

/bin/bash 12-30-2005 04:02 PM

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



All times are GMT -5. The time now is 09:19 PM.