Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 13th, 2008, 5:10 PM   #1
centurion555
Newbie
 
Join Date: Nov 2008
Posts: 9
Rep Power: 0 centurion555 is on a distinguished road
Selecting every third line

Hi every one,
I am trying to write a sed/awk command to select every third line from a text file. I have text files with 1200 lines and I need to cut every third line starting from 1 line (no header or footer in the file.)
Thanks in advance
centurion
centurion555 is offline   Reply With Quote
Old Nov 16th, 2008, 9:40 AM   #2
centurion555
Newbie
 
Join Date: Nov 2008
Posts: 9
Rep Power: 0 centurion555 is on a distinguished road
Cutting every third line

Hi every one,
I am trying to write a perl command to select every third line from a text file. I have text files with 1200 lines and I need to cut every third line starting from 1st line (no header or footer in the file.)
Thanks in advance
centurion
centurion555 is offline   Reply With Quote
Old Nov 16th, 2008, 9:50 AM   #3
Freaky Chris
Expert Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 768
Rep Power: 3 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: Cutting every third line

Quote:
Originally Posted by centurion555 View Post
Hi every one,
I am trying to write a perl command to select every third line from a text file. I have text files with 1200 lines and I need to cut every third line starting from 1st line (no header or footer in the file.)
Thanks in advance
centurion
read it line by line, and count three before you actually do anything with the lines

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old Nov 18th, 2008, 5:48 PM   #4
centurion555
Newbie
 
Join Date: Nov 2008
Posts: 9
Rep Power: 0 centurion555 is on a distinguished road
Re: Cutting every third line

Hi Chris,
Thanks for your reply. I am new to perl. I don't understand what this

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],

means and how to use this in perl script. I have perl installed but i never saw a syntax like this.
Centurion
centurion555 is offline   Reply With Quote
Old Nov 18th, 2008, 7:11 PM   #5
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,676
Rep Power: 7 lectricpharaoh will become famous soon enough
Re: Cutting every third line

Quote:
Originally Posted by centurion555
I am new to perl. I don't understand what this

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],

means and how to use this in perl script. I have perl installed but i never saw a syntax like this.
That is not Perl; it's a program in the brainfuck language (a language designed to be confusing, hence the name). It's also a sig, and not part of the message text itself, thus not part of his reply to you.

To expand on his initial post, you need to read the file line by line, and discard every line that's not a multiple of three. Generally, 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.

I've never used Perl, but here is pseudocode that you should be able to adapt into Perl:
pseudocode Syntax (Toggle Plain Text)
  1. inFile = open("SourceFile")
  2. outfile = open("DestinationFile")
  3. linecount = 0
  4. while(inFile.containsMoreLines())
  5. {
  6. increment lineCount
  7. textLine = inFile.readLine()
  8. if((lineCount mod 3) equals 0)
  9. outFile.writeLine(textLine)
  10. }
  11. inFile.close()
  12. outFile.close()
__________________
My microwave has settings for snake, gremlin, and puppy.

Last edited by lectricpharaoh; Nov 18th, 2008 at 7:23 PM.
lectricpharaoh is offline   Reply With Quote
Old Nov 21st, 2008, 10:34 AM   #6
Chetanji
programmer
 
Join Date: Nov 2008
Location: In the present moment
Posts: 19
Rep Power: 0 Chetanji is on a distinguished road
Re: Selecting every third line

Are you using dos or linux?

Is it possible that the third line is a duplicate of another line?

This fact would make it easy to remove the duplicate and only print out the unique lines.
Chetanji
Chetanji is offline   Reply With Quote
Old Nov 22nd, 2008, 4:39 AM   #7
Chetanji
programmer
 
Join Date: Nov 2008
Location: In the present moment
Posts: 19
Rep Power: 0 Chetanji is on a distinguished road
Re: Cutting every third line

Quote:
Originally Posted by centurion555 View Post
Hi every one,
I have text files with 1200 lines and I need to cut every third line starting from 1st line (no header or footer in the file.)centurion
If this third line is a duplicate of another line, ALWAYS.
Then you can use the a Awk program that mimicks the Unix sort command
"sort -u -k1,1".
Please give a little more information of what you are trying to achieve and what you data is.

Thanks,
Chetanji
Chetanji is offline   Reply With Quote
Old Nov 29th, 2008, 4:10 PM   #8
centurion555
Newbie
 
Join Date: Nov 2008
Posts: 9
Rep Power: 0 centurion555 is on a distinguished road
Re: Selecting every third line

Hello Chetanji,
I am using linux. All lines in the file are unique. No duplicate lines.
Centurion


Quote:
Originally Posted by Chetanji View Post
Are you using dos or linux?

Is it possible that the third line is a duplicate of another line?

This fact would make it easy to remove the duplicate and only print out the unique lines.
Chetanji
centurion555 is offline   Reply With Quote
Old Dec 1st, 2008, 7:32 AM   #9
Chetanji
programmer
 
Join Date: Nov 2008
Location: In the present moment
Posts: 19
Rep Power: 0 Chetanji is on a distinguished road
Re: Selecting every third line

Linux makes it easier.

please give 9 line sample of your text.

Chetanji
Chetanji is offline   Reply With Quote
Old Dec 1st, 2008, 8:03 AM   #10
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,484
Rep Power: 10 Infinite Recursion will become famous soon enough
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Re: Selecting every third line

No example of data needed.

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.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding REAL length of line dnguyen1022 C 14 Nov 18th, 2008 1:06 AM
Print line below and line above gmgenova Sed and Awk 0 Oct 27th, 2008 12:43 PM
Ackerman's function - crash upon launch codylee270 C++ 6 Oct 19th, 2006 2:14 AM
How to get the line in the coding? myName C# 2 Dec 14th, 2005 9:45 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC