By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

is it posible to append to txt file vertically?

P: 72
Is it posible to append to txt file vertically???
Example original file... (constants: A, B , C, Car, Boat)
Expand|Select|Wrap|Line Numbers
  1. BLANK,    Car,    BLANK,    Boat,    BLANK
  2. BLANK,    Nov07,    Dec07,    Nov07,    Dec07
  3. A,    1,    2,    2,    7,
  4. B,    1,    2,    2,    7,
  5. C,    1,    2,    2,    7,
when come to january, i wish to add the Jan 08 data (for noth car and boat respectively into the previous data), it should be adding to vertically right of the dec 07 data..

Expand|Select|Wrap|Line Numbers
  1. BLANK,    Car,    BLANK,    BLANK,    Boat,    BLANK,    BLANK
  2. BLANK,    Nov07,    Dec07,    Jan08,    Nov07,    Dec07,    Jan08
  3. A,    1,    2,    3,    2,    7,    4
  4. B,    1,    2,    4,    2,    7,    4
  5. C,    1,    2,    5,    2,    7,    5

Thanks.
Mar 22 '08 #1
Share this Question
Share on Google+
4 Replies


eWish
Expert 100+
P: 971
I believe that there are some modules that will let you append the file in the middle. However, I think that you would have to read the entire line and add the data to it. Then append the file. I would search CPAN for the Tie::File module. Going off memory, so I could be wrong here.

--Kevin
Mar 22 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
It is possible but it is not a standard function or standard filehandle option. You would have to write code to append data to the ends of lines or ends of specific lines.
Mar 22 '08 #3

P: 72
Thanks for the inputs.
Could anyone show me the code on how to add in the whole column to the file?

____Original text file___
A,1,2
B,1,2
C,1,2
D,1,2

__column to be input__
NEWA
NEWB
NEWC
NEWD


___OutputFile___
A,1,NEWA,2
B,1,NEWB,2
C,1,NEWC,2
D,1,NEWD,2
Mar 23 '08 #4

nithinpes
Expert 100+
P: 410
Thanks for the inputs.
Could anyone show me the code on how to add in the whole column to the file?

____Original text file___
A,1,2
B,1,2
C,1,2
D,1,2

__column to be input__
NEWA
NEWB
NEWC
NEWD


___OutputFile___
A,1,NEWA,2
B,1,NEWB,2
C,1,NEWC,2
D,1,NEWD,2
This can be done easily if you know the column number/field number where you want to insert data. Else, you have to calculate the field number by searching for a keyword(like 'Dec 07' in your initial example).
For the above case, where data need to be inserted in 3rd column/field, the following code works:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. open(DATA,"data.txt") or die "data open failed:$!";
  5. open(UPDATE,"update.txt") or die "update open failed:$!";
  6. open(RES,">result.txt") or die "create failed:$!";
  7.  
  8. my @update;
  9. ##take data for updating into an array
  10. while(<UPDATE>){chomp; push @update,$_ ;} 
  11. while(<DATA>) {
  12.   my @fields=split(/,/,$_);  ##splitting on commas
  13.   splice(@fields,2,0,shift(@update)); ##inserting in position:3/index:2
  14.   my $res=join(",",@fields);  ##joining elements with coma
  15.   print RES $res;
  16. }
  17. close(DATA);close(UPDATE);  close(RES);
  18.  
Mar 25 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.