![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2008
Posts: 9
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2008
Posts: 9
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#3 | |
|
Expert Programmer
|
Re: Cutting every third line
Quote:
Chris
__________________
Steven Skiena - Algorithms ,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],brainf**k -- It's such a pretty language |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2008
Posts: 9
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 | |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,676
Rep Power: 7
![]() |
Re: Cutting every third line
Quote:
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)
__________________
My microwave has settings for snake, gremlin, and puppy. Last edited by lectricpharaoh; Nov 18th, 2008 at 7:23 PM. |
|
|
|
|
|
|
#6 |
|
programmer
Join Date: Nov 2008
Location: In the present moment
Posts: 19
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#7 | |
|
programmer
Join Date: Nov 2008
Location: In the present moment
Posts: 19
Rep Power: 0
![]() |
Re: Cutting every third line
Quote:
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 ![]() |
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Nov 2008
Posts: 9
Rep Power: 0
![]() |
Re: Selecting every third line
Hello Chetanji,
I am using linux. All lines in the file are unique. No duplicate lines. Centurion |
|
|
|
|
|
#9 |
|
programmer
Join Date: Nov 2008
Location: In the present moment
Posts: 19
Rep Power: 0
![]() |
Re: Selecting every third line
Linux makes it easier.
please give 9 line sample of your text. ![]() Chetanji |
|
|
|
|
|
#10 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |