For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2005 > hello => [Hh].[Ee].[Ll].[Ll].[Oo]









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author hello => [Hh].[Ee].[Ll].[Ll].[Oo]
Jonny

2004-12-29, 3:56 pm

Hi,

Is it possible to change each line of a file as shown in the examples
below:

hello becomes [Hh]-[Ee]-[Ll]-[Ll]-[Oo]
there becomes [Tt]-[Hh]-[Ee]-[Rr]-[Ee]

and so on?

Many Thanks,
Jonny


William James

2004-12-29, 8:55 pm

This crashes on my computer. Could it be a Unix-only solution?

Do it this way:

BEGIN { FS=""; OFS="]-[" }
{ for (i=1; i<=NF; i++)
$i=toupper($i)tolower($i)
print "[" $0 "]" }

Janis Papanagnou

2004-12-30, 3:55 am

William James wrote:
> This crashes on my computer. Could it be a Unix-only solution?


Hmm.. Not as far as I see. Did you see anything in Ed's solution
that is non-standard awk? Or OS dependent? Indications are welcome.
(Or maybe it's a problem of the specific awk/OS set you are using?)

Janis
Ted Davis

2004-12-30, 3:55 am

On Thu, 30 Dec 2004 01:45:51 +0100, Janis Papanagnou
<Janis_Papanagnou@hotmail.com> wrote:

>William James wrote:
>
>Hmm.. Not as far as I see. Did you see anything in Ed's solution
>that is non-standard awk? Or OS dependent? Indications are welcome.
>(Or maybe it's a problem of the specific awk/OS set you are using?)


It's completely Unix ... or at least non-Microsoft. Line continuation
is available in some Windows versions, but it doesn't work the same
way. Inline programs have to be enclosed in "" instead of '', and
internal "s escaped with a backslash (even then, there can be problems
with quotes that were already escaped: too many backslashes tends to
break things). The printf and for syntaxes are broken: () must be
used without a space following the function name. etc., etc., etc.
This varies somewhat with the specific awk port and version. Most
Unix gawk scrips work in Cygwin under Windws.

--
T.E.D. (tdavis@gearbox.maem.umr.edu)
Ed Morton

2004-12-30, 3:55 pm



Ted Davis wrote:
> On Thu, 30 Dec 2004 01:45:51 +0100, Janis Papanagnou
> <Janis_Papanagnou@hotmail.com> wrote:
>
>
>
>
> It's completely Unix ... or at least non-Microsoft. Line continuation
> is available in some Windows versions, but it doesn't work the same
> way. Inline programs have to be enclosed in "" instead of '', and
> internal "s escaped with a backslash


Then that can't be the problem William's encountered since he doesn't
escape his backslashes.

William - which awk/OS are you using and what part of the script is
causing the problem?

(even then, there can be problems
> with quotes that were already escaped: too many backslashes tends to
> break things). The printf and for syntaxes are broken: () must be
> used without a space following the function name. etc., etc., etc.
> This varies somewhat with the specific awk port and version. Most
> Unix gawk scrips work in Cygwin under Windws.
>


Thanks for the information. I don't think you're suggesting we start
writing our scripts to accomodate those MS peculiarities, but it's good
to know they exist.

Ed.
Kenny McCormack

2004-12-30, 3:56 pm

In article <geOdnTUJLaBdn0ncRVn-qg@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>Thanks for the information. I don't think you're suggesting we start
>writing our scripts to accomodate those MS peculiarities, but it's good
>to know they exist.


In the category of "water is wet", we shouldn't be writing
shell/COMMAND.COM/CMD.EXE scripts at all in this ng. We should, of course,
be writing AWK. It is up to the user to make it work on his/her
implementaiton.

OBonTopic: BEGIN {print "hello, world"}

Janis Papanagnou

2004-12-30, 3:56 pm

Ted Davis wrote:
> On Thu, 30 Dec 2004 01:45:51 +0100, Janis Papanagnou
> <Janis_Papanagnou@hotmail.com> wrote:
>
> It's completely Unix ... or at least non-Microsoft.


I won't say that awk syntax has primarily to do (ignoring path and other
interface issues, which are not present in Ed's solution anyway - where
the only deviation seems to be the inline call syntax) with Unix or MS.
But if you want to put it that way; the latter, I'd say, since the awk
syntax seems completely valid to me.

> Line continuation is available in some Windows versions, but it doesn't
> work the same way.


Would standard awk programs work on WinDos if put in a file and started
by "awk -f ..."?

> Inline programs have to be enclosed in "" instead of '', and internal "s


That's a WinDos shells issue, then, isn't it?

