Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write SELECTIVELY into a txt-file?

Status
Not open for further replies.

subotai

Programmer
Jun 26, 2002
6
DE
Greetings!

Ive been fumbling around with reading and writing linux config-files, especially reading out different user-input, using pattern matching etc. Im getting there, perl seems very user-friendly in that matter.

Only thing left to me is: how can i SELECTIVELY change (write) data in a text file?
Lets say i have a whatever.conf file eg. for network settings:
ETH='ETH0'
USER='burningrubber'
DEMAND=300
DNSTYPE=SERVER

Selectively reading out is no problem. But lets say the order in which the above mentioned parameters are set in the file changes (for whatever reason; there are config scripts that would just put the "user=" line below "DEMAND)
What i did until now was open the file for write, and "print FILE "line\n"; for every line, thus rewriting the complete file. Even as a rookie i imagine thats far from being pro or 7337-style ;-)
Is there a way to say "just change that-and-that line, and leave the rest unchanged"?

Greatful for every hint,
Subotai
 
Well going along with Perl's slogan 'There is more than one way to do it' there are at least a few ways to do this. Assuming that there are only a few lines of text, per your example, you could always use a loop to iterate and use regexps to use something in the order of a hash or array to store and then print out the information in the order you desire. For example:

while ($line = <FILE>)
{
chomp $line;
if ($line =~ /^ETH.*/)
{
$array[0] = $line;
next;
}
elsif ($line =~ /^USER.*/)
{
$array[1] =~ $line;
next;
}
# continue on with remaining line possibilities

}

foreach $item (@array)
{
print OUTFILE $item . &quot;\n&quot;;
}

# don't forget to close the files


This is a quick and dirty way of writing the script. I'm not sure on its efficiency. There is probably a more &quot;correct&quot; way of writing it. You can also use hashes here. They may make more sense. If this file is huge (many lines of input) the array/hash method may not be what you want to do. In that case a possibility could be to take an already processed file and check each line in the file you are working on with the already processed file. Those lines which have the same beginnings (i.e. 1st line in both is &quot;ETH=something&quot;) can be printed out. When they don't match, then you can store that line in a hash. Each time a line doesn't match you can check the hash to see if the line occurred previously and then possibly print it out or else add the new line to the hash. Does that make sense? I know its long and drawn out. These are the things I would try. Hope it helps point you in the right direction at least.

Good luck,
Derek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top