LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2010, 02:20 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193

Whilst I agree this will work, my question still remains about how do you get the maximums?
I assume not every square has 9 and 15
 
Old 05-12-2010, 07:15 AM   #17
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
@Dillusionist

I don't get it.

Howcome the quote on top says "between 1 and ymax" and your code says "greater than 0 and <= ymax"? That's not the same (unless the OP's data file contains only integers).

Also the OP's pseudo code contains this:

Code:
$yyy == 0 && $yyy > 0 && $xxx >= $xmax ? bottom :\
y == 0 and y > 0

?!?

That's never going to evaluate to true.

Last edited by MTK358; 05-12-2010 at 07:16 AM.
 
Old 05-12-2010, 10:37 AM   #18
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
In perl and c the code does
but i do not know about awk anyway
the $ymax and $xmax, are the the upper right coordinates of the square.
$xxx and $yyy, are more legible, and they stand there because the numbers are large, not digits.
As what i want to do, well, is to determine if a given point goes into the left, rigth, top, or bottom boundary, and putting this into a label in the $6th column of the output data

i get the maximums like this:

Code:
y_max=`sed -e '1,/flag/d' -e '/glaf/,$d' $1 | awk '{print $8}' | sed 's/)//'`
x_max=`sed -e '1,/flag/d' -e '/glaf/,$d' $1 | awk '{print $9}' | sed 's/(//'`
They are on the same row different column, before the data in the log file.



By the way reading the code, you are right it is a typo

Code:
xxx=$6
yyy=$8
  $xxx == 0 && $yyy > 0 && $yyy >= $ymax ? left :\
  $yyy == 0 && $xxx > 0 && $xxx >= $xmax ? bottom :\
  $yyy == $ymax && $xxx > 0 && $xxx >= $xmax ? top :\
  $xxx == $xmax && $yyy > 0 && $yyy >= $ymax ? right :o_0
Or:
Code:
xxx = 0 && 0 < yyy <= ymax ? left:\
yyy = 0 && 0 < xxx <= xmax ? bottom:\
yyy = ymax && 0 < xxx <= xmax? top:\
xxx = xmax && 0 < yyy <= ymax?right:o_0
It is if in short form: condition?then_code:else_code
 
Old 05-12-2010, 10:43 AM   #19
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
That is right thanks, i was doing copy'n'paste
And thanks for the awk code, i will try it , within the hour
And yes they are integers, large ones, to avoid floating data, but they are scaled by the program which takes this data as input.

Quote:
Originally Posted by Disillusionist View Post
Working from your pseudo code, I have corrected your test statements:

