LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-26-2007, 03:35 AM   #1
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
deleting even numbered lines


In a file how can i delete line no. 2 , 4 , 6 .. etc?
 
Old 11-26-2007, 03:47 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Code:
perl -i -ne 'print if ($. % 2);' your_file
 
Old 11-26-2007, 03:48 AM   #3
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Count (\r)\n occurences. http://en.wikipedia.org/wiki/Newline
 
Old 11-26-2007, 04:19 AM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Awk:

Code:
zsh-4.3.4-dev-2% print -l {1..10}|awk NR%2
1
3
5
7
9
 
Old 11-26-2007, 04:28 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
sed -n '1~2p' file >newfile

or

sed -i '2~2d' file
to edit the file in place.

Last edited by jschiwal; 11-26-2007 at 04:31 AM.
 
Old 11-26-2007, 04:57 AM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by jschiwal View Post
sed -n '1~2p' file >newfile

or

sed -i '2~2d' file
to edit the file in place.
Nice one!
(only GNU Sed, I suppose)
 
Old 11-26-2007, 05:24 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Yes, that is a gnu extension.

Just an FYI, take a look at the respective sized of the utilities used for regex searches:
ls -l /bin/grep /bin/sed /bin/gawk /usr/bin/perl
-rwxr-xr-x 1 root root 295432 Sep 21 14:16 /bin/gawk
-rwxr-xr-x 1 root root 201056 Sep 21 14:20 /bin/grep
-rwxr-xr-x 1 root root 53056 Sep 21 14:15 /bin/sed
-rwxr-xr-x 3 root root 1301600 Nov 7 18:58 /usr/bin/perl

I was surprised that grep was almost as large as gawk. Perl is 25.5 times the size of sed.
 
Old 11-26-2007, 05:34 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk 'NR%2==1' file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting hidden lines in Wondows XP registry -- How? moxieman99 General 2 11-07-2007 07:07 AM
deleting specified lines in a huge text file ruh31 Linux - General 10 06-30-2006 03:34 AM
Deleting the lines from a file using shell scripts sharad Linux - General 1 05-22-2006 03:17 AM
An easy way of deleting lines from multipe files? delawhere Linux - General 2 04-02-2004 11:58 AM


All times are GMT -5. The time now is 04:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration