Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting and moving a line in a txt file

Posted on 1999-07-04
7
Medium Priority
?
159 Views
Last Modified: 2010-03-04
I will try to explain my problem
(I deleted the first attempt):

I have a file called: order.txt
The path to file it is at: $order_dir/$order_database

This is what is in it:

the first line containt the headers
the following line contains the data.
The fields are separated by |

Here is an example:

date|cart_id|price|name|test1|test2|test3|test4
201299|1234567445566|22.22|n1|aa|bb|cc|dd
211299|2234567445567|32.32|n2|ee|uu|cc|ss
221299|3234567445568|42.42|n3|ff|vv|cc|ll
231299|4234567445569|52.52|n4|gg|nn|mm|kk

What I want to do is move a line to a file
called orderold.txt
The path to file it is at: $order_dir/$order_database_old

For example: move the line:
211299|2234567445567|32.32|n2|ee|uu|cc|ss
where card_id is 2234567445567 to the
file orderold.txt!

order.txt will look like:

date|cart_id|price|name|test1|test2|test3|test4
201299|1234567445566|22.22|n1|aa|bb|cc|dd
221299|3234567445568|42.42|n3|ff|vv|cc|ll
231299|4234567445569|52.52|n4|gg|nn|mm|kk


and orderold.txt will look like

date|cart_id|price|name|test1|test2|test3|test4
211299|2234567445567|32.32|n2|ee|uu|cc|ss


When I now want to (re)move an other line to
orderold.txt (for example 201299|1234567445566|22.22|n1|aa|bb|cc|dd):

date|cart_id|price|name|test1|test2|test3|test4
211299|2234567445567|32.32|n2|ee|uu|cc|ss
201299|1234567445566|22.22|n1|aa|bb|cc|dd

I have tried

elsif ($in{'action'} eq "remove_orders") {

# your script part has to come here
   {local $^I=''; local @ARGV=('$order_dir/order.txt');
      while( <> ){
         print unless (split'\|')[1] eq '3779e4e604529084';
      }
   }
# end of your script part

}

but this would not work.

can you help me?
Thank you in advance!

greetings Michel Weegerink
0
Comment
Question by:weversbv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1213819
use Fcntl qw(:flock);
open ORDER,"<order.txt" or print "can't open order.txt: $!";
flock ORDER,LOCK_EX;
open NEW,">ordernew.txt" or print "can't open ordernew.txt: $!";
open OLD,">>orderold.txt" or print "can't open orderold.txt: $!";
while( <ORDER> ){
    if( (split'\|')[1] eq '2234567445567' ){
        print OLD;
    }else{
        print NEW;
    }
}
close NEW;
close OLD;
rename 'ordernew.txt','order.txt' or print "can't rename ordernew.txt,order.txt:$!";
close ORDER;
0
 

Author Comment

by:weversbv
ID: 1213820
I have edited the file (See down)
It works fine but only the rename part won't work

      use Fcntl qw(:flock);
      open ORDER,"<$order_dir/orders.txt" or print "can't open orders.txt: $!";
      flock ORDER,LOCK_EX;
      open NEW,">$order_dir/ordersnew.txt" or print "can't open ordersnew.txt: $!";
      open OLD,">>$order_dir/ordersold.txt" or print "can't open ordersold.txt: $!";
      while( <ORDER> ){
         if( (split'\|')[1] eq "$in{'cart'}" ){
            print OLD;
         }
         else{
            print NEW;
         }
      }
      close NEW;
      close OLD;
      rename '$order_dir/ordersnew.txt','$order_dir/orders.txt' or print "can't rename ordersnew.txt,orders.txt:$!";
      close ORDER;

The files ordersnew.txt & orders.txt & ordersold are made.
I get the error:
can't rename ordersnew.txt,orders.txt:No such file or directory

What am I doing wrong?
 
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213821
rename "$order_dir/ordersnew.txt","$order_dir/orders.txt" or print "can't rename $order_dir/ordersnew.txt,$order_dir/orders.txt:$!";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:weversbv
ID: 1213822
The script works fine now.
I have another question.
I want to move a file.

/test/test1/test.txt to /test/test1/old/test.txt

Can I use the move command and if yoes how?

greetings Michel Weegerink
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213823
rename '/test/test1/test.txt','/test/test1/old/test.txt' or print "can't rename'/test/test1/test.txt','/test/test1/old/test.txt' $!";
0
 

Author Comment

by:weversbv
ID: 1213824
Thank you.
How to give you the point or do I have to delete the question???
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 1213825
You may grade the answer.
Or you can reject it if you need any more clarification, or want to get someone elses opinion.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

670 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