Code:
awk '
{if ( $6 == 0 && $8 > 0 && $8 <= 9 ) { $6 = "left" } else {
  if ( $8 == 0 && $6 > 0 && $6 <= 15 ) { $6 = "bottom" } else {
    if ( $8 == 9 && $6 > 0 && $6 <= 15 { $6 = "top" } else {
      if ( $6 == 15 && $8 > 0 && $8 <= 9 { $6 = "right" }
      }
    }
  }
} {print}'
 
Old 05-12-2010, 10:44 AM   #20
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Quote:
Originally Posted by MTK358 View Post
I also don't understand, the OP gave us a broken program to process some data and doesn't even say how that data is formatted.
Ok a stupid question what does OP stands for?

And it is not broken, it is all the program i am using, few lines you might see but they are actually the ones on my script:
[code]
#!/bin/bash
tempo2=$2
touch $tempo2
rm $tempo2
touch $tempo2
clear
sed -e '1,/flag1/d' -e '/flag2/,$d' $1\
| sed '/power/,+3d' \
| tr "\n" " " \
| sed 's/^ *- *//' \
| tr "-" "\n" \
| awk '{print "dat", $1, $17, 10, 20, $11, 0, $12}'\
[\code]
If you run it on the log file to process, you get eight columns of data, as described below

Last edited by patolfo; 05-12-2010 at 10:51 AM.
 
Old 05-12-2010, 10:56 AM   #21
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
OP means Original Poster or Original Post.

And i still don't understand all this.
 
Old 05-12-2010, 11:07 AM   #22
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
So can we see the data, all be it fake, in the correct format as being given bits and pieces kinda makes it hard to help?

Assuming you are getting the max values then the awk nested ifs should work.
However, looking at the nesting I am not sure it is necessary as I would imagine only one if can be true at a time.

PS. awk has this format too - selector ? if-true-exp : if-false-exp

Last edited by grail; 05-12-2010 at 11:13 AM.
 
Old 05-12-2010, 11:18 AM   #23
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Question bash var into awk code

OP=original poster alright please do not tease me

One last minuet question:

how do i pass the $ymax, $max, from bash to AWK?
more specific, how do i pass it to this code, instead of 9 and 15?
I tried single quotes but did not work :s
Code:
awk '
{if ( $6 == 0 && $8 > 0 && $8 <= 9 ) { $6 = "left" } else {
  if ( $8 == 0 && $6 > 0 && $6 <= 15 ) { $6 = "bottom" } else {
    if ( $8 == 9 && $6 > 0 && $6 <= 15 { $6 = "top" } else {
      if ( $6 == 15 && $8 > 0 && $8 <= 9 { $6 = "right" }
      }
    }
  }
} {print}'
 
Old 05-12-2010, 11:25 AM   #24
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
You have two choice:

1. Use awk variables - awk -v ymax=$bash_ymax

2. Incorporate into script (but with them being used so many times I think this could get messy) - {short example}
Code:
awk '{if($6 == 0 && $8 < '"$bash_ymax"')<do_blah>}' file
 
Old 05-12-2010, 11:28 AM   #25
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Or you can initialize them in the script and use them by name later:

Code:
awk 'BEGIN { xmax=15; ymax=9 } { put code here }'
The "BEGIN" and "END" special patterns are run before and after the file is scanned, respectively.

Last edited by MTK358; 05-12-2010 at 11:29 AM.
 
Old 05-12-2010, 11:53 AM   #26
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
And how do i pass the max values to awk?...

Give me some minutes to come up with fake data

Yes only one condition is expected to be true, you are right, so how about a case statement inside awk?.

Ok, case statement is like red magic for me

About the data mmmmmm thinking how to put it, well in a post below a put a square, it is the same problem, but with more points, and a lot of garbage, i hate log files. That is all, i think, well besides the garbage data which i do not need, the data in the log file is between two flags, somewhere in the text file, and the description of the square is somewhere in the body of the text file also, and between two flags too, thank GOD.

Anyway, the fact is: in perl i used flip-flop operator to cut the data between the flags, as with the first sed command. To be honest, i did my perl script based on these one line commands lol. Just between you and me, only the nested ifs are in perl, the rest are the piped one line commands. I used a shell wrapper, so nobody notices it, yet.


Code:
                (8,9)
         -------D--------(15,9)
         |      g       |
         |  g           |
         |          g   |
         |   g          |
         |        g     B(15,4)
         |     g        |
    (0,2)A           g  |
         |      g       |
    (0,0)-------C--------
                (7,0)
g-->garbage
file.data
garbage
   garbage
garbage
   garbage
flag_square
coordinates ( 0 0 ) ( 15 9 ) ;
glaf_square
garbage
   garbage
garbage
   garbage
garbage
   garbage
flag_data
   - data.data + data data_name_des
      + data data
      + data ( 0 2 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - data.data + data data_garbage_des
      + data data
      + data ( 10 5 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - data.data + data data_name_des
      + data data
      + data ( 8 9 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - data.data + data data_garbage_des
      + data data
      + data ( 5 3 ) data
      + data name ( %% %% ) ( ## ## ) ;
glaf_data
garbage
   garbage
garbage
   garbage
Sorry but the actual data i can not disclose

Quote:
Originally Posted by grail View Post
So can we see the data, all be it fake, in the correct format as being given bits and pieces kinda makes it hard to help?

Assuming you are getting the max values then the awk nested ifs should work.
However, looking at the nesting I am not sure it is necessary as I would imagine only one if can be true at a time.

PS. awk has this format too - selector ? if-true-exp : if-false-exp

Last edited by patolfo; 05-12-2010 at 11:56 AM.
 
Old 05-12-2010, 12:07 PM   #27
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Sorry to be a pain, and I have to go to bed as working tomorrow and already 0230
but can you show me the expected output based on this input file?

i will work on it tomorrow if you haven't already got your answer

oops ... nearly forgot ... with your output tell me what fields they represent, ie $5, $6, $7, $8,...

Last edited by grail; 05-12-2010 at 12:10 PM.
 
Old 05-12-2010, 12:46 PM   #28
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
So i had a quick look, but based on the current testing criteria (i think i got it right) the value is never set
I can only think that I am either looking at the wrong values or pulled out the wrong values.
Anyway ... some changes should be able to get it to work:
Code:
#!/usr/bin/awk -f

BEGIN{f=0}

f && /[0-9]+ [0-9]+/{_[i++]=$4;_[i++]=$5}

/coord/{xmin=$3;ymin=$4;xmax=$7;ymax=$8;f=1}

END{
    if(_[5] == 0 && _[7] > 0 && _[7] <= ymax)_[5]="left"
    if(_[7] == 0 && _[5] > 0 && _[5] <= xmax)_[5]="bottom"
    if(_[7] == ymax && _[5] > 0 && _[5] <= xmax)_[5]="top"
    if(_[5] == xmax && _[7] > 0 && _[7] <= ymax)_[5]="right"

    print _[5]"|"
    for(x=0;x<8;x++)
        printf "%s ",_[x]

    print ""
}
I put it in a script just to make it a little clearer.
btw. awk does have a case statement but I could not see any value you could use it on??
 
1 members found this post helpful.
Old 05-12-2010, 01:08 PM   #29
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Not sure if anyone here realizes it, but awk has an "else if" statement.

For example:

Code:
$ awk 'BEGIN{ if(1 == 1){ print "first" } else if(2 == 2){ print "second" } }'
will print
Code:
first
, not
Code:
first
second
, even though both statements are true.
 
1 members found this post helpful.
Old 05-12-2010, 04:11 PM   #30
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Passing xmax and ymax values into awk:

Code:
awk '
{if ( $6 == 0 && $8 > 0 && $8 ymax ) { $6 = "left" } else {
  if ( $8 == 0 && $6 > 0 && $6 <= xmax ) { $6 = "bottom" } else {
    if ( $8 == ymax && $6 > 0 && $6 <= xmax { $6 = "top" } else {
      if ( $6 == xmax && $8 > 0 && $8 <= ymax { $6 = "right" }
      }
    }
  }
} {print}' xmax=$my_xmax ymax=$my_ymax file_to_check
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk one liner help niknak Linux - Newbie 1 05-07-2009 04:52 AM
Need one liner copy command madhi Linux - Software 2 07-31-2008 01:32 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Does awk support nested if? lawrence_lee_lee Linux - Software 2 11-16-2007 11:40 PM
the 'awk' command. iconicmoronic Linux - Newbie 2 04-08-2007 12:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration