![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-23 15:27 |
47,812 topics / 349,917 posts / today 0 new / 48,273 members |
| DOS批处理 & 脚本技术(批处理室) » My AWK can't print strings |
| Printable Version 1,682 / 9 |
| Floor1 yhsean | Posted 2005-12-18 14:11 |
| 初级用户 Posts 26 Credits 90 | |
|
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 ] |
|
| Floor2 tigerpower | Posted 2005-12-18 15:46 |
| 中级用户 Posts 99 Credits 377 | |
|
gawk "BEGIN {print \"The start\"}"
|
|
| Floor3 yhsean | Posted 2005-12-18 18:06 |
| 初级用户 Posts 26 Credits 90 | |
|
Thank you very much, tigerpower. Finally, I understand. Does the material say that on the UNIX platform, there is no need for "
|
|
| Floor4 yhsean | Posted 2005-12-19 08:49 |
| 初级用户 Posts 26 Credits 90 | |
|
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. |
|
| Floor5 willsort | Posted 2005-12-19 18:05 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor6 yhsean | Posted 2005-12-19 19:23 |
| 初级用户 Posts 26 Credits 90 | |
|
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 ] |
|
| Floor7 tigerpower | Posted 2005-12-19 20:51 |
| 中级用户 Posts 99 Credits 377 | |
Originally posted by yhsean at 2005-12-19 08:49: 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. |
|
| Floor8 无奈何 | Posted 2005-12-19 21:04 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
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. |
|
| Floor9 yhsean | Posted 2005-12-21 12:06 |
| 初级用户 Posts 26 Credits 90 | |
|
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) |
|
| Floor10 yhsean | Posted 2005-12-22 12:38 |
| 初级用户 Posts 26 Credits 90 | |
Originally posted by willsort at 2005-12-19 18:05: 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 ] |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |