You are not logged in.

#1 2010-09-27 18:48:25

Pursuit
Member
Registered: 2010-09-27
Posts: 6

Awk returns different string from what appears to be the exact same?

Hey, this is more of a general awk issue. 

I'm running Arch on an x200 Thinkpad tablet and trying to improve on the rotate script in the wiki.

Currently, the wiki's script has predefined values for the stylus/eraser devices that don't work for all systems (mine for example).  I would like to make it so that these device names are discovered by the script at runtime so that the user doesn't have to edit the script to make it work.
In order to do this, I'm using

stylus="$(xsetwacom --list | grep STYLUS | awk '{$NF="\b"; print}')"

and likewise for the eraser.

The problem is that when the script runs, it says "Cannot find device 'Serial Wacom Tablet stylus'." even though that's the exact thing I had in the script before messing with the variable assignment. 

What makes these strings different? Is there some nuance of awk that I'm missing?

Offline

#2 2010-09-27 19:01:11

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Awk returns different string from what appears to be the exact same?

What happens if you run just 'xsetwacom --list'? It doesn't look like an awk issue to me.
Mind posting the rest of the script?

Offline

#3 2010-09-27 19:49:28

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Awk returns different string from what appears to be the exact same?

Check what NF actually is, and whether you mean FS instead. wink   (EDIT: Forget get this line, I'd made a bad assumption about the script. )
Also, you don't need grep there, you can use an awk pattern to match STYLUS.

Last edited by skanky (2010-09-28 08:41:22)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#4 2010-09-27 22:58:13

Pursuit
Member
Registered: 2010-09-27
Posts: 6

Re: Awk returns different string from what appears to be the exact same?

#!/bin/bash

#### rotate.sh - A script for tablet PCs to rotate the display.

## This software is licensed under the CC-GNU GPL.
## http://creativecommons.org/licenses/GPL/2.0/

## http://wiki.archlinux.org/index.php/Tablet_PC
## REQUIRES: linuxwacom (http://linuxwacom.sourceforge.net/)
 
#### Function(s)
function set_normal {
xsetwacom set "$stylus" Rotate 0
xsetwacom set "$eraser" Rotate 0
xrandr -o normal
}

function set_right {
xsetwacom set "$stylus" Rotate 1
xsetwacom set "$eraser" Rotate 1
xrandr -o right
}

function set_left {
xsetwacom set "$stylus" Rotate 2
xsetwacom set "$eraser" Rotate 2
xrandr -o left
}

function set_inverted {
xsetwacom set "$stylus" Rotate 3
xsetwacom set "$eraser" Rotate 3
xrandr -o inverted
}

#### Variable(s)
orientation="$(xrandr --query --verbose | grep connected | grep -v dis | awk '{print $5}')"
eraser="$(xsetwacom --list | grep ERASER | awk '{$NF="\b"; print $0}')"
stylus="$(xsetwacom --list | grep STYLUS | awk '{$NF="\b"; print $0}')"
#### Main
if [ "$orientation" = "normal" ]; then
    set_right
elif [ "$orientation" = "right" ]; then
    set_inverted
elif [ "$orientation" = "inverted" ]; then
    set_left
elif [ "$orientation" = "left" ]; then
    set_normal
fi

#### EOF 

Like I said, a slight modification of the original wiki script. 

xsetwacom --list returns

Serial Wacom Tablet stylus STYLUS    
Serial Wacom Tablet eraser ERASER 

Edit:
I whipped up a short script to illustrate my problem

#! /bin/bash
therightthing="Serial Wacom Tablet stylus"
stylus="$(xsetwacom --list | grep STYLUS | awk '{$NF="\b"; print}')"
if [ "$therightthing" = "$stylus" ]; then
        echo "Equal"
else
        echo "Not Equal"
        echo "$stylus |"
        echo "$therightthing |"
fi

The vertical line is my check for trailing whitespace.  When run, it returns that they're not equal, but the strings look the exact same.
Output:

Not Equal
Serial Wacom Tablet stylus |
Serial Wacom Tablet stylus |

Last edited by Pursuit (2010-09-27 23:05:39)

Offline

#5 2010-09-27 23:35:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Awk returns different string from what appears to be the exact same?

You get 'Serial Wacom Tablet stylus STYLUS' and you want to drop the last word? Just that or am I missing something (sorry, I'm tired)?

stylus="$(xsetwacom --list | grep STYLUS | cut -d " " -f 1-4)"

You may want to examine the string more closely, what is the last character - is it a space or something else?

Last edited by karol (2010-09-27 23:37:26)

Offline

#6 2010-09-28 00:09:02

Pursuit
Member
Registered: 2010-09-27
Posts: 6

Re: Awk returns different string from what appears to be the exact same?

Yeah, I'm just looking to drop the last word, regardless of how many words there are.

Would that line work if, say, the name of the device were a word longer than mine?  Or is it dependent on the device name being four words long?

I believe that before attempting to modify the string with awk, the last character is the capital 'S' in STYLUS. Once awk is used, it should be the lowercase 's' in stylus.  Is the error maybe coming from the \b escape sequence used to remove the trailing space?

Last edited by Pursuit (2010-09-28 00:16:34)

Offline

#7 2010-09-28 01:08:13

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Awk returns different string from what appears to be the exact same?

Pursuit wrote:

Yeah, I'm just looking to drop the last word, regardless of how many words there are.

Would that line work if, say, the name of the device were a word longer than mine?  Or is it dependent on the device name being four words long?

I don't have a tablet so I can't check other that simulating it with echo. This example is trivial but rigid.

This will remove the last word:

stylus="$(xsetwacom --list | grep STYLUS | awk '{NF-=1} END {print}')"

See if it works.

Offline

#8 2010-09-28 01:30:23

Pursuit
Member
Registered: 2010-09-27
Posts: 6

Re: Awk returns different string from what appears to be the exact same?

Excellent, that's exactly what I needed smile

Thanks!

Offline

#9 2010-09-28 01:31:25

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Awk returns different string from what appears to be the exact same?

Regarding '\b' - IIRC it can't be the last character:

[karol@black ~]$ echo -e "123"
123
[karol@black ~]$ echo -e "12\b3"
13
[karol@black ~]$ echo -e "123\b"
123
[karol@black ~]$ echo -e "123" | wc -c
4
[karol@black ~]$ echo -e "12\b3" | wc -c
5
[karol@black ~]$ echo -e "123\b" | wc -c
5

I saved the outputs of those 'echo' commands to a file named 'test'.

[karol@black ~]$ cat test
123
13
123

The contents as seen in 'vim':
123
12^H3
123^H

As seen in 'less':
123
13
12

I think you can reconfigure the behavior regarding non-printable characters, but it's still a bit hackish.

The lines aren't equal because they actually look like this:

Serial Wacom Tablet stylus ^H
Serial Wacom Tablet stylus

Offline

#10 2010-09-28 02:12:51

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Awk returns different string from what appears to be the exact same?

If this works you can do w/o grep and have one pipe less:

stylus="$(xsetwacom --list | awk '{/STYLUS/; NF-=1} END {print}')"

Offline

#11 2010-09-28 08:45:41

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Awk returns different string from what appears to be the exact same?

karol wrote:

If this works you can do w/o grep and have one pipe less:

stylus="$(xsetwacom --list | awk '{/STYLUS/; NF-=1} END {print}')"

I think you also don't need the END there:

 $echo "blah blah STYLUS" | awk '/STYLUS/ {NF-=1; print}'
blah blah

Works either way, though. smile


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#12 2010-09-28 13:13:22

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Awk returns different string from what appears to be the exact same?

skanky wrote:
karol wrote:

If this works you can do w/o grep and have one pipe less:

stylus="$(xsetwacom --list | awk '{/STYLUS/; NF-=1} END {print}')"

I think you also don't need the END there:

 $echo "blah blah STYLUS" | awk '/STYLUS/ {NF-=1; print}'
blah blah

Works either way, though. smile

My gut feeling told me END is not needed, but, it was already early morning here so I had to run ;P

Offline

Board footer

Powered by FluxBB