Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WHy the awk output is so strange? Any idea?

65 views
Skip to first unread message

mohsen...@gmail.com

unread,
Aug 10, 2017, 4:34:20 PM8/10/17
to
Hi all

I use Matlab to get visualized the results of our test measurements. For this purpose I write GUIs in Matlab. I generate the buttons, fields, checkboxes, axes, radio buttons . . . using “UICONTROL” command. I could use also GUIDE to generate the GUI, but it is not so precise for having really aligned elements on the GUI. Therefore I prefer the UICONTROL command.
Once I am finished with one project and want to begin with a new one using the old script/GUI and moving around its buttons and fields, it is a big action to calculate and edit all the position values (x, y, width and height) of each button, fields and so on.
Therefore I have written an AWK script (mv_matlab_buttons.awk) to move a group of Matlab elements which are lying near to each other from one side to another side of the GUI. As an example below, the file GUI_Code.m is shown with two elements. See the two lines 10 and 22 with the position values. On line 10 is the second last line and on line 22 is the last line of the command.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gh.btnShowPOBs = uicontrol(...
gh.MainWindow,'Style','pushbutton',...
'String','ShowPOBs',...
'FontWeight','bold',...
'FontSize', 10,...
'Units','pixel',...
'BackgroundColor', [0.8 0 0], ...
'ForegroundColor', [1 1 1], ...
'Position',[475 FigY + 60 100 25],...
'Callback',{@btnShowPOBs});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gh.btnGenPOBs = uicontrol(...
gh.MainWindow,'Style','pushbutton',...
'String','Gen POBs',...
'FontWeight','bold',...
'FontSize', 10,...
'Units','pixel',...
'BackgroundColor', [0.6 0 0], ...
'ForegroundColor', [1 1 1], ...
'Callback',{@btnGenPOBs},...
'Position',[475 FigY - 30 100 25]);

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
After having used the AWK script with the command line:
mv_matlab_button.awk GUI_Code.m 50 20 0 0
It means: move the 2 buttons 50 in X and 20 in Y direction and do not change their widths and heights
I got the modification in the following file:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Out_GUI_Code.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gh.btnShowPOBs = uicontrol(...
gh.MainWindow,'Style','pushbutton',...
'String','ShowPOBs',...
'FontWeight','bold',...
'FontSize', 10,...
'Units','pixel',...
'BackgroundColor', [0.8 0 0], ...
'ForegroundColor', [1 1 1], ...
'Position', [525 FigY+60+20 100 25], ...
'Callback',{@btnShowPOBs});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gh.btnGenPOBs = uicontrol(...
gh.MainWindow,'Style','pushbutton',...
'String','Gen POBs',...
'FontWeight','bold',...
'FontSize', 10,...
'Units','pixel',...
'BackgroundColor', [0.6 0 0], ...
'ForegroundColor', [1 1 1], ...
'Callback',{@btnGenPOBs},...
'Position', [525 FigY-30+20 100 25
]);

==========================================================================
As you can see on the last line here, there is a failure. The string “]);” should be at the end of the Position line. This mistake happens, when the Position line is the last line of the UICONTROL command.
I have also printed the relevant values on the shell to see what the problem is. The last line of the New_Position seems to be somehow overwritten as you see in the following print commands:


mohsen@mohsen-PC /cygdrive/c/Users/mohsen/Desktop/neu
$ mv_matlab_button.awk GUI_Code.m 50 20 0 0
POSITION ==========> 475 FigY + 60 100 25
NR = 10
New_Position Before = 'Position', [525 FigY+60+20 100
PAR[4] = 25
New_Position After = 'Position', [525 FigY+60+20 100 25], ...

POSITION ==========> 475 FigY - 30 100 25
NR = 22
New_Position Before = 'Position', [525 FigY-30+20 100
PAR[4] = 25
]);_Position After = 'Position', [525 FigY-30+20 100 25


I am using AWK under CYGWIN and Windows 10.
Do you have any idea or can you see anything why the last output line
is so strange?

Thanks a lot for your help in advance.
Mohsen Owzar

And my AWK script is as the following:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#! /usr/bin/gawk -f
# Filename: mv_matlab_button.awk
# Author: Mohsen Owzar
# Date: 09.08.2017
####################################################################
# Description:
#
# Usage: mv_matlab_button.awk <Matlab GUI Code> <x y w h>
# mv_matlab_button.awk GUI_Code.m 50 20 0 0
#
####################################################################
# BEGIN Part
####################################################################
BEGIN {
FILE = ARGV[1]
par[1] = ARGV[2]
par[2] = ARGV[3]
par[3] = ARGV[4]
par[4] = ARGV[5]

delete ARGV[2]
delete ARGV[3]
delete ARGV[4]
delete ARGV[5]

DIGIT = "^[0-9]+$"
SIGN = "[-|+|*|/]"
LAST = "[;$]"

OutputFile = "Out_" FILE
print OutputFile > OutputFile
}
####################################################################
# MAIN Part
#####################################################################
{ # Find the line with "uicontrol" and get button name
if (/[ \t]*=[ \t]*uicontrol/) {Button = $1}

# If the line has Position than modify it, otherwise print the line
if (/Position/) {modifyPosition(par, Button)}
else {print $0 >> OutputFile}
}

####################################################################
# END Part
####################################################################
END {
print "\nDas File wurde modifiziert und als Folgendes abgespeichert:"
print "==========================================================="
print OutputFile
print ""
}

####################################################################
# function modifyPosition
####################################################################
function modifyPosition(par, but) {
split($0, ARRAY, ",")
Len = length(ARRAY[1])

POSITION = ARRAY[2]

####################################################################
# Check if the position line is the last line
####################################################################
LastLine = 0
if (POSITION ~ LAST) {
LastLine = 1
}

####################################################################
# Remove the left braket
####################################################################
sub(/\[/, "", POSITION)

############################################################
# Remove the last braket
############################################################
if (LastLine == 0) {sub(/\]/, "", POSITION)}
else {sub(/\]\);/, "", POSITION)}

