Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-31-2006
Registered User
 

Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Remove every third line from a file

I need to remove every second and every third line from a file. My idea was to do it in two operations. First every third line, then every second line. The problem is that i can't find out how to do it. I tried to look for some sed oneliners, but couldn't find any.

Suggestions?
Sponsored Links
    #2  
Old 01-31-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Thanks: 0
Thanked 0 Times in 0 Posts
awk 'NR%3 != 0' filename

in the same way, change 3 as 2 to remove 2nd line from the file
Sponsored Links
    #3  
Old 01-31-2006
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Thanks: 0
Thanked 7 Times in 7 Posts
Is this what you mean?


Code:
$ cat bistru.txt
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
$ sed -n 'p;N;N' bistru.txt
line 1
line 4
line 7
$

Cheers
ZB
    #4  
Old 01-31-2006
Registered User
 

Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, both worked perfectly!

BTW, zazzybob, i'm not able to understand the syntax rather well. Do You mind giving a short explanation? I understand '-n' and that 'p' overruns the '-n'. From man sed i found that 'n' or 'N' means that the next line is input into the pattern space, implying that the line is suppressed. Am i completely wrong here, or?
Sponsored Links
    #5  
Old 01-31-2006
Technorati Master
 

Join Date: Mar 2005
Location: classification algos
Posts: 3,174
Thanks: 17
Thanked 26 Times in 24 Posts
Quote:
Originally Posted by mahendramahendr
awk 'NR%3 != 0' filename

in the same way, change 3 as 2 to remove 2nd line from the file
in one shot it can be done,


Code:
awk 'NR % 3 == 1' filename

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 remove the first line from a flat file ? xli UNIX for Dummies Questions & Answers 21 12-16-2008 12:37 AM
remove a line within a file new2ss UNIX for Dummies Questions & Answers 3 11-23-2008 01:34 PM
How to remove FIRST Line of huge text file on Solaris madoatz UNIX for Dummies Questions & Answers 5 06-23-2007 01:19 PM
How to remove last line of the file mani_um Shell Programming and Scripting 3 05-14-2007 08:35 AM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 02:36 PM



All times are GMT -4. The time now is 05:01 AM.