(Not to mention that I tried naked SED and CAT commands on everything
here with a QSHELL, and they either came back with a "not recognized"
message, or they did nothing at all, not even giving me a message
telling me the syntax).
If you want to play with qsh commands, it might be best to do so initially
from the qshell itself. To get there, type qsh with no parameters, and
press ENTER. Then enter your commands without qsh.
Almost all of the QSHELL stuff is going right over my head. Could
somebody please explain what it does?
sed, a Stream EDitor, is aptly named, so hopefully no more description
needed. What we are doing in this case, is telling the Stream Editor (sed)
to replace a regular expression with a certain value. There are thousands
of sites where you can read about regular expressions; here's one that you
may find helpful:
http://www.regular-expressions.info/tutorial.html
Let's break our expression down by components. Here is the whole string:
sed -n 's/^foo="\(.\{20\}\)"/\1/p$'
-n tells sed not to "print" (or echo) anything until told otherwise.
The first s character at the beginning of the sed command says, "Let's do a
substitution.
The next character (/ for those who don't want to count) is an arbitrary
separator character. We could have used anything (I commonly use % or /).
A substitute command requires three occurrences of the separator character:
One right after the s, one ending the substitution string, and one ending
the replacement string.
Following that, we have a carat (which you don't want in this case). The ^
says "what follows must be at the beginning of the line.
Then we have a part of our string that we want to find and then throw away.
That part is foo=" (including the quote).
The backwacky-open-parens stream tells sed that we want to save what follows
in a buffer that we might use later. The first occurrence of this will be
saved in \1; the second in \2, and so on. You'll see how we use this in a
minute.
The . stands for "any character"
\{20\} says "We must have exactly twenty of the "any character"
Backwacky=close-parens tells sed that this is the end of our first (only)
save value.
Then we have "$ which tells sed that our 20 characters must be followed by a
quote, and by end of line (drop the $ in your case, but you asked for
explanation).
Next we have our separator character (/). So now sed knows that when it
finds a string that matches foo="something" (where 'something' is exactly 20
characters long), it needs to save the 'something' and replace it with ...
what?
Well, following our separator, we have a single metacharacter: \1. Recall
that our saved values are referenced by \1 et cetera. So what we're saying
is, Replace our foo="my20CharValue" with my20CharValue.
Since the \1 is immediately followed by our separator, the replacement data
ends there. Sed begins scanning the file
"So what?" I hear you cry. "We're not printing, so what good is it?" Well,
that p following our ending separator says, "print the value of this
replacement." So sed dutifully echoes:
My20CharValue
Even if the file is multimegabytes in size, the only output will be that one
string (assuming that the string appears only once within the file; if it
occurs more than once, you'll get more than one line like that, and our
little solution will break!!!!
Incidentally, I tried a GREP, and it blew up. Maybe from the fact that
the file (and yes, it's XML) has no line breaks.
AFAIK grep does not blow up due to extremely long line length. But it makes
line anchoring interesting. Since you're dealing with XML and therefore not
interested in anchoring, this should be no issue.
As an Amazon Associate we earn from qualifying purchases.