LinuxQuestions.org
Review your favorite Linux distribution.
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, 04:15 PM   #31
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723

Quote:
Originally Posted by Disillusionist View Post
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
Corrected to use the else if statement and fix missing parens:

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 $0;
}' xmax=$my_xmax ymax=$my_ymax file_to_check
Much nicer, isn't it?

Last edited by MTK358; 05-12-2010 at 04:17 PM.
 
1 members found this post helpful.
Old 05-12-2010, 05:20 PM   #32
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
Thumbs up Ok guys it is done :) THANKS A BUNCH!!!!!!!!

Final script:
Code:
#!/bin/bash
clear
#	Boundaries	#
x_max=`sed -e '1,/flag_square/d' -e '/glaf_square/,$d' $1 | awk '{print $7/1000}'`
y_max=`sed -e '1,/flag_square/d' -e '/glaf_square/,$d' $1 | awk '{print $8/1000}'`
#	Data extraction	#
sed -e '1,/flag_data/d' -e '/glaf_data/,$d' $1\
| tr "\n" " "\
| tr ";" "\n"\
| sed 's/^ *- *// ; /garbage/d'\
| awk '{print "pato", $1, $17, 88/1000, 88/1000, $11/1000, 0, $12/1000}'\
| sed '$d'\
|awk '{	if( $6 == 0		&& $8 > 0 && $8 <= '"$y_max"' ){ $6="left" }\
else	if( $8 == 0		&& $6 > 0 && $6 <= '"$x_max"' ){ $6="bottom" }\
else	if( $8 == '"$y_max"'	&& $6 > 0 && $6 <= '"$x_max"' ){ $6="top" }\
else	if( $6 == '"$x_max"'	&& $8 > 0 && $8 <= '"$y_max"' ){ $6="right" } \
else {$6="o_0"} }\
{ print }'
input data file:
Code:
garbage
   garbage
garbage
   garbage
flag_square
coordinates ( 0 0 ) ( 1500 900 ) ;
glaf_square
garbage
   garbage
garbage
   garbage
garbage
   garbage
flag_data
   - A + data data_name_des
      + data data
      + data ( 0 200 ) data
      + data name ( 0 0 ) ( 2 4 ) ;
   - data.data + data data_garbage_des
      + data data
      + data ( 10 5 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - B + data data_name_des
      + data data
      + data ( 1500 400 ) data
      + data name ( 0 0 ) ( 2 4 ) ;
   - data.data + data data_garbage_des
      + data data
      + data ( 5 3 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - data.data + data data_garbage_des
      + data data
      + data ( 5 3 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - data.data + data data_garbage_des
      + data data
      + data ( 5 3 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - data.data + data data_garbage_des
      + data data
      + data ( 5 3 ) data
      + data name ( %% %% ) ( ## ## ) ;
   - C + data data_name_des
      + data data
      + data ( 700 0 ) data
      + data name ( 0 0 ) ( 2 4 ) ;
   - D + data data_name_des
      + data data
      + data ( 800 900 ) data
      + data name ( 0 0 ) ( 2 4 ) ;
glaf_data
garbage
   garbage
garbage
   garbagels
And final output:
Code:
pato A name 0.088 0.088 left 0 0.2
pato B name 0.088 0.088 right 0 0.4
pato C name 0.088 0.088 bottom 0 0
pato D name 0.088 0.088 top 0 0.9
Guys just wanted to thank you all, i really appreciate your help, i learned a lot, incredible
Thanks for your time...
If you have any ways i mean any crazy idea to improve it, you are very welcome, i just printed it and put it on my cubicle wall, hell yeah!!!
With the following label:
"We take impossibles on a daily basis, for miracles make an appointment."
 
Old 05-12-2010, 06:47 PM   #33
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Were you able to check if my code worked for you?

If you can tell me what the following values refer to I can refine to see if I can get your output:
Code:
$1 = 
$6 = 
$8 = 
$11 = 
$12 = 
$17 =
I already have [xy]max

btw. awk does not have an "else if" statement, but it does allow single line entries after an else or an if.
The lines written in your code are equivalent to:
Code:
awk '{	
if( $6 == 0		&& $8 > 0 && $8 <= '"$y_max"' ){ $6="left" }\
else
    if( $8 == 0		&& $6 > 0 && $6 <= '"$x_max"' ){ $6="bottom" }\
    else
	if( $8 == '"$y_max"'	&& $6 > 0 && $6 <= '"$x_max"' ){ $6="top" }\
        else
	    if( $6 == '"$x_max"'	&& $8 > 0 && $8 <= '"$y_max"' ){ $6="right" } \
            else {$6="o_0"} }\
{ print }'
 
Old 05-13-2010, 11:34 AM   #34
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
I am trying to put/add the script to the other script
Well i think i will dump the data to a file and process it with your code

Meanwhile the values ok,
as you flatten the lines before every ";" you get a line like this:
Code:
sed -e '1,/flag_data/d' -e '/glaf_data/,$d' $1\
| tr "\n" " "\
| tr ";" "\n"\
| sed 's/^ *- *// ; /garbage/d'\
gives this:
Code:
A + data data_name_des       + data data       + data ( 0 200 ) data       + data name ( 0 0 ) ( 200 400 ) 
B + data data_name_des       + data data       + data ( 1500 400 ) data       + data name ( 0 0 ) ( 200 400 ) 
C + data data_name_des       + data data       + data ( 700 0 ) data       + data name ( 0 0 ) ( 200 400 ) 
D + data data_name_des       + data data       + data ( 800 900 ) data       + data name ( 0 0 ) ( 200 400 )
Now using upto this line:
Code:
sed -e '1,/flag_data/d' -e '/glaf_data/,$d' $1\
| tr "\n" " "\
| tr ";" "\n"\
| sed 's/^ *- *// ; /garbage/d'\
| awk '{print "pato", $1, $17, $23/1000, $24/1000, $11/1000, 0, $12/1000}'\
| sed '$d'\
you get:
Code:
pato A name 0.2 0.4 0 0 0.2
pato B name 0.2 0.4 1.5 0 0.4
pato C name 0.2 0.4 0.7 0 0
pato D name 0.2 0.4 0.8 0 0.9
Finally the issue, of the position with this code:
Code:
sed -e '1,/flag_data/d' -e '/glaf_data/,$d' $1\
| tr "\n" " "\
| tr ";" "\n"\
| sed 's/^ *- *// ; /garbage/d'\
| awk '{print "pato", $1, $17, $23/1000, $24/1000, $11/1000, 0, $12/1000}'\
| sed '$d'\
|awk '{	if( $6 == 0				&& $8 > 0 && $8 <= '"$y_max"' ){ $6="left" }\
else	if( $8 == 0				&& $6 > 0 && $6 <= '"$x_max"' ){ $6="bottom" }\
else	if( $8 == '"$y_max"'	&& $6 > 0 && $6 <= '"$x_max"' ){ $6="top" }\
else	if( $6 == '"$x_max"'	&& $8 > 0 && $8 <= '"$y_max"' ){ $6="right" } \
else {$6="o_0"} }\
{ print }'
And you get:
pato A name 0.2 0.4 left 0 0.2
pato B name 0.2 0.4 right 0 0.4
pato C name 0.2 0.4 bottom 0 0
pato D name 0.2 0.4 top 0 0.9
Then the columns your are asking are, those counting from left to right starting in $1, and yes i did a typo in the final code, the 88 is from the original data, , not the fake one.
I am running you script give a couple of minutes to get back to you


Quote:
Originally Posted by grail View Post
Were you able to check if my code worked for you?

If you can tell me what the following values refer to I can refine to see if I can get your output:
Code:
$1 = 
$6 = 
$8 = 
$11 = 
$12 = 
$17 =
I already have [xy]max

btw. awk does not have an "else if" statement, but it does allow single line entries after an else or an if.
The lines written in your code are equivalent to:
Code:
awk '{	
if( $6 == 0		&& $8 > 0 && $8 <= '"$y_max"' ){ $6="left" }\
else
    if( $8 == 0		&& $6 > 0 && $6 <= '"$x_max"' ){ $6="bottom" }\
    else
	if( $8 == '"$y_max"'	&& $6 > 0 && $6 <= '"$x_max"' ){ $6="top" }\
        else
	    if( $6 == '"$x_max"'	&& $8 > 0 && $8 <= '"$y_max"' ){ $6="right" } \
            else {$6="o_0"} }\
{ print }'
p.s. Use this data:
garbage
garbage
garbage
garbage
flag_square
coordinates ( 0 0 ) ( 1500 900 ) ;
glaf_square
garbage
garbage
garbage
garbage
garbage
garbage
flag_data
- A + data data_name_des
+ data data
+ data ( 0 200 ) data
+ data name ( 0 0 ) ( 200 400 ) ;
- data.data + data data_garbage_des
+ data data
+ data ( 10 5 ) data
+ data name ( %% %% ) ( ## ## ) ;
- B + data data_name_des
+ data data
+ data ( 1500 400 ) data
+ data name ( 0 0 ) ( 200 400 ) ;
- data.data + data data_garbage_des
+ data data
+ data ( 5 3 ) data
+ data name ( %% %% ) ( ## ## ) ;
- data.data + data data_garbage_des
+ data data
+ data ( 5 3 ) data
+ data name ( %% %% ) ( ## ## ) ;
- data.data + data data_garbage_des
+ data data
+ data ( 5 3 ) data
+ data name ( %% %% ) ( ## ## ) ;
- data.data + data data_garbage_des
+ data data
+ data ( 5 3 ) data
+ data name ( %% %% ) ( ## ## ) ;
- C + data data_name_des
+ data data
+ data ( 700 0 ) data
+ data name ( 0 0 ) ( 200 400 ) ;
- D + data data_name_des
+ data data
+ data ( 800 900 ) data
+ data name ( 0 0 ) ( 200 400 ) ;
glaf_data
garbage
garbage
garbage
garbagels
 
Old 05-13-2010, 11:59 AM   #35
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
Code:
>grail_code tempi
awk: grail_code:20: }
awk: grail_code:20:  ^ invalid char '' in expression
Well i could not run your code... :'(
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 will debug it, to try to make it run
 
Old 05-13-2010, 07:26 PM   #36
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Just to confirm, you did run it like a normal script:
Code:
./grail_code tempi
 
Old 05-13-2010, 09:52 PM   #37
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
mmm nope i made it executable :P
tomorrow i will try that at work

Quote:
Originally Posted by grail View Post
Just to confirm, you did run it like a normal script:
Code:
./grail_code tempi
 
Old 05-14-2010, 12:19 AM   #38
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Try this one instead
Code:
#!/usr/bin/awk -f

BEGIN{
    div=1000
    f=0
}

/flag_data/{f=1;RS=";\n"}

/coord/{xmax=$7;ymax=$8}

f && /name_des/{
    if($12 == 0 && $13 > 0 && $13 <= ymax)$12="left"
    if($13 == 0 && $12 > 0 && $12 <= xmax)$12="bottom"
    if($13 == ymax && $12 > 0 && $12 <= xmax)$12="top"
    if($12 == xmax && $13 > 0 && $13 <= ymax)$12="right"

	_[$2]="pato "$2" "$18" "$24/div" "$25/div" "$12" 0 "$13/div
}


END{
    for(x in _)
        print _[x]
}
This can of course be put into your code like you have done originally.
 
  


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 08:14 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