How to select every third line from a text file using Perl?, Monday, December 22, 2008

I am having a text file which contains 1500 lines and I need to cut every third line starting from 1st line .In my file there is no header or footer.



We can do this by reading it line by line, and count three before you actually do anything with the lines.

You need to read the file line by line, and discard every line that is not a multiple of three. In general, the way you do this is by writing the lines you want to keep to a new file, so that when you're done, it will contain every third line of the first file.



Here is pseudocode that you should be able to adapt into Perl:

pseudocode Syntax
inFile = open("SourceFile")
outfile = open("DestinationFile")
linecount = 0
while(inFile.containsMoreLines())
{
increment lineCount
textLine = inFile.readLine()
if((lineCount mod 3) equals 0)
outFile.writeLine(textLine)
}
inFile.close()
outFile.close()

If you are using Linux, we can simply do this by the command given below.

awk 'NR%3==0' FILENAME



where FILENAME is the name of your data file. You can replace 3 with N and get the Nth line as well.

posted by power @ 9:42 PM permanent link   |

Post a Comment

|

0 comments
India positioned fourth in the midst of top ten internet users, Tuesday, December 2, 2008

The escalating faith on the web build India stands fourth with the country being home for 81 million net users. The sum of internet users in the world is 134, 85, 72,040 by the end of 2007 and Asia is the core of about 568.7 million users.

The United States of America is on top of the chart with 220 million net users whereas China and Japan pursue with 210 million and 88.1 million users, in the same way. Next to India, Brazil set at the fifth rank with 53.1 million users. As stated by the statistics which was revealed by the Internet Governance Forum, United Kingdom stands as sixth uppermost internet usage center with 40.2 million citizens being accessible to the internet, followed by Germany with 39.1 million users, Republic of Korea with 35.5 million users, and Italy with 32 million users and France with 31.5 million users.

But if we consider broadband connection, India has no place in the top ten nations. But U.S. occupies first position with 73.2 million connections. The other countries who had bagged a position in the broadband chart are China with 66.4 million connections, followed by Japan with 28.28 million, Germany with 19.6 million, UK with15.6 million, France with 15.5 million, Republic of Korea with 14.7 million, Italy with 10.8 million, and Canada with nine million and Spain with eight million.

posted by power @ 4:52 AM permanent link   |

Post a Comment

|

0 comments