How to delete specific lines (2n+3 line, n=0,1,2...296) in a file? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to delete specific lines (2n+3 line, n=0,1,2...296) in a file?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-09-2013
phamnu phamnu is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 8 May 2013, 10:31 PM EDT
Posts: 8
Thanks: 5
Thanked 0 Times in 0 Posts
How to delete specific lines (2n+3 line, n=0,1,2...296) in a file?

Dear everyone,
I have a file with 900 lines (there is only numbers in one line, no string),
I only need the lines 2+3n (n=0,1...296), i.e line 2, 5, 8, 11...888.
I tried google but only the results such as how to delete all the odd lines or all the even lines with 'awk' command.
Thanks in advance.
Sponsored Links
    #2  
Old 02-09-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Assuming that you need those particular lines to be printed:

Code:
awk '!((NR-2)%3)' file

The Following 2 Users Say Thank You to elixir_sinari For This Useful Post:
alister (02-09-2013), phamnu (02-09-2013)
Sponsored Links
    #3  
Old 02-09-2013
phamnu phamnu is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 8 May 2013, 10:31 PM EDT
Posts: 8
Thanks: 5
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by elixir_sinari View Post
Assuming that you need those particular lines to be printed:

Code:
awk '!((NR-2)%3)' file

You are great!!!
    #4  
Old 02-09-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 6:20 AM EDT
Location: Aachen, Germany
Posts: 4,397
Thanks: 73
Thanked 1,071 Times in 1,010 Posts

Code:
$ sed -n '2~3p' file
2
5
8

Not sure this is POSIX compliant, though...
Sponsored Links
    #5  
Old 02-09-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 October 2014, 4:27 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,282
Thanks: 45
Thanked 824 Times in 651 Posts
Quote:
Originally Posted by RudiC View Post
Code:
$ sed -n '2~3p' file

Not sure this is POSIX compliant, though...
Not sure either, but the AIX sed doesn't understand it. I suppose it is a GNU extension.

I hope this helps.

bakunin
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to delete lines from a file which starts with a specific pattern mady135 Shell Programming and Scripting 3 12-02-2010 04:10 AM
Delete all lines after a specific line ? hakermania Shell Programming and Scripting 4 11-16-2010 12:41 PM
Delete specific lines from a file itzz.me Shell Programming and Scripting 4 04-08-2009 04:12 PM
Delete lines prior to a specific date in a log file. ahSher Shell Programming and Scripting 4 04-06-2009 05:19 AM
Delete specific lines in a text file dniz Programming 9 08-08-2005 08:30 AM



All times are GMT -4. The time now is 06:20 AM.