473,385 Members | 2,015 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,385 software developers and data experts.

print lines from a text file

89
I am new to perl. I would like to open a file and print 2,6,10,14.. etc lines of that file content. How to do it? I amm able to open a file and print the contetnts as below:
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2.  
  3. $data_file = "test.txt";
  4. open(EXPORTFILE, $data_file) || die("Could not open file!");
  5.  print "Opened $data_file\n";
  6.  
  7. $counter = 0;
  8. while(($line=<EXPORTFILE>) && ($counter<100)) {
  9.  
  10.         if($counter == 0) {
  11.         print "Skip $counter \n";
  12.         $counter++;
  13.         next;
  14.         }
  15.  
  16.         print "COUNTER: $counter \n";
  17.         chomp($line);
  18.         print "$line \n";
  19.         $counter++;
  20.  
  21. }
  22.  
  23. close EXPORTFILE;
  24. exit(0);
Is it possible to skip $line or jump every 4 $line(ie) the lines in that file. Please let me know.

Thanks.
Aug 12 '08 #1
3 2657
eWish
971 Expert 512MB
Have a look at the Tie:File module it will be able to achieve what you want.

--Kevin
Aug 12 '08 #2
nithinpes
410 Expert 256MB
Also, you can read the entire file into an array and later increment the index of the array to print only the desired lines. Each element in the array would be each line in the file.
Expand|Select|Wrap|Line Numbers
  1. my @lines = <EXPORTFILE>;
  2. ###print lines 2,6,10,14........
  3. for(my $i=1; $i<@lines;) {
  4.   print "$lines[$i]";
  5.   $i+ =4;
  6. }
  7.  
  8.  
Aug 12 '08 #3
KevinADC
4,059 Expert 2GB
$. (dollar-sign dot) is the input record line number variable (files start at one unlike arrays, that start at zero). You can use the variable to find specific lines in files, but Tie::File does make it easy to do the same thing. Just keep in mind, the first line of a file is 0 (zero) if you use Tie::File since you are accessing the file as if it were an array.
Aug 12 '08 #4

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

Similar topics

5
by: MouseHart | last post by:
I've written a simple program in VB 6.0 to list all my MP3 files. To show them on the screen I used an MSFlexGrid named TextGrid (which is not associated with any table or text file) in the...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
7
by: Leonel Gayard | last post by:
Hi all, I had to write a small script, and I did it in python instead of shell-script. My script takes some arguments from the command line, like this. import sys args = sys.argv if args ==...
7
by: Ron | last post by:
Hi All, Is it possible to have Access print a report, identical to one that would print to a printer, only print to a "standard" text file? I can't find it in help and when I try to just print...
0
by: jigsmshah | last post by:
I need to print a text file .When i print it ,it is not printed exactly form 0,0 coordiante of the page. I mean the top most left corner.It is leaving some margin from top and from left.The code is...
12
by: summy | last post by:
Write a program includes ALL the features: 1.Read the specify file sample.txt and then print the contents of the file on screen. 2.Print each lines of the file in reversed order on screen. ...
3
by: itdaddy | last post by:
hey perl gurus! i am new to this forum cause i need help. I have done many scripts. but i want to use perl to do this: What I want to do is this. I have a QRP file that I can convert to a txt...
2
by: alivip | last post by:
when I wont to inser (anyting I print) to the textbox it will not inser it just print then hanging # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, #...
12
by: Studiotyphoon | last post by:
Hi, I have report which I need to print 3 times, but would like to have the following headings Customer Copy - Print 1 Accounts Copy - Print 2 File Copy -Print 3 I created a macro to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.