为awk编写shell包装器脚本

hel*_*hod 2 shell awk

我想在shell脚本中嵌入一个awk脚本但是我很难这样做,因为我不知道在哪里结束语句; 哪里没有.

这是我的剧本

#!/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
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我正在使用nawk.

提前致谢

Dal*_*und 5

据我所知,有几件事是错的:

  1. 您不要关闭分配给的多行字符串$awk.您需要在该行之后使用单引号END { ... }
  2. 你似乎没有在任何地方使用 $awk.也许你的意思是在do循环中调用awk .
  3. 一旦解决了这些问题,awk通常对分号是相当宽容的,但是在这方面的任何问题都与在shell脚本中使用它没有任何关系.