为 awk 编写 shell 包装脚本
我想在 shell 脚本中嵌入 awk 脚本,但我很难做到这一点,因为我不知道在哪里用 ; 结束语句。哪里没有。
这是我正在使用 nawk 的脚本
#!/bin/sh
awk='
BEGIN {FS = ",?+" }
# removes all backspaces preceded by any char except _
function format() {
gsub("[^_]\b", "")
}
function getOptions() {
getline
format()
print
}
{
format()
if ($0 ~ /^SYNOPSIS$/ {
getOptions()
next
}
if ($0 /^[ \t]+--?[A-Za-z0-9]+/) {
print $0
}
}
END { print "\n" }'
path='/usr/share/man/man1'
list=$(ls $path)
for item in $list
do
echo "Command: $item"
zcat $path$item | nroff -man | awk "$awk"
done > opts
顺便说一句,
。提前致谢
I want to embed an awk script inside a shell script but I have trouble to do so as I don't know where to end a statement with a ; and where not.
Here's my script
#!/bin/sh
awk='
BEGIN {FS = ",?+" }
# removes all backspaces preceded by any char except _
function format() {
gsub("[^_]\b", "")
}
function getOptions() {
getline
format()
print
}
{
format()
if ($0 ~ /^SYNOPSIS$/ {
getOptions()
next
}
if ($0 /^[ \t]+--?[A-Za-z0-9]+/) {
print $0
}
}
END { print "\n" }'
path='/usr/share/man/man1'
list=$(ls $path)
for item in $list
do
echo "Command: $item"
zcat $path$item | nroff -man | awk "$awk"
done > opts
I'm using nawk by the way.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,有几个问题:
$awk
的多行字符串。您需要在END { ... }
之后的行上使用单引号$awk
。也许您的意思是在 do 循环内调用 awk。awk
通常对分号相当宽容,但是这方面的任何问题都与在 shell 脚本中使用分号无关。There are several things wrong, as far as I can see:
$awk
. You need a single quote on the line afterEND { ... }
$awk
anywhere. Perhaps you meant on the invocation of awk inside thedo
loop.awk
is usually fairly forgiving about semicolons, but any problems in that regard don't have anything to do with using it inside a shell script.这三行:
需要更改为:
如果文件名中有空格,并且
ls
不打算以这种方式使用。These three lines:
Need to be changed into:
in case there are spaces in filenames and since
ls
is not intended to be used in this way.我不太确定你的意思,但如果我理解正确的话,你的 showOpts.awk 是脚本开头的 awk 代码,所以你可以这样做,
你可能应该使用 >>而不是 > 。
i am not really sure what you meant, but if i understand you correctly, your showOpts.awk is that awk code at the beginning of your script, so you could do this
and you should probably use >> instead of > .