473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

File creation using Perl

Hi,

I want to parse a file and from some of the attributes of this file, i want to create another file using perl. Can anybody suggest me a way out? It would be great if i dont have to use any module. Is it possible? Any sample code will be of a great help to me.

Cheers,
Amit
Oct 1 '07 #1
6 4374
numberwhun
3,509 Expert Mod 2GB
Hi,

I want to parse a file and from some of the attributes of this file, i want to create another file using perl. Can anybody suggest me a way out? It would be great if i dont have to use any module. Is it possible? Any sample code will be of a great help to me.

Cheers,
Amit
Can you first tell us what OS you are on? (ie: Linux, Unix, Windows, Mac)

Also, are you wanting to create the file with the same permissions? attributes? what? Can you provide a few more details about your goal?

Regards,

Jeff
Oct 1 '07 #2
Can you first tell us what OS you are on? (ie: Linux, Unix, Windows, Mac)

Also, are you wanting to create the file with the same permissions? attributes? what? Can you provide a few more details about your goal?

Regards,

Jeff
Hi Jeff,

Thanks a lot for the reply. I always expect a reply from atleast you :-) . I will be using Windows environment as well as Solaris environment.

Suppose i have a file a.txt with 4 rows as shown below:-

************************************************** ************
i@am@amit@and@i@work@in@this@world
i@am@amit@and@i@work@in@this@world
i@am@amit@and@i@work@in@this@world
i@am@amit@and@i@work@in@this@world

************************************************** ************

i want my code to parse the above file such that only "amit" and "work" are picked up and put into a file b.txt as follows:-

************************************************** **************
amit@work
amit@work
amit@work
amit@work

************************************************** ************************

I am not too concerned about the permissions. Whatever permissions a.txt has, i want to create file b.txt file with the same permissions either in the same directory or a different directory.

Hopefully i have been able to explain you my requirement.

Thanks in advance for your help.

Cheers,
Amit
Oct 2 '07 #3
Here's a concise explanation of how to do file IO in perl


If you don't understand any of that I might be able to explain further.
Oct 2 '07 #4
Here's a concise explanation of how to do file IO in perl


If you don't understand any of that I might be able to explain further.
Thanks a lot. I was able to understand it from there :-). May i know your good name please so that instead of a blank "Hi" or "Thanks", i could refer to your name :-).

Thanks again.

Cheers,
Amit
Oct 3 '07 #5
humaid
24
Here's a concise explanation of how to do file IO in perl


If you don't understand any of that I might be able to explain further.

hi amit,
i have tried this but couldnt get the exact result you want.but i got the maximunm result so try this out and modify according.

Expand|Select|Wrap|Line Numbers
  1. #!/user/bin/perl
  2. open (FH,"amit.txt");
  3. @film = <FH>;
  4. close (FH);
  5. foreach $line(@film)
  6. {
  7.     chomp($line);
  8.     push @arr,(split(/\@/,$line))[2,5];
  9. }
  10. foreach $line (@arr)
  11. {
  12. print " $line " ;
  13.  
Oct 3 '07 #6
hi amit,
i have tried this but couldnt get the exact result you want.but i got the maximunm result so try this out and modify according.

Expand|Select|Wrap|Line Numbers
  1. #!/user/bin/perl
  2. open (FH,"amit.txt");
  3. @film = <FH>;
  4. close (FH);
  5. foreach $line(@film)
  6. {
  7.     chomp($line);
  8.     push @arr,(split(/\@/,$line))[2,5];
  9. }
  10. foreach $line (@arr)
  11. {
  12. print " $line " ;
  13.  
Thanks a lot for taking pain. I tried something like below:-

Expand|Select|Wrap|Line Numbers
  1. open(FILE, "amy.csv") ;
  2. my @lines = <FILE>;
  3. close(FILE);
  4.  
  5.  
  6. foreach my $line (@lines)
  7. {
  8.     next if $line =~ /^\s*$/;
  9.     $line =~ tr/ /-/ ;                         
  10.     my @cols = split(/\@/, $line);                 
  11.     print "$cols[1]********$cols[3]*****$cols[4]\n" ;     
  12.     open(IN, ">>amit.txt");
  13.     print IN "$cols[1]\@$cols[3]\@$cols[4]\n";
  14. }
  15.  
  16. close(FILE);
  17.  
and i was able to parse file amy.csv line by line and put the desired columns one by one in the file amit.txt.

Cheers,
Amit
Oct 3 '07 #7

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

Similar topics

14
by: Xah Lee | last post by:
Just bumped into another irresponsibility in perl. the crime in question this time is the module File::Basename. Reproduction: 1. create a directory containing a file of this name:...
3
by: Hugz | last post by:
Hello, And Thank you for taking time to help me.Myself Hugz.I am new to perl and want to write my own Scripts.But i can't understand how perl "rename" function and "file locking" works. ...
3
by: SK | last post by:
I have a file. i get the creation time using File.GetCreationTime. then i go and delete that file. and then create it again and print the File.GetCreationTime. It is giving me the old creation...
6
by: jonathanmcdougall | last post by:
I have read many posts on this subject and found no satisfying answer. I am creating a file on the server via a PHP script. The file is created using GD (imagejpeg()), though I don't think it is...
19
by: Materialised | last post by:
Hi everyone, What I am wanting to do, is to copy, a simple plain text file, to another file, but omitting duplicate items. The way I thought of doing this, involved copying all the items into...
8
by: Eddie Suey | last post by:
I want to add a new line to the begining of a text file. I dont want to write over existing data. How do I do this? the file is about 7 mb.
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
8
by: mailtofinny | last post by:
Hi This is my first post to this forum. I am struck up with a problem. We have a perl script which deals with creation/deletion of files. This perl script is trigerred through a java...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.