[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: vi within a shel script




charles@lunargraphics.net wrote:

> set UserName = $<
> vi ./subscribers
> A $UserName
> :! :wq
>
> how would one take input from a variable and append it to the end of a
> line within a text file? I tried the above, and needless to say, it doesnt
> work. the one hitch is that is must append the text to the end of one
> contiguous line with no carriage returns. any suggestions?

If the line is the last (or only) line in the file, and that line doesn't end
in a LF, it's simple:

echo -n $UserName >> ./subscribers

That will append $UserName to the ever-lengthening last line of
./subscribers.  Note the double-greater-than - miss one, and you'll munch the
entire file's contents.

For more sophisticated stuff, I'd use Perl, such as:

cat ./subscribers | perl -pe '$_ .= $ENV{"UserName"} if /regexp/;' >
./subscribers.new

That will tack $UserName on to every line that matches the regular expression
in regexp.  The '-pe' arguments to the Perl interpreter put it into a
sed-like mode, where it reads in each line, evaluates your Perl code for each
line, and prints the line (with any modifications made by the Perl code).

Does this help?



--
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.