Awk: greater than sign is working upside down

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Awk: greater than sign is working upside down
# 1  
Old 04-23-2018
Awk: greater than sign is working upside down

Hi,

I noticed a weird behaviour with awk.

input:
Code:
A|B|1-100|blabla_35_40_blabla;blabla_53_60_blabla;blabla_90_110_blabla

Objective:
For each string separated by ';' in $4, if the first and second numbers are included in the interval in $3, then print "TRUE". Otherwise print "FALSE".

In order to get this output:
Code:
A|B|1-100|blabla_35_40_blabla|TRUE
A|B|1-100|blabla_53_60_blabla|TRUE
A|B|1-100|blabla_90_110_blabla|FALSE

My code:
Code:
awk '
BEGIN{FS=OFS="|"}
{
    START=FINISH=$3
    gsub(/-.+$/,"",START)                         # isolate the first number in the interval in $3
    gsub(/^.+-/,"",FINISH)                       # isolate the second number in the interval in $3

    a=split($4,b,";")
    for(i=1; i<=a; i++){
        beg=gensub(/(^[^_]+_)([0-9]+)(_.+$)/,"\\2","g",b[i])                      # isolate first number in $4
        end=gensub(/(^[^_]+_[0-9]+_)([0-9]+)(_.+$)/,"\\2","g",b[i])          # isolate second number in $4

        if(beg > START && end < FINISH){
            print $1 FS $2 FS $3 FS b[i] FS "TRUE"
        }
        else{
            print $1 FS $2 FS $3 FS b[i] FS "FALSE"
        }
    }
}' input


But I get:
Code:
A|B|1-100|blabla_35_40_blabla|FALSE
A|B|1-100|blabla_53_60_blabla|FALSE
A|B|1-100|blabla_90_110_blabla|FALSE

---------- Post updated at 08:23 AM ---------- Previous update was at 07:57 AM ----------

It actually works when I use arrays instead of 'gsub /gensub'. So I assume awk treats the number as numbers with arrays and as text with gensub maybe
# 2  
Old 04-23-2018
Code:
awk '
BEGIN{FS=OFS="|"}
{
    split($3,interval, "[-]")

    a=split($4,string, ";")

    for(i=1; i<=a; i++){
       b=split(string[i], numbers, "_")
       print $1, $2, $3, string[i], (numbers[2] >= interval[1] && numbers[3] <= interval[2]) ? "TRUE" : "FALSE";
    }
}' input

# 3  
Old 04-23-2018
@OP, try:
Code:
if(beg+0 > START+0 && end+0 < FINISH+0){

# 4  
Old 04-23-2018
rdrtx1's proposal works well on the sample data line posted, but it assumes that the second number is always greater than the first, so that if N2 is less than the high boundary, so is N1, and if N1 is greater than the low boundary, so is N2, and thus omits two boundary tests.
If that assumption is not valid, and nothing has been said it is, then use
Code:
awk '
BEGIN{FS=OFS="|"}
{
    split($3,interval, "[-]")

    a=split($4,string, ";")

    for(i=1; i<=a; i++){
       b=split(string[i], numbers, "_")
       print $1, $2, $3, string[i], (numbers[2] >= interval[1] && numbers[2] <= interval[2] && numbers[3] >= interval[1] && numbers[3] <= interval[2]) ? "TRUE" : "FALSE";
    }
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Percentage sign causing awk problems

looks like awk gets confused when there's a % next to a number. command im running: awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log ... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. UNIX for Dummies Questions & Answers

Insert sign every n character (awk)

Hi, For example, I would like to insert a pipe every 4 characters for each second field (including after the last block). input (coma separated): line1,AAAABBBBCCCCDDDDEEEE line2,FFFFGGGGHHHHIIIIJJJJ output: line1,AAAA|BBBB|CCCC|DDDD|EEEE| line2,FFFF|GGGG|HHHH|IIII|JJJJ| my... (2 Replies)
Discussion started by: beca123456
2 Replies

3. Shell Programming and Scripting

awk to get values greater than

data.txt August 09 17:16 2013 August 09 17:17 2013 August 09 17:19 2013 August 09 17:20 2013 August 09 17:21 2013 August 09 17:22 2013 August 09 17:23 2013 August 09 17:24 2013 to print from a point in this file, to the end of the file, i type: awk '/August 09 17:22/,0' data.txt. ... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. UNIX for Dummies Questions & Answers

upside-down reversed desktop

I got some problem at startup before login such screen splashing, startup blocking on "checking battery status" and screen going black. Now, that I've solved the above problems I see the desktop screen is upside-down n reversed as well. I tried re-installing nvidia-current, re-installing... (1 Reply)
Discussion started by: dr_mabuse
1 Replies

5. Emergency UNIX and Linux Support

SSH Is hanging and the & sign does not seem to be working

I am having an issue where I am do an SSH to about 30 servers one at a time however my script is getting hung up sometimes on the SSH. I thought the & at the end as seen below would fire it and move on but that does not seem to be working. #!/bin/sh for remsys in trumpetsnail angel... (15 Replies)
Discussion started by: LRoberts
15 Replies

6. Shell Programming and Scripting

Need to Grep or awk a logfile for greater than value

Hello all Hoping someone would be kind enough to suggest a solution to a problem i have, and see if maybe i can even do this without a script. Essentially i have a very large log file, and within it each line had a value called TTMT, and it records a variable number in the following way, so... (6 Replies)
Discussion started by: 1905
6 Replies

7. UNIX Desktop Questions & Answers

rxvt: backgroundPixmap upside down

Anyone have any advice? I've tried just about every option, tweak, re-phrase in the man page, FAQ, and such, but there doesn't seem to be any information about what to do when your AfterImage-enabled RXVT displays its background image (xbm format converted from PNG by ImageMagick v6.-something)... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

8. Shell Programming and Scripting

AWK greater than?

Sorry for such a basic question, but I have spent hours trying to work this out! I need an awk command (or similar) that will look at a text file and output to the screen if the 4th column of each line has a value greater than or equal to x. data.txt This is the 1 line This is the 2 line This... (6 Replies)
Discussion started by: dlam
6 Replies

9. Shell Programming and Scripting

Searching for + sign in a string using awk

Hi All, My query is: I have string say xyz+ how to determine that whether it ends with a + sign or not using awk command. (5 Replies)
Discussion started by: satyajit2512
5 Replies

10. Shell Programming and Scripting

AWK greater than 200

I need to grep out usernames with UID's greater than 200 out of the /etc/passwd file for a server migration. It is the third field however I am not sure to to accomplish this. I tried to search the forums but did not seem to find an answer (might have over looked it). Any help would be... (2 Replies)
Discussion started by: insania
2 Replies
Login or Register to Ask a Question