On lör, 2024/09/07 at 10:50:36 -0500, Richard Owlett wrote:
> This started with be exploring "regular expressions".
> I discovered some tutorials that were using Bash in their samples.
> One {lost the reference at the moment} was almost a match for a real
> world problem I have.

Bash has some nifty uses when it comes to variables.

If you just want to store a file in a variable,
VAR="$(</path/to/file)
will do it. If you want to do an array instead, use the 'while read line;
do' construct. As others have pointed out, this is not an optimal usecase
for bash.

Perl on the other hand is well suited for manipulating text (it was the
main reason for its creation) and has a good regexp engine built in. AWK
is also a useful tool for manipulating text, a bit depending on the
structure of it.

> But I've not used Bash in eons and have forgotten how to read a file
> into a single variable or a array variable.

See above.

However, you may be picking the wrong hammer for cracking this particular
nut. As other pointed out, both Python and Perl have modules specifically
targeted at handling SGML/XML/HTML which was something you wanted to
process. Do not re-invent the wheel.

You can still code the harness in Bash if you want, but use a better tool
for processing the input files. No need to go to C/C++ or something like
Go for that. Perl or Python will do just fine.

> I've downloaded "Bash Reference Manual"
>   [https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html] for
> when I need fine grained details.
> I've bookmarked the various links on
>   [https://mywiki.wooledge.org/BashFAQ]
> for quick reference.
> 
> I find neither to be search friendly.

The Bash Manual page is actually quite encompassing and very useful. While
it might not have a lot of examples as such, it does showcase what is
available within Bash itself. Besides that, the O'Reilly book on Bash has
served me well. I have quite a few O'Reilly books and they are all full of
examples and detailed explanations. Even the small quick reference books
they do for things are worthwhile picking up. If you do pick up the Bash
book, might I suggest you add the Sed & Awk book at the same time.

It is very common to code something in bash and either call out to sed or
awk for processing something, depending on the structure of what is
processed. Those three tools often go hand in hand and if you plan on
writing larger projects in Bash, you will want to cover Sed and Awk as
well.

> Suggestions?
> 
> TIA
> 
> 
> 

-- 
Kind regards,

/S

Reply via email to