5

I have a command set as an alias like this:

alias badalarm="cat ~/sagLogs/* | grep -I 'failed to generate expected' | awk '{print $4}' | sort | uniq | tee /dev/tty | wc -l" 

It gives me this output:

$ badalarm
alg-t1sg0103
alg-t1sg0104
all-t1sg0006
all-t1sg0009
input)
5

However, if I run the commands from the CLI directly I get:

$ cat ~/sagLogs/* | grep -I 'failed to generate expected' | awk '{print $4}' | sort | uniq | tee /dev/tty | wc -l
alg-t1sg0103
alg-t1sg0104
all-t1sg0006
all-t1sg0009
4

How come the alias version is picking up some other file? When I use
cat ~/sagLogs/* | grep 'failed to generate expected'

I get this output:

[...]
Apr:09:09:31:01:         >>>1 on all-t1sg0009 failed to generate expected 134
Apr:09:09:31:01:         >>>2 on all-t1sg0009 failed to generate expected 107
Apr:09:09:31:01:         >>>2 on all-t1sg0009 failed to generate expected 108
Apr:10:08:00:35:         >>>1 on all-t1sg0009 failed to generate expected 133
Apr:10:08:00:35:         >>>1 on all-t1sg0009 failed to generate expected 107
Binary file (standard input) matches

How can I omit the 'standard input' file from my alias?

2
  • Are you sure you want to omit it? it likely indicates a genuine match in an encoded file - see Why do I get “Binary file matches” with grep -I? Apr 29, 2019 at 14:49
  • @steeldriver I'm not well versed with what sort of scenarios might create this binary file. My intentions are for this directory to contain nothing but my logs (names like xyz-t1sg00123) and see if they recently have the 'failed to generate' message. These logs are generated in a script which is appending the file via an echo command. I dont think that this would result in the creation of an encoded file, so I think it's safe to omit. Let me know what you think. Thanks.
    – KuboMD
    Apr 29, 2019 at 14:54

1 Answer 1

8

When you declare your alias, the $4 in the awk command is within double quotes (since the whole alias string is within double quotes). This means that it will be expanded by the shell, most likely to an empty string. It does not matter that the $4 is within single quotes within the double quotes.

Instead, consider using a shell function,

badalarm () {
    cat "$HOME"/sagLogs/* |
    grep -I 'failed to generate expected' |
    awk '{ print $4 }' | sort -u |
    tee | wc -l
}

This avoids any quoting issues.

I also shorten the pipeline somewhat and replaced outputting directly to the TTY with outputting to standard output instead (so that both the output of tee and wc -l are sent there).

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .