Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Awk: greater than sign is working upside down Post 303016246 by beca123456 on Monday 23rd of April 2018 09:23:10 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
VARIANT_OR(3)								 1							     VARIANT_OR(3)

variant_or - Performs a logical disjunction on two variants

SYNOPSIS
mixed variant_or (mixed $left, mixed $right) DESCRIPTION
Performs a bitwise OR operation. Note that this is slightly different from a regular OR operation. PARAMETERS
o $left - The left operand. o $right - The right operand. Note As with all the variant arithmetic functions, the parameters for this function can be either a PHP native type (integer, string, floating point, boolean or NULL), or an instance of a COM, VARIANT or DOTNET class. PHP native types will be converted to variants using the same rules as found in the constructor for the "VARIANT" class. COM and DOTNET objects will have the value of their default property taken and used as the variant value. The variant arithmetic functions are wrappers around the similarly named functions in the COM library; for more information on these functions, consult the MSDN library. The PHP functions are named slightly differently; for example variant_add(3) in PHP cor- responds to VarAdd() in the MSDN documentation. RETURN VALUES
Variant OR Rules +------------+--------------------+---+ |If $left is | | | | | | | | | If $right is | | | | | | | | then the result is | | | | | | +------------+--------------------+---+ | | | | | TRUE | | | | | | | | | | | | | TRUE | | | | | | | | | | | | TRUE | | | | | | | | | | | TRUE | | | | | | | | | | | | | FALSE | | | | | | | | | | | | TRUE | | | | | | | | | | | TRUE | | | | | | | | | | | | | NULL | | | | | | | | | | | | TRUE | | | | | | | | | | | FALSE | | | | | | | | | | | | | TRUE | | | | | | | | | | | | TRUE | | | | | | | | | | | FALSE | | | | | | | | | | | | | FALSE | | | | | | | | | | | | FALSE | | | | | | | | | | | FALSE | | | | | | | | | | | | | NULL | | | | | | | | | | | | NULL | | | | | | | | | | | NULL | | | | | | | | | | | | | TRUE | | | | | | | | | | | | TRUE | | | | | | | | | | | NULL | | | | | | | | | | | | | FALSE | | | | | | | | | | | | NULL | | | | | | | | | | | NULL | | | | | | | | | | | | | NULL | | | | | | | | | | | | NULL | | | | | | +------------+--------------------+---+ SEE ALSO
variant_and(3), variant_xor(3). PHP Documentation Group VARIANT_OR(3)
All times are GMT -4. The time now is 12:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy