|
|
Finding numbers using regular expression
I have a CNC file which i want to comment using AWK
the single number "3" on a line means "Ignore on"
the single number "5" on a line means "Rapid traverse on"
there are also lines containing "RV22.5" which means that the plate is cut
in a angle of "22.5 degrees.
Running the script using this command on a SUN computer produces:
jp(vile)$ ./mpg01 a
Starter
a This is a test line.
a This is a test line.
ab This is a test line.
This is a blank line.
Slutter
jp(vile)$ ^C
How do I make this scrit so AWK reconices lines containing numbers and out
put the lines:
3 Ignore on
0 Program Stop
111PP050
1 1
4 Ignore off
a This is a test line.
a This is a test line.
ab This is a test line.
This is a blank line.
3 Ignore on
1193
111P P050
8364 932 350
04/03/00
4 Ignore off
....
....
The CNC file called "a" looks like this:
3
111PP050
1 1
4
a
a
ab
3
1193
111P P050
8364 932 350
04/03/00
4
3
MOVE TO PROG.ORIGO
4
81
5
-83640+
6
82
0
3
5
+42105+417 0
6
RV22.5
and the awk script called "mpg01" looks like this:
#! /bin/nawk -f
BEGIN { print
print
print "Starter"}
/^0$/ {Print $0 " Program Stop"}
/^3$/ {Print $0 " Ignore on"}
/^a.*$/ {print $0 " This is a test line."}
/^4$/ {Print $0 " Ignore off"}
/^RV.*/ {Print $0 " RV";}
/^$/ {print $0 " This is a blank line."}
# /5/ {Print $0 " Rapid traverse on"}
# /6/ {Print $0 " Rapid traverse off"}
# /7/ {Print $0 " Cutting cycle on"}
# /8/ {Print $0 " Cutting cycle off"}
# /9/ {Print $0 " Compressed air marking on"}
# /10/ {Print $0 " Compressed air marking off"}
...
...
END { print "Slutter"
print
print
print
}
--
Kind regards Jens Borchert Pedersen
Odense Steel Shipyard Ltd. PO Box 176 , DK-5100 Odense C , DENMARK
mailto:jp@oss.junkdk | http://www.oss.junkdk
phone: +45 6397 1027 | fax: +45 6397 2360
|
|
0
|
|
|
|
Reply
|
Jens
|
4/27/2004 1:45:25 PM |
|
Jens Borchert Pedersen wrote:
> I have a CNC file which i want to comment using AWK
> the single number "3" on a line means "Ignore on"
> the single number "5" on a line means "Rapid traverse on"
I've read through this a couple of times and I'm still not sure exactly
what you want. What I THINK you want (looking for lines that just
contain a "5", etc.) seems trivial and the equivalent is already handled
elsewhere in your sample script so there must be some subtlety I'm
missing. Part of the problem is there's just so much to read here -
can't you post a smaller example that illustrates the problem you're
trying to solve without all this context of CNC files, cutting plates,
etc.?
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
4/27/2004 1:55:11 PM
|
|
Ed Morton <morton@lsupcaemnt.com> wrote in
news:c6lonv$c6a@netnews.proxy.lucent.com:
Thanks for your reply
> Jens Borchert Pedersen wrote:
>> I have a CNC file which i want to comment using AWK
>> the single number "3" on a line means "Ignore on" the single number
>> "5" on a line means "Rapid traverse on"
>
> I've read through this a couple of times and I'm still not sure exactly
> what you want. What I THINK you want (looking for lines that just
> contain a "5", etc.) seems trivial and the equivalent is already handled
> elsewhere in your sample script so there must be some subtlety I'm
> missing. Part of the problem is there's just so much to read here -
> can't you post a smaller example that illustrates the problem you're
> trying to solve without all this context of CNC files, cutting plates,
> etc.?
>
> Ed.
>
>
In the AWK script i have theese two lines:
/^a.*$/ {print $0 " This is a test line."}
/^4$/ {Print $0 " Ignore off"}
The first one works fine finding lines starting with "a"
The second one does NOT work...
I want it to look for lines just having the number "4"
To my opinion it should work.
The only difference I see, is that the regular expression works fine on
strings but fails on the numbers.
Hope you can help me
--
Kind regards Jens Borchert Pedersen
|
|
0
|
|
|
|
Reply
|
Jens
|
4/27/2004 2:10:32 PM
|
|
Your regular expression in the second line matches exactly (and only)
lines with a "4" in the first position an no other data. You probably
want /^4/ which matches all lines starting with a "4" and havins any
continuation (including none).
Jens Borchert Pedersen wrote:
> Ed Morton <morton@lsupcaemnt.com> wrote in
> news:c6lonv$c6a@netnews.proxy.lucent.com:
>
> Thanks for your reply
>
>
>
>>Jens Borchert Pedersen wrote:
>>
>>>I have a CNC file which i want to comment using AWK
>>>the single number "3" on a line means "Ignore on" the single number
>>>"5" on a line means "Rapid traverse on"
>>
>>I've read through this a couple of times and I'm still not sure exactly
>>what you want. What I THINK you want (looking for lines that just
>>contain a "5", etc.) seems trivial and the equivalent is already handled
>>elsewhere in your sample script so there must be some subtlety I'm
>>missing. Part of the problem is there's just so much to read here -
>>can't you post a smaller example that illustrates the problem you're
>>trying to solve without all this context of CNC files, cutting plates,
>>etc.?
>>
>> Ed.
>>
>>
>
>
> In the AWK script i have theese two lines:
> /^a.*$/ {print $0 " This is a test line."}
> /^4$/ {Print $0 " Ignore off"}
> The first one works fine finding lines starting with "a"
> The second one does NOT work...
> I want it to look for lines just having the number "4"
>
> To my opinion it should work.
> The only difference I see, is that the regular expression works fine on
> strings but fails on the numbers.
>
> Hope you can help me
>
>
|
|
0
|
|
|
|
Reply
|
Robert
|
4/27/2004 2:48:55 PM
|
|
Jens Borchert Pedersen wrote:
<snip>
> In the AWK script i have theese two lines:
> /^a.*$/ {print $0 " This is a test line."}
The above RE can be simplified to just /^a/
> /^4$/ {Print $0 " Ignore off"}
> The first one works fine finding lines starting with "a"
> The second one does NOT work...
In what way? Does it not find any lines or does it find too many lines?
> I want it to look for lines just having the number "4"
>
> To my opinion it should work.
> The only difference I see, is that the regular expression works fine on
> strings but fails on the numbers.
That's not it. I suspect there's white-space after the printable
characters in your input file, in which case you'll be seeing the "a"
lines print but not the "4" lines. The sample input file you posted had
leading white-space too, but I assume that was just you indenting it for
the posting otherwise the above wouldn't work for "a" either.
To test if there's trailing white-space, modify the "4" line above to
/^4/ and see if that prints. If it doesn't print then there's leading
white-space. If either of those is the problem, then using this instead
will work:
(NF == 1) && ($1 == "4") {Print $0 " Ignore off"}
> Hope you can help me
Me too ;-).
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
4/27/2004 2:55:31 PM
|
|
In article <Xns94D8A48C94AC7jpossdk@194.255.237.193>,
Jens Borchert Pedersen <jp@oss.junkdk> wrote:
>Ed Morton <morton@lsupcaemnt.com> wrote in
>news:c6lonv$c6a@netnews.proxy.lucent.com:
>
>Thanks for your reply
>
>
>> Jens Borchert Pedersen wrote:
>>> I have a CNC file which i want to comment using AWK
>>> the single number "3" on a line means "Ignore on" the single number
>>> "5" on a line means "Rapid traverse on"
>>
>> I've read through this a couple of times and I'm still not sure exactly
>> what you want. What I THINK you want (looking for lines that just
>> contain a "5", etc.) seems trivial and the equivalent is already handled
>> elsewhere in your sample script so there must be some subtlety I'm
>> missing. Part of the problem is there's just so much to read here -
>> can't you post a smaller example that illustrates the problem you're
>> trying to solve without all this context of CNC files, cutting plates,
>> etc.?
>>
>> Ed.
>>
>>
>
>In the AWK script i have theese two lines:
>/^a.*$/ {print $0 " This is a test line."}
>/^4$/ {Print $0 " Ignore off"}
print should not be capitalized
Chuck Demas
>The first one works fine finding lines starting with "a"
>The second one does NOT work...
>I want it to look for lines just having the number "4"
>
>To my opinion it should work.
>The only difference I see, is that the regular expression works fine on
>strings but fails on the numbers.
>
>Hope you can help me
>
>
>--
>Kind regards Jens Borchert Pedersen
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
|
|
0
|
|
|
|
Reply
|
demas
|
4/27/2004 4:10:25 PM
|
|
Charles Demas wrote:
> In article <Xns94D8A48C94AC7jpossdk@194.255.237.193>,
> Jens Borchert Pedersen <jp@oss.junkdk> wrote:
>
>>Ed Morton <morton@lsupcaemnt.com> wrote in
<snip>
>>>Jens Borchert Pedersen wrote:
>>>
>>>>I have a CNC file which i want to comment using AWK
>>>>the single number "3" on a line means "Ignore on" the single number
>>>>"5" on a line means "Rapid traverse on"
>>>
>>>I've read through this a couple of times and I'm still not sure exactly
>>>what you want. What I THINK you want (looking for lines that just
<snip>
>>In the AWK script i have theese two lines:
>>/^a.*$/ {print $0 " This is a test line."}
>>/^4$/ {Print $0 " Ignore off"}
>
>
> print should not be capitalized
At least tell me you spent a while looking at it before you spotted
that! Note to self - look at the characters you CAN see before looking
for ones you can't.....
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
4/27/2004 6:21:13 PM
|
|
In article <c6m8ap$k07@netnews.proxy.lucent.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>Charles Demas wrote:
>> In article <Xns94D8A48C94AC7jpossdk@194.255.237.193>,
>> Jens Borchert Pedersen <jp@oss.junkdk> wrote:
>>
><snip>
>>>In the AWK script i have theese two lines:
>>>/^a.*$/ {print $0 " This is a test line."}
>>>/^4$/ {Print $0 " Ignore off"}
>>
>>
>> print should not be capitalized
>
>At least tell me you spent a while looking at it before you spotted
>that! Note to self - look at the characters you CAN see before looking
>for ones you can't.....
When the OP said that it didn't print the /^4/ stuff, I looked
at that statement. From there it wasn't difficult.
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
|
|
0
|
|
|
|
Reply
|
demas
|
4/27/2004 8:11:53 PM
|
|
demas@TheWorld.com (Charles Demas) wrote in
news:c6meq9$21a$1@pcls3.std.com:
> In article <c6m8ap$k07@netnews.proxy.lucent.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
>>
>>
>>Charles Demas wrote:
>>> In article <Xns94D8A48C94AC7jpossdk@194.255.237.193>,
>>> Jens Borchert Pedersen <jp@oss.junkdk> wrote:
>>>
>><snip>
>>>>In the AWK script i have theese two lines:
>>>>/^a.*$/ {print $0 " This is a test line."} /^4$/ {Print $0 "
>>>> Ignore off"}
>>>
>>>
>>> print should not be capitalized
YESSS thats it - it works now.
>>
>>At least tell me you spent a while looking at it before you spotted
>>that! Note to self - look at the characters you CAN see before looking
>>for ones you can't.....
>
> When the OP said that it didn't print the /^4/ stuff, I looked
> at that statement. From there it wasn't difficult.
I should have seen that myself.
It is my first awk project, and I have used 2 working days fighting this
"Print/print" problem.
I hope my boss does not read this newsgroup. ;)
Thanks a lot for your help.
>
>
> Chuck Demas
>
--
Kind regards
Jens Borchert Pedersen
Odense Steel Shipyard Ltd.
|
|
0
|
|
|
|
Reply
|
Jens
|
4/28/2004 6:52:59 AM
|
|
Ed Morton <morton@lsupcaemnt.com> wrote in
news:c6ls94$e2c@netnews.proxy.lucent.com:
<snip>
>
> To test if there's trailing white-space, modify the "4" line above to
> /^4/ and see if that prints. If it doesn't print then there's leading
> white-space. If either of those is the problem, then using this instead
> will work:
>
> (NF == 1) && ($1 == "4") {Print $0 " Ignore off"}
Thanks for your input
I will save this for later use, I guess it will come in handy later on in
my little project.
>
>> Hope you can help me
>
> Me too ;-).
>
> Ed.
>
--
Kind regards
Jens Borchert Pedersen
Odense Steel Shipyard Ltd.
|
|
0
|
|
|
|
Reply
|
Jens
|
4/28/2004 7:00:31 AM
|
|
|
9 Replies
116 Views
(page loaded in 0.131 seconds)
|
|
|
|
|
|
|
|
|