I have used all of these, and I think they are safe and reliable. But if you don't understand what any of them do, the PLEASE ASK, BEFORE you use them, on the KM Forum for help, and I'm sure one or more of the Shell Script gurus will jump in and explain.
pbpaste says use the information on the pasteboard
cat -n = "adds line numbers"
egrep '\t456$' = "look for a 'tab' (that's the \t) followed by '456' and then the end of the line (that's the $)
awk '{print $1}' = print the first item on the line, which is the line number that was added by cat -n
My Assumptions
I am assuming that you want to find the line which just has 456. For example, if your input was
"456
456
456456
456"
the egrep command that I suggested would still give you the 2nd line because all of the other lines have more than just 456.
If that is not an accurate assumption, please let me know. If you just wanted to find the first line that has 456 anywhere in it, you would want to use this:
pbpaste | cat -n | fgrep '456' | awk '{print $1}'
I am also assuming that there is only one line that has exactly what you are looking for. However, if your input looked like this:
"456
456
456456
456
456"
Then you are going to get
2
4
as a result because it is found on both lines 2 and 4. If you only want the first line that matches, you would use:
Now let's assume that you might want to have a shell script that will search for any item which is saved as a Keyboard Maestro variable named Something.
In shell scripts, Keyboard Maestro Variables must be prefixed with $KMVAR_ so the variable Something in Keyboard Maestro becomes $KMVAR_Something.
To do the same thing as above with a variable, it would look like this:
Here's where I explain why you have to use \$ instead of just $
This gets a little technical so if it's confusing or boring, feel free to skip this part.
Since we are using a variable we need to use " instead of ' because shell scripts only 'expand' variables when they are in " and not in '.
If the variable Something was equal to 456 then "${KMVAR_Something}" would be replaced by 456 but '${KMVAR_Something}' would not be replaced, so you would end up searching for the literal string ${KMVAR_Something} which is not what you want.
Then there is one last little detail to be aware of. Previously I said that egrep will interpret $ to mean "the end of the line" but if you put $ inside " instead of ' then the shell will think that the $ is trying to refer to some variable. In order to use a literal $ for egrep so it can still understand it as "the end of the line" you need to use \$.
Note that for our purposes ${KMVAR_Something} and $KMVAR_Something are the same.
I tend to suggest using the {brackets} because I think it makes it easier to see where the variable name starts and ends.
Thanks very much!!! Your reply is a textbook of an answer given to beginners. Not only does it explain what each step does, but it also points out all the pitfalls that one might step into when dealing with the script.
It explained why I could not make Chris's script work. I did not change the single quote in '/456/' to double-quote when I replaced 456 with a KM variable: "/${KMVAR_LocalString}/". So I was searching for a literal '/${KMVAR_LocalString}/'. I should have caught that~
Single-vs-double quotes are something that has tripped up nearly everyone who has dealt with shell scripts at some point.
There is a logic to it, but if you aren’t familiar with it, it is easy to use the wrong ones -- especially because in many cases you can get away with using either one and it will not matter, it’s only when dealing with certain characters, especially $ that it becomes crucial to use the right one.