1

I have the following script to filter an ascii table if one of the fields equals the user input:

ascii () {
        arg1=$1
        cmd=$(man ascii | awk '/000/,/077/ {print}' | sed '/^$/d')
        if [ #$ -eq 0 ]
                then
                        echo "${cmd}"
                else
                        newcmd=$(echo "${cmd}" | awk '{for(i=1;i<NF;i++) if ($i=="${arg1}") {print $0}}')
                        echo "${newcmd}"
        fi
}

needless to say, it's not working but I'm getting the following error:

bash: [: missing `]'

and the colors shown inside the awk string are all purple in my shell instance so I'm assuming the "${arg1}" isnt being interpreted correctly

(i used a var for the first arg the user inputs because awk also uses dollar sign syntax and I didn't know how else to distinguish functions args vs awk fields)

Mathew
  • 235
  • 4
    #$ is a comment that starts with $. Maybe you want $# – muru Oct 15 '23 at 15:06
  • 1
    And yes, arg1 isn't going to be expanded in single quotes. You should use an awk variable and set it to the value of the shell variable, like in https://unix.stackexchange.com/a/218773/70524 – muru Oct 15 '23 at 15:08

1 Answers1

2

I think this might be what you're trying to do but with neither sample input nor expected output nor an idea of what values might be passed to the function as arguments it's an untested guess:

ascii() {
    man ascii |
    awk -v tgt="$1" '
        /000/ { f=1 }
        f && NF && ((tgt == "") || index(" "$0" "," "tgt" "))
        /077/ { f=0 }
    '
}

See how-do-i-use-shell-variables-in-an-awk-script for more information on using the value of shell variables in awk script.

man ascii on my system outputs "No manual entry for ascii" so I don't know what format that command outputs on your system but if the input to the awk script has spaces that aren't blanks (e.g. tabs) in the sections you care about then do this or similar to convert all spaces to blanks just for the comparison with the target string:

ascii() {
    man ascii |
    awk -v tgt="$1" '
        /000/ { f=1 }
        { rec=$0; gsub(/[[:space:]]+/," ",rec) }
        f && NF && ((tgt == "") || index(" "rec" "," "tgt" "))
        /077/ { f=0 }
    '
}

That could be written a bit more efficiently by only doing tgt-related actions when f and NF are set, e.g.:

ascii() {
    man ascii |
    awk -v tgt="$1" '
        !NF { next }
        /000/ { f=1 }
        f {
            if ( tgt == "" ) {
                print
            }
            else {
                rec = $0
                gsub(/[[:space:]]+/," ",rec)
                if ( index(" "rec" "," "tgt" " ) {
                    print
                }
            }
            if ( /077/ ) {
                f=0
            }
        }
    '
}

but it's not worth worrying about efficiency over conciseness for input the size of a man page.

Ed Morton
  • 31,617
  • Can't say for sure, but OP's man ascii may look something like https://manpages.ubuntu.com/manpages/lunar/en/man7/ascii.7.html – muru Oct 17 '23 at 04:40
  • @muru thanks, but I'm not sure if the spacing in that HTML table would be the same as the spacing output by the command-line command - that may even contain control chars for formatting or something, idk. Looks like the OP has gone away anyway though so it may be a moot point. – Ed Morton Oct 18 '23 at 10:29