0

I have a list of data in a table. By using awk to pull out column 5, i was able to differentiate the data amongst the rows. If the entry at column 5 is more than 4, the row should be printed.

However, there is one entry that comes in the form of a string that cannot be directly compared to a number, before deciding whether it should be printed or not. That entry has a parenthesis around the number that should be compared.

Here is the example of the column 5:

on
%)
%
replica
(

0
(100.0    <= this one
0.0
10.8
13.8
12.0
16.3
13.2
12.1
11.4
10.4
0.0
devices:

From the exmaple above, i am suppose to print rows 8 and 10 to 17 of the table.

I will provide a example table(file.txt):

1        0          0        0          on
2        0          0        0          %)
3        0          0        0          %
4        0          0        0          replica
5        0          0        0          (
6        0          0        0          
7        0          0        0          0
8        0          0        0          (100.0
9        0          0        0          0.0
1        0          0        0          10.8
1        1          0        0          13.8
1        2          0        0          12.0
1        3          0        0          16.3
1        4          0        0          13.2
1        5          0        0          12.1
1        6          0        0          11.4
1        7          0        0          10.4
1        8          0        0          0.0
1        9          0        0          devices:

My attempt:

awk '{if (($5>=4)) print;
else
    NUMBER=($5 | grep -o -E '[0-9]+');
    if (($NUMBER>=4)) print' file.txt

Error:

awk: syntax error near line 2
awk: illegal statement near line 2
awk: syntax error near line 3
awk: illegal statement near line 3
  • I may not have the gsub command in my system – tthhss Jun 8 '18 at 2:25
1

You could strip off the non-numeric characters before comparing:

$ awk '{x=$5; gsub(/[^0-9.]/,"",x);} x+0>=4' file.txt
8        0          0        0          (100.0
1        0          0        0          10.8
1        1          0        0          13.8
1        2          0        0          12.0
1        3          0        0          16.3
1        4          0        0          13.2
1        5          0        0          12.1
1        6          0        0          11.4
1        7          0        0          10.4
  • hmm, what if i have other conditions such as print when $1=="PM"? can i do this: awk '{if (($1=="PM")) print; x=$5; gsub(/[^0-9.]/,"",x);} x+0>=4' file.txt I received a syntax error – tthhss Jun 7 '18 at 11:19
  • Yes that should work - although it would be more vernacular to add it as a rule like x+0>=4 || $1=="PM" (if a rule evaluates TRUE, the default action is print) – steeldriver Jun 7 '18 at 11:23
  • for some reason, the line of code you provided is giving me syntax errors. I am thinking perhaps i do not have the gsub command. May i ask what other commands can i explore? I tried {$5|egrep -v '(' }, but doesn't work – tthhss Jun 8 '18 at 3:17
  • it works now! I just had to use gawk instead of awk for the gsub to work. Thank you for your help – tthhss Jun 8 '18 at 5:48
  • @tthhss out of interest, what awk do you have? I tested the above with both gawk and mawk before posting – steeldriver Jun 8 '18 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.