print "POSITION ==========> " POSITION

split(POSITION, ARR)
if (POSITION ~ SIGN) {
POS = gensub(/[ \t]*([\+|\-|\*|\/])[ \t]*/, "\\1", "g", POSITION)
}

####################################################################
# Split four parts of the position line, x, y, w and h
####################################################################
N = split(POS, ARR)

for (i = 1; i <= N; i++) {
####################################################################
# If the part is a number than add the ARR[i] to parameter
# par[i] if it is non zero, otherwise take only ARR[i]
####################################################################
if (ARR[i] ~ DIGIT) {
if (par[i] != 0) {
PAR[i] = ARR[i] + par[i]
}
else {
PAR[i] = ARR[i]
}
}
####################################################################
# The part is not a number. It is an expression which has
# to be kept as expression by adding "+" Sign and the par[i]
# if par[i] is positive, otherwise concatenating ARR & par
####################################################################
else {
if (par[i] != 0) {
if (par[i] > 0) {PAR[i] = ARR[i] "+" par[i]}
else {PAR[i] = ARR[i] par[i]}
}
else {PAR[i] = ARR[i]}
}
}

LEER = ""
for (i = 1; i <= Len-10; i++) {
LEER = LEER " "
}
####################################################################
# Joining of the four parts of the position line
# At first the beginning of the line
####################################################################
New_Position = LEER "'Position', ["
####################################################################
# Next adding the first three parts with a space afterwards
####################################################################
for (i = 1; i <= 3; i++) {
New_Position = New_Position PAR[i] " "
}
####################################################################
# Next adding the last part of the "position line"
# depending of the position of the "position line"
# Is it in the middle of the UICONTRL-Command add -> "], ..."
# Is it as the last line of the UICONTRL-Command add -> "]);"
####################################################################
printf "NR = %s\n", NR
if (LastLine == 1) {
printf "New_Position Before = %s\n", New_Position
printf " PAR[%s] = %s\n", N,PAR[N]

New_Position = New_Position PAR[N] "]);"

printf "New_Position After = %s\n", New_Position
printf "\n"
}
else {
printf "New_Position Before = %s\n", New_Position
printf " PAR[%s] = %s\n", N,PAR[N]

New_Position = New_Position PAR[N] "], ..."

printf "New_Position After = %s\n", New_Position
printf "\n"
}

print New_Position >> OutputFile
}

Janis Papanagnou

unread,
Aug 10, 2017, 8:51:03 PM8/10/17
to
On 10.08.2017 22:34, mohsen...@gmail.com wrote:
> Hi all
>
> I use Matlab to get visualized the results of our test measurements. [...]

You will probably get more attention if you restrict your awk question(s)
to the significant parts with testcases reduced to the necessary.

Janis

mohsen...@gmail.com

unread,
Aug 11, 2017, 2:09:33 AM8/11/17
to
Dear Janis

You are right.
I wanted to attach my awk script, the input and output files to my posting.
But I couldn't find any buttons regarding to attach symbol.
Without these three files, my posting would be much more less.

Regards
Mohsen

Janis Papanagnou

unread,
Aug 11, 2017, 2:35:15 AM8/11/17
to
On 11.08.2017 08:09, mohsen...@gmail.com wrote:
> Am Freitag, 11. August 2017 02:51:03 UTC+2 schrieb Janis Papanagnou:
>> On 10.08.2017 22:34, mohsen...@gmail.com wrote:
>>> Hi all
>>>
>>> I use Matlab to get visualized the results of our test measurements. [...]
>>
>> You will probably get more attention if you restrict your awk question(s)
>> to the significant parts with testcases reduced to the necessary.
>
> You are right.
> I wanted to attach my awk script, the input and output files to my posting.

It's not a technical question where to put attachments, it's
about the amount and quality of provided data and information.

> But I couldn't find any buttons regarding to attach symbol.
> Without these three files, my posting would be much more less.

To re-emphasise; "with testcases *reduced* to the *necessary*".
(Note that that's also one approach to find the actual problem.)
You have a lot of irrelevant text and code. When you are asking
"Do you have any idea or can you see anything why [...]", don't
expect that folks jump in to analyse a 280 line posting. Don't
get me wrong; you can post what you like. It's just that (as I
said before) you may not likely get the attention you expect.
Maybe you're lucky and someone has nothing better to do and will
anylyse your code, data, and questions. Otherwise I suggest to
strip your posting.

Janis

>
> Regards
> Mohsen
>

mohsen...@gmail.com

unread,
Aug 11, 2017, 3:03:19 AM8/11/17
to
Dear Janis
Actually, after I have posted all these unnecessary Stuff, as you mentioned, the only important thing here is, why the printf command in the case of LastLine == 1 adds a "\n" or newline character to the output, when the line of the position values is the last line in UICONTROL command.

Regards
Mohsen

Ed Morton

unread,
Aug 11, 2017, 5:37:33 AM8/11/17
to
To be able to answer that question, though, we'd have to read and understand all
that unnecessary stuff. Come up with a minimal example (script+input+output)
that demonstrates the problem you're asking about and then post that and you'll
find a lot more people willing to take the much less time then required to try
to understand your question. Chances are though that, as Janis mentioned, by
going through that process you'll answer the question for yourself and won't
need our help.

Ed.

Dave Sines

unread,
Aug 11, 2017, 8:17:57 AM8/11/17
to
mohsen...@gmail.com wrote:

> As you can see on the last line here, there is a failure. The string
> “]);” should be at the end of the Position line. This mistake happens,
> when the Position line is the last line of the UICONTROL command.
> I have also printed the relevant values on the shell to see what the
> problem is. The last line of the New_Position seems to be somehow
> overwritten as you see in the following print commands:

> ]);_Position After = 'Position', [525 FigY-30+20 100 25

> I am using AWK under CYGWIN and Windows 10.
> Do you have any idea or can you see anything why the last output line
> is so strange?

Carriage returns in the input file.

Set RS and ORS to a carriage return and line feed pair in the BEGIN section

RS = ORS = "\r\n"

mohsen...@gmail.com

unread,
Aug 11, 2017, 9:09:43 AM8/11/17
to

> > ]);_Position After = 'Position', [525 FigY-30+20 100 25
>
> > I am using AWK under CYGWIN and Windows 10.
> > Do you have any idea or can you see anything why the last output line
> > is so strange?
>
> Carriage returns in the input file.
>
> Set RS and ORS to a carriage return and line feed pair in the BEGIN section
>
> RS = ORS = "\r\n"


Dear Dave

Wow, it has worked.
After I put the above line into the BEGIN-section, I couldn't see any
Failure more in the output file.

I cannot understand, why the code functions fine, when the position
values lines are not the last line in the UICONTROL command.

When they are the last line, then it puts a newline in the output.
I checked the input file for some invisible control signs, but I couldn't find any.

Thanks a lot for your help.

Reagrds
Mohsen

Ed Morton

unread,
Aug 11, 2017, 10:55:31 AM8/11/17
to
That will only work in GNU awk due to multi-char RS and will only work on
platforms where the underlying C primitives allow gawk to see the `\r`s so make
sure to add `BINMODE=3` if you're going to do that for portability across
platforms and mention that it is gawk-only.

Running dos2unix or similar on the input file before running awk or any other
UNIX tool on it is usually a better choice for handling \r\n newlines unless the
file has embedded line feeds (\n) within fields or uses them as the FS.

Ed.

mohsen...@gmail.com

unread,
Aug 12, 2017, 6:31:30 AM8/12/17
to
Thanks Ed

You are right. It is better to use at first dos2unix and then use AWK.

Regards
Mohsen

Anton Treuenfels

unread,
Aug 15, 2017, 1:08:20 AM8/15/17
to
I haven't got time right now to produce any sample code, but for now it
seems to me you are doing much more work than you need to. Offhand there
doesn't seem to be any real need to worry about whether or not
'position' is the last line of the control or not. What you're really
interested in is the four groups between the left and right brackets.
One approach would be to look at each group one by one without doing
anything to them. Instead use the information to build a replacement
string incorporating the changes you specify. When it's finished,
substitute it for the original contents of the brackets. That way you
never touch anything outside the brackets, and so whatever the original
line ends with stays intact.

- Anton Treuenfels
0 new messages