Deletion of strings depending of the value in a specific column

Happy new year guys!

I have a new question for you!

Ubuntum, Bash version: 4.3.46 Bash

I have a csv file, composed from several columns.

 INPUT
x1 x2 x3 x4 x5
as 10 32 T 3
sd 50 7 B 48
af 18 98 D 25
fe 75 55 P 15

I want to cancel the strings where the x2 and/or x3 values are <=10 ...

 OUTPUT
x1 x2 x3 x4 x5
af 18 98 D 25
fe 75 55 P 15

I tried to use grep ... but it isn't a safe way because I can't specify at the system that it have to read the values from the columns x2 and x3 ...

I also found this code

awk -F, '{if ($2 == "10") print $0;}' inputfileX.csv > outputfileX2.csv
awk -F, '{if ($3 == "10") print $0;}' inputfileX.csv > outputfileX3.csv

... and I think it works... this codes save my strings with x2 = 10 or x3 =10... They are to indipendente conditions

How can I merge those codes to one string? in the way to have two conditions in the same code?

Best regards!!!

awk '$2>10 && $3>10' myFile

With awk you can access the fields

awk 'NR==1 || $2<=10 || $3<=10' inputfile
awk 'NR==1 || !($2<=10 || $3<=10)' inputfile

NR is line number
|| is OR
! is NOT
The default action is {print}

1 Like

Many thanks guys! Awk seems too easy to use... For now i use only some bash commands...

Please be aware that there can be a serious difference between AND & OR when you specify "where the x2 and/or x3 values" ... assuming OR for now. And, you seem to want to specify the columns by their header value, here x2 and x3 .
Try:

awk -vDISCR="x2=10;x3=10" '
NR == 1 {for (n = split (DISCR, TMP1, ";"); n>0; n--)   {split (TMP1[n], TMP2, "=")
                                                         TMP3[TMP2[1]] = TMP2[2]
                                                        }
         for (i=1; i<=NF; i++) if ($i in TMP3) THRSH = TMP3[$i]
        }

        {P = 1
         for (i in THRSH) if ($i <= THRSH) P = 0
        }

P

' file
x1 x2 x3 x4 x5
af 18 98 D 25
fe 75 55 P 15
1 Like

Here is one with bash builtins:

while read x1 x2 x3 x4 x5 junk
do
  if ! { [[ $x2$x3 != *[!0-9]* ]]  &&
    { [[ $x2 -le 10 ]] || [[ $x3 -le 10 ]]
    }
  }
  then
    printf "%s\n" "$x1 $x2 $x3 $x4 $x5"
  fi
done < inputfile

OK!!!

Post solved