Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 10 Aug 2020 16:44:53 +0200
From:      Polytropon <freebsd@edvax.de>
To:        Ernie Luzar <luzar722@gmail.com>
Cc:        "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Subject:   Re: csh use of grep | tr  commands
Message-ID:  <20200810164453.378835aa.freebsd@edvax.de>
In-Reply-To: <5F30962B.5060005@gmail.com>
References:  <5F30962B.5060005@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 09 Aug 2020 20:34:51 -0400, Ernie Luzar wrote:
> Double quotes are giving me trouble.
> 
> I have a file with a line in it like this
> ip4="10.111.098.2"
> I want to get just the ip address
> 
> ip=`grep "ip4=" directory-path/file-name
> 
> $ip ends up having ip4="10.111.098.2"  in it
> 
> ip=`echo -n "${ip}" | tr -d "ip4="
> 
> $ip ends up having "10.111.098.2"  in it
> 
> Putting | tr """ " "` after the echo above gives error.
> 
> How do I remove the " around the ip address?

Without any insult: You're using the wrong tool.

While the C shell is acceptable as an interactive shell
(and I even prefer it over bash to a certain degree),
it's absolutely terrible, and I may even say unsuited
for scripting.

The system's default scripting shell is sh. Use that.
See "man sh" for quoting rules. It makes your life
easier and more portable. :-)

You can use bash to interactively develop sh scripts;
even for creating one-liners bash is very convenient.

Back on topic. If you don't mind an additional program
call to sed, you can use the "replace with nothing"
method:

	% echo 'those "are" quotes' | sed 's/"//g'
	those are quotes

This example is from a C shell session. :-)

Applied to your initial problem:

	% echo 'ip4="10.111.098.2"' | sed 's/ip4="//g; s/"//g'
	10.111.098.2

You can use sed for multiple "replace with nothing"
statements.



Allow me to leave a pointer to the following article:

	Tom Christiansen:
	Csh Programming Considered Harmful

	http://harmful.cat-v.org/software/csh

If you want to process some input files and do something
with the lines they contain, use the right tool: This is
often perl, but can also be sh or bash. It cannot be csh.
That's the truth. ;-)



I'd like to remind all readers that I've written one
(in numbers: 1) script in the C shell which still works
and which I still use from time to time. But I cannot
recommend following my example, and I have promised
to never do it again. :-)






-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20200810164453.378835aa.freebsd>