William has just omitted the surrounding call syntax in his solution.
I think that people in c.l.a are capable of identifying the awk program
within a non-MS like awk -f '...' call.

> escaped with a backslash (even then, there can be problems with quotes that
> were already escaped: too many backslashes tends to break things). The
> printf and for syntaxes are broken: () must be used without a space
> following the function name.


So WinDos' awk's do not conform to the standard? Too bad.

> etc., etc., etc. This varies somewhat with the specific awk port and
> version. Most Unix gawk scrips work in Cygwin under Windws.


Would you say we should on WinDos, if possible, use awk under Cygwin,
only, to avoid bad surprises?

Janis
Ed Morton

2004-12-30, 3:56 pm



Kenny McCormack wrote:
> In article <geOdnTUJLaBdn0ncRVn-qg@comcast.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
> ...
>
>
>
> In the category of "water is wet", we shouldn't be writing
> shell/COMMAND.COM/CMD.EXE scripts at all in this ng. We should, of course,
> be writing AWK. It is up to the user to make it work on his/her
> implementaiton.
>
> OBonTopic: BEGIN {print "hello, world"}
>


According to Teds posting, what you posted above is UNIX-specific since
you'd need to escape the quotes for it to work on an MS platform. It
also will only work for some versions of awk. So, by your definition,
your posting is off-topic.

To follow your own rules, please:

a) test every program you post on every possible platform
b) test every program you post using every implementation of awk
c) only post programs that work on every possible platform under every
implementation of awk
d) make sure you never give any hint to a poster about how to invoke
your program on any platform, even when they tell you their platform.

Actually, why not stick to whining about topicality since that, by
definition, is guranteed to be topical and you don't run the risk of
providing any useful but arguably OT information.

Ed.
Kenny McCormack

2004-12-30, 3:56 pm

In article <VfadnWMVRJ07h0ncRVn-tw@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>
>According to Teds posting, what you posted above is UNIX-specific since
>you'd need to escape the quotes for it to work on an MS platform. It
>also will only work for some versions of awk. So, by your definition,
>your posting is off-topic.


Are you really this stupid or are you just playing that role on the net?

What I posted above will work on any AWK, including, yes, Sun's stone age
AWK (*) - on any platform. Let me repeat: How you get the AWK code into
your interpreter on your platform is your problem, not mine.

(*) Yes, in stone age AWK, it will then expect input data as if there was
some kind of non-empty set of pattern/action pairs. This problem can be
worked around, in various, implementation-specific ways (which are, of
course, off-topic)

Ed Morton

2004-12-30, 3:56 pm



Kenny McCormack wrote:

> In article <VfadnWMVRJ07h0ncRVn-tw@comcast.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
> ...
>
>
>
> Are you really this stupid or are you just playing that role on the net?


You constantly post articles whining about topicality, while
simultaneously posting articles that by your definition are off-topic,
sometimes in the same article. You then resort to name-calling when your
inconsistency is pointed out. No doubt your next follow up will be along
the lines of "my dad's bigger than your dad". You're just embarassing
yourself, son.

> What I posted above will work on any AWK, including, yes, Sun's stone age
> AWK (*) - on any platform. Let me repeat: How you get the AWK code into
> your interpreter on your platform is your problem, not mine.


Yes, your problem is apparently far more serious and you have my pity.
This is my last posting in this thread on the subject of topicality.

Ed.

> (*) Yes, in stone age AWK, it will then expect input data as if there was
> some kind of non-empty set of pattern/action pairs. This problem can be
> worked around, in various, implementation-specific ways (which are, of
> course, off-topic)
>

Jonny

2004-12-30, 8:55 pm

Ed Morton wrote:

> Something like this:
>
> awk 'BEGIN{FS=""}
> {
> sep=""
> for (i=1;i<=NF;i++) {
> printf "%s[%s%s]",sep,toupper($i),tolower($i)
> sep="-"
> }
> printf "\n"
> }'
>
> should to it.



Thou art THE man.

Ed Morton

2005-01-01, 3:55 pm



Ted Davis wrote:
> On Thu, 30 Dec 2004 01:45:51 +0100, Janis Papanagnou
> <Janis_Papanagnou@hotmail.com> wrote:
>
>
>
>
> It's completely Unix ... or at least non-Microsoft. Line continuation
> is available in some Windows versions, but it doesn't work the same
> way. Inline programs have to be enclosed in "" instead of '', and
> internal "s escaped with a backslash


Then that can't be the problem William's encountered since he doesn't
escape his backslashes.

William - which awk/OS are you using and what part of the script is
causing the problem?

(even then, there can be problems
> with quotes that were already escaped: too many backslashes tends to
> break things). The printf and for syntaxes are broken: () must be
> used without a space following the function name. etc., etc., etc.
> This varies somewhat with the specific awk port and version. Most
> Unix gawk scrips work in Cygwin under Windws.
>


Thanks for the information. I don't think you're suggesting we start
writing our scripts to accomodate those MS peculiarities, but it's good
to know they exist.

Ed.
John Savage

2005-01-02, 3:55 am

Janis Papanagnou <Janis_Papanagnou@hotmail.com> writes:
>Would standard awk programs work on WinDos if put in a file and started
>by "awk -f ..."?


In DOS, certainly.

>Would you say we should on WinDos, if possible, use awk under Cygwin,
>only, to avoid bad surprises?


I've not encountered any issues with the MAWK and GAWK ports that I use
under MSDOS. (I avoid Windoze platforms so cannot comment on them.) For
inline code in batch files you just need to remember that each % sign must
be doubled and each double quote escaped with \. There are no surprises,
no issues any different from standard batch coding. ?AWK is a pleasure to
use with DOS, just as it is with Unix. (Single quotes aren't usually
available in DOS, so were this Unix, the $ would have to be escaped, too.)

DOS has a limited length on its commandline, so only where your code is
of limited length can it be run as a oneliner. The code William posted on
this thread fits neatly after eliminating unneeded spaces, to wit,

mawk "BEGIN{FS=\"\"}{sep=\"\";for(i=1;i<=NF;i++){printf\"%%s[%%s%%s]\",
sep,toupper($i),tolower($i);sep=\"-\"}printf\"\n\"}" data

[Hh]-[Ee]-[Ll]-[Ll]-[Oo]-[,,]-[ ]-[Ww]-[Oo]-[Rr]-[Ll]-[Dd]-[!!]
--
John Savage (my news address is not valid for email)

Kenny McCormack

2005-01-06, 8:55 am

In article <geOdnTUJLaBdn0ncRVn-qg@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>Thanks for the information. I don't think you're suggesting we start
>writing our scripts to accomodate those MS peculiarities, but it's good
>to know they exist.


In the category of "water is wet", we shouldn't be writing
shell/COMMAND.COM/CMD.EXE scripts at all in this ng. We should, of course,
be writing AWK. It is up to the user to make it work on his/her
implementaiton.

OBonTopic: BEGIN {print "hello, world"}

Janis Papanagnou

2005-01-06, 8:55 am

Ted Davis wrote:
> On Thu, 30 Dec 2004 01:45:51 +0100, Janis Papanagnou
> <Janis_Papanagnou@hotmail.com> wrote:
>
> It's completely Unix ... or at least non-Microsoft.


I won't say that awk syntax has primarily to do (ignoring path and other
interface issues, which are not present in Ed's solution anyway - where
the only deviation seems to be the inline call syntax) with Unix or MS.
But if you want to put it that way; the latter, I'd say, since the awk
syntax seems completely valid to me.

> Line continuation is available in some Windows versions, but it doesn't
> work the same way.


Would standard awk programs work on WinDos if put in a file and started
by "awk -f ..."?

> Inline programs have to be enclosed in "" instead of '', and internal "s


That's a WinDos shells issue, then, isn't it?

William has just omitted the surrounding call syntax in his solution.
I think that people in c.l.a are capable of identifying the awk program
within a non-MS like awk -f '...' call.

> escaped with a backslash (even then, there can be problems with quotes that
> were already escaped: too many backslashes tends to break things). The
> printf and for syntaxes are broken: () must be used without a space
> following the function name.


So WinDos' awk's do not conform to the standard? Too bad.

> etc., etc., etc. This varies somewhat with the specific awk port and
> version. Most Unix gawk scrips work in Cygwin under Windws.


Would you say we should on WinDos, if possible, use awk under Cygwin,
only, to avoid bad surprises?

Janis
Kenny McCormack

2005-01-06, 8:55 am

In article <VfadnWMVRJ07h0ncRVn-tw@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>
>According to Teds posting, what you posted above is UNIX-specific since
>you'd need to escape the quotes for it to work on an MS platform. It
>also will only work for some versions of awk. So, by your definition,
>your posting is off-topic.


Are you really this stupid or are you just playing that role on the net?

What I posted above will work on any AWK, including, yes, Sun's stone age
AWK (*) - on any platform. Let me repeat: How you get the AWK code into
your interpreter on your platform is your problem, not mine.

(*) Yes, in stone age AWK, it will then expect input data as if there was
some kind of non-empty set of pattern/action pairs. This problem can be
worked around, in various, implementation-specific ways (which are, of
course, off-topic)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com