How to edit and delete records from flat file in perl

Posted on 2000-04-04
Last Modified: 2011-09-20
I have a flat file which I store some data in it and each record in a line like this
note: |!!| is the field separator.

This file contain about 4000 records and can be more (4000 line because each line is a record)

so what I want to do is:
1- to edit one of the records (lets say record 3476)
2- to delete one of the records (lets say record 3000).

so what is the perfect way to do so.......
do I have to rebuild the file which contain 4000+ record, and it could be more.
or there is a better way to do so....
Please give code for both ways.....

Wait.... I will run the cgi on a NT web-server....
Question by:kifah
  • 2
  • 2
LVL 10

Expert Comment

ID: 2683710
the beter way is to use a database and DBI to talk to it ..

its easy. 4000 + records > use access. you dont even need access on the server

write a script to read through your existing file pumping your existing data into the db and then use the db from then on
LVL 84

Expert Comment

ID: 2684870
perldoc -q "How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file"
tie %hash,$DBM,$file;

Author Comment

ID: 2690907
I could not understand both of you.

Mr. makerp
What database you are talking about, and what is the DBI... can you please give more details....

Mr. ozo
where I can find this document.. can you give me a link to this page....

please I prefer a code....
LVL 10

Accepted Solution

makerp earned 100 total points
ID: 2692565
all i mean is create a database. such as access (your on NT). design it based on your flat file and them write a script to take the data from the flat file and pump it into the db, then use the db from then on and scrap your file.

databases make life so mush easier. you will have less code to maintain as the sql can do alot of the work for you. also it will be a mush more robust solution and make enhacments to your app alot easier.

dbi is a perl module that provides a frame work for database access. dbd modules implement the methods for dbi but for a specific db. therefore you install dbi and then decide which database you want to use then get the appropriate dbd driver for it.

if you going to be on NT then i suugest activeperl and the go to the /packages/zips/ dir of there web site and get dbi and install it. then get the DBD-ODBC module. this will be fine for any odbc complient data store . i.e access/sql server/oracle etc.

below is a comprehensive example of dbi. it uses an access data base made up of a single table called table1 with two fileds name and addess. set an odbc link up in contol panel called test_db and point it to the db you have created. put some junk data in the db fileds.

# make use of the dbi module. for activeperl go to and
# download the DBI package first and then download what ever DBD drivers you what to use
# this script makes use of the odbc driver module
use DBI;

# the dbd driver we actually want to use

# the dsn info
$DATA_SOURCE = "test_db";

# global database handle for this script
my $dbh;

# connect to database, the first parameter will take the form of 'dbi:ODBC:test_db'
      || die("Connection error : ".$DBI::errstr);

# set the LongReadLen really high incase we have meno fields
$dbh->{LongReadLen} = 30000;

# prepare a stmt, this returns a statment handle
$sth = $dbh->prepare("SELECT * FROM table1") || die("Stmt error : ".$dbh->errstr);

# use some of the stmt handle vars to determine the fields that
# will be returned. this is usfull if we dont know in advance
# the number/name of fields that will be returned i.e 'select *'
print("Expected fields to be returned from first query.......\n");
$fi = $sth->{'NAME'};
      #print("field : @{$fi[0]}[$i]\n");
       print("field : $fi->[$i]\n");
# execute our stmt
$sth->execute() || die("Stmt error : ".$dbh->errstr);

# if we are debugging then we may wish to dump our result in one hit
# the dump_results function uses default values for row delimiter's etc
# these can be overridden
# dump_results(st_handle,maxlen,row_delimiter,field_delimiter,fileh_for_dump)
print("Dump of the results returned by stmt, for debugging purposes\n");

print("\nNormal results retrival using a loop to go through each row returned\n");
# execute our stmt
$sth->execute() || die("Stmt error : ".$dbh->errstr);

# fetch and print the rows, returns undef on failure, hash reference on success
while($dat = $sth->fetchrow_hashref())
      # get the name field from our hashref
      print("Name : ".$dat->{"name"}."\t Address : ".$dat->{"address"}."\n");

# finish the statement handle, if we dont do this the disconnect function will spew some errors

print("\nDoing some inserts into the database\n");
# set auto commit to false so we have to explicetly call commit on the database handle
$dbh->{"AutoCommit"} = 0;
# we can prepare a stmt and execute it several times with different values in the place holders like this
$sth = $dbh->prepare("INSERT INTO table1 (name,address) VALUES (?,?)") || die("Stmt error : ".$dbh->errstr);
# execute it but this time pass the values we want to replace the place holders
$sth->execute("bart","saturn") || die("Stmt error : ".$dbh->errstr);
# commit it
$dbh->commit() || die("Stmt error : ".$dbh->errstr);
# turn auto commit back on
$dbh->{"AutoCommit"} = 1;
# finish with the stmt handle

print("\nDoing an update on all records (changing address to mars)\n");
# we can use the do function to execute a stmt and get the number of rows it affected
$rows = $dbh->do("UPDATE table1 SET address = 'mars'") || die("Stmt error : ".$dbh->errstr);
print("That affected ".$rows." rows\n");

# now print the updated table out
print("\nUpdated table contents are : \n");
$sth = $dbh->prepare("SELECT * FROM table1") || die("Stmt error : ".$dbh->errstr);
$sth->execute() || die("Stmt error : ".$dbh->errstr);

while($dat = $sth->fetchrow_hashref())
      # get the name field from our hashref
      print("Name : ".$dat->{"name"}."\t Address : ".$dat->{"address"}."\n");

print("\n\tBye bye.... Shutting up shop !!!!");
# closes the database



Author Comment

ID: 2696048
Thanks for that.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question