perl -i.bak -nle 's/search/replace/g unless $. & 2; print' file
Your one-liner is not going to do what you think. $. & 2 is going to evaluate to true whenever the third second bit from the right (2 is binary 100) is set. Given this input file (lines.txt)
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
adapting your one-liner to just print the lines gives
$ perl -nle 'print unless $. & 2;' lines.txt
line 1
line 4
line 5
line 8
line 9
Did you actually test your adaptation of GrandFather's idea before posting? Changing between odd and even is as simple as if $. & 1 and unless $. & 1. Perhaps you could have a look at "Bitwise And" in perlop to further your understanding. Cheers, JohnGG
Update: Corrected typo.
Update 2: Corrected huge balls-up, thanks fenLisesi. Where did I get "2 is binary 100" from?
Update 3: And thanks clinton, saw your reply after /msg from fenLisesi |