awk with multiple condition

Hi Guys,

I just wanted to print all the lines execpt 1st and 3rd line. For that i wrote a awk command,

awk 'NR != 1 || NR != 3 {print $0}' c.out

the command is working if i give an equal to instead of not equal to.

In the case of not equal to, it gives me the entire file.

Can you anybody point out where i am going wrong.

Magesh

Should be AND, no?

&&

i do not want both the 1st and 3rd line.. thats why i used a OR (||) condition

awk 'NR != 1 && NR != 3' c.out

Or with sed...

sed "2,3d" c.out

Too short :eek: did you check the your output and the OP requirement ?

sed '1d;3d' file

My mistake, sorry.

ya guys, thanks for the solution..
So, its not possible with awk ???

Danmero already provided an awk solution.

Here's another one:

awk 'NR!~/^[13]$/' infile
awk '(NR != 1 && NR != 2) {print $0}' < filename>

, it should be &&

Guys, i think i have not explained myslef well..

I dont want an AND (&&) condition. I want an OR (||) condition.

The problem is awk is working with AND condition but it is not working with || condition
When i am using || condition, it is giving all the records instead of the two records i needed.

Hope i made myself clear

So what exactly is wrong with this solution? It's just boolean logic: !(line 1 or line 3) == !line 1 and !line 3

actually i want !line 1 or !line 3.. which is not working

What should be the output given the following input:

zsh-4.3.10[t]% print -l line{1..5}                               
line1
line2
line3
line4
line5

Which lines should appear?

Given your last comments, I suppose that you don't want this:

zsh-4.3.10[t]% print -l line{1..5}|awk 'NR !~ /^[13]$/'
line2
line4
line5

for an example,

d02 $ cat file1
4 3.855143e-07 3.855143e-07
6 1.927572e-07 5.782715e-07
7 3.855143e-07 9.637858e-07
8 1.927572e-07 1.156543e-06
30 4.018987e-05 8.086162e-05
d02 $ awk '(NR != 1 || NR != 2) {print $0}' file1
4 3.855143e-07 3.855143e-07
6 1.927572e-07 5.782715e-07
7 3.855143e-07 9.637858e-07
8 1.927572e-07 1.156543e-06
30 4.018987e-05 8.086162e-05

I want only the first and third row, but it is returning all the records

So it's quite the opposite :slight_smile:

% cat infile 
4 3.855143e-07 3.855143e-07
6 1.927572e-07 5.782715e-07
7 3.855143e-07 9.637858e-07
8 1.927572e-07 1.156543e-06
30 4.018987e-05 8.086162e-05
% awk 'NR ~ /^[13]$/' infile
4 3.855143e-07 3.855143e-07
7 3.855143e-07 9.637858e-07

you were awesome.. i apolgise for taking this in a merry go round style..

Can i know what is wrong with this awk statement?

awk 'NR = 1 || NR == 3 {print $0}' file1

Thanks again

To achieve what you want, you should change = to ==:

NR == 1

pludi his solution resolved my problem.. but i like to know why my code is not working.

awk 'NR = 1 || NR == 3 {print $0}' file1

---------- Post updated at 02:39 PM ---------- Previous update was at 02:38 PM ----------

you the man.. that solved my issue....

Thanks dude..