China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 14:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » My AWK can't print strings View 1,643 Replies 9
Original Poster Posted 2005-12-18 14:11 ·  中国 浙江 金华 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
gawk "BEGIN{print \"The start\"}"

The above code cannot correctly print the string The start

Please help the moderator analyze the reason, is it related to the platform (WIN2000)?


—————————————— willsort Moderation Record ——————————————
Split topic: {18102}How to split a file by markers using batch processing The last two posts are merged here
Operation reason: The two topics have direct contextual connections
Moderator prompt: After the topic is split and merged, the posts will be rearranged according to the posting time, which will affect the reading order
—————————————— willsort Moderation Record ——————————————


[ Last edited by willsort on 2005-12-19 at 22:33 ]
Floor 2 Posted 2005-12-18 15:46 ·  中国 上海 闵行区 电信
中级用户
★★
大师兄
Credits 377
Posts 99
Joined 2005-08-26 07:37
20-year member
UID 41945
Status Offline
gawk "BEGIN {print \"The start\"}"
Floor 3 Posted 2005-12-18 18:06 ·  中国 浙江 金华 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
Thank you very much, tigerpower. Finally, I understand. Does the material say that on the UNIX platform, there is no need for "
Floor 4 Posted 2005-12-19 08:49 ·  中国 浙江 金华 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
After studying for a period of time, fortunately, I have some C language foundation, otherwise I would be at a loss. I studied the predecessor's AWK code.

In fact, intermediate variables do not reduce efficiency because it is a memory operation.

For example, print $0 >> "temp"

Belongs to disk operation, and there is this action for each line, and the efficiency will be lower. But the bamboo shooter's code is more concise, and the system() function is called. However, the REN operation is very fast, almost negligible in terms of time.

In addition, there is something I don't understand. In Windows, double quotes are used instead of single quotes, and double quotes with backslashes are used instead of the original double quotes. Please let the great gods tell me the reason for this.
Floor 5 Posted 2005-12-19 18:05 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re yhsean:

This is related to the different command-line characteristics of the two platforms.

In the Windows command-line environment, if a parameter contains spaces or other characters with special meanings, it must be enclosed in double quotes to be treated as part of a string without being escaped. Single quotes do not have this function.

For command-line tools like gawk, they only obtain the script content from a specific parameter. If there are spaces in this script, it must be enclosed in double quotes; otherwise, the part after the space will be regarded as the next parameter and ignored by gawk, etc.

Once double quotes are used, the double quotes also become special escape symbols. There may also be double quotes in the script of gawk, etc., and then there may be ambiguity in double quote matching: whether adjacent double quotes are matched or the double quotes at both ends are matched? Windows chooses the latter to better support parameter selection, while gawk chooses the former to support the script.

In this way, a command line like gawk "BEGIN{print "The start"}" is obviously problematic. "The Start" is placed outside the double quotes and is no longer a string. To solve this problem, in gawk, use \ to escape the double quote before "The Start" again. At this time, it will no longer match the double quote before BEGIN and will be directly read into the script to match the subsequent double quotes.

That should be it. Everyone, if you have any insights, feel free to put them forward and discuss together.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2005-12-19 19:23 ·  中国 浙江 金华 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
Understand the meaning of the moderator.

But why did we abandon the single quote? Because if we put double quotes inside single quotes, there will be no ambiguous interpretation.

Because the content inside the single quotes can be regarded as the command content of AWK, and has nothing to do with WINDOWS, unless outside the single quotes, that is, no matter what happens inside the single quotes, WIN has no right to interfere.

That is, When seeing AWK, AWK should have a mechanism for WIN to understand its single quote practice.

[ Last edited by yhsean on 2005-12-19 at 19:38 ]
Floor 7 Posted 2005-12-19 20:51 ·  中国 上海 闵行区 电信
中级用户
★★
大师兄
Credits 377
Posts 99
Joined 2005-08-26 07:37
20-year member
UID 41945
Status Offline
Originally posted by yhsean at 2005-12-19 08:49:
...There is a little part that I don't understand, that is, in Windows, double quotes replace single quotes, and double quotes with backslashes replace the original double quotes...


It's because the environment is different
In Windows XP, it's cmd. The first two statements below are wrong, and the last statement is correct:
cd 'C:\Program Files'
'calc'
"calc"

In Unix, it's shell. The above mawk script, in Unix, is
mawk -F"*" '/\*/ {printf t>$2 ".txt";t="";next};{t=t $0 "\n"}' aa.txt
or
mawk -F"*" '
/\*/ {printf t>$2 ".txt"
t=""
next}
{t=t $0 "\n"}
' aa.txt

Some DOS versions can use single quotes, for example, you can use it like this:
echo.|awk '{print "Hello world!"}'
But in other win32 versions, it seems not possible.
Floor 8 Posted 2005-12-19 21:04 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
Finally, I can have some time to surf the Internet.
to yhsean
Because CMD does not recognize the single quote as a special character that is not interpreted in between, it only recognizes double quotes and partially interprets the characters in between. There is no special character in CMD that has the same function as the single quote in UNIX, so it is unavoidable and we can only accommodate like this.
Moreover, CMD interprets the command and then submits it to AWK, and AWK cannot and has no way to restore the original command before CMD submits it.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 9 Posted 2005-12-21 12:06 ·  中国 浙江 金华 永康市 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
Finally realized the charm of AWK, easy to learn and powerful and applicable.

Due to the above problem, I specially searched for a WIN32 version SHELL program and posted it for everyone to share.

In addition, when writing BAT files, in order to complete in one file (no need to separately establish AWK scripts), in this way, in order to write programs with good readability, is there a way to generate an AWK script with one ECHO or other methods?

The content of MY.BAT is as follows:

set file=input.txt
>my.awk echo {
>>my.awk echo BEGIN { }
>>my.awk echo {
...
>>my.awk echo }
>>my.awk echo END { }
>>my.awk echo }
mawk -f my.awk %file%
del my.awk

During the test, it was noticed that if the AWK script is referenced in the file, the double quotes in it do not need to be escaped (for " in my.awk, it does not need to be written as \")

[ Last edited by yhsean on 2005-12-22 at 09:34 ]
Attachments
sh.rar (186.54 KiB, Credits to download 1 pts, Downloads: 14)
Floor 10 Posted 2005-12-22 12:38 ·  中国 浙江 金华 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
Originally posted by willsort at 2005-12-19 18:05:
Re yhsean:

...Is it matching adjacent double quotes or the quotes at both ends? Windows chooses the latter to better support parameter selection, while gawk chooses the former to support scripts.



The problem of quote pairing mentioned by the moderator, CMD is rather ambiguous in some cases,

Suppose the return value of %~f0 is d:\aa bb\cc\my.bat

For example, echo. """%~f0""" (I added a . after ECHO seeing others do it, but I don't know the reason yet, so depressed...)
in order to test in the batch file

It will output "d:\aa bb\cc\my.bat"

Not ""d:\aa bb\cc\my.bat""

Nor d:\aa bb\cc\my.bat (while echo. ”%~f0" will output this string )

[ Last edited by yhsean on 2005-12-22 at 13:14 ]
Forum Jump: