Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a define pattern using awk

Tags:

bash

awk

I have a file with two columns and want to print the first column only if a determined pattern is not found in the second column, the file can be for example:

3   0.
5   0.
4   1.
3   1.
10  0.

and I want to print the values in the first column only if there isn't the number 1. in the second file, i.e.

3
5
10

I know that to print the first column I can use

awk  '{print $1}' fileInput >> fileOutput

Is it possible to have an if block somewhere?

like image 479
ziulfer Avatar asked Aug 30 '26 03:08

ziulfer


1 Answers

In general, you just need to indicate what pattern you don't want to match:

awk '! /pattern/' file

In this specific case, where you want to print the 1st column of lines where 2st column is not "1.", you can say:

$ awk '$2 != "1." {print $1}' file
3
5
10

When the condition is accomplished, {print $1} will be performed, so that you will have the first column of the file.

like image 77
fedorqui 'SO stop harming' Avatar answered Sep 03 '26 14:09

fedorqui 'SO stop harming'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!