csh use of grep | tr commands

Polytropon freebsd at edvax.de
Mon Aug 10 14:49:31 UTC 2020


On Sun, 9 Aug 2020 21:26:56 -0400, Robert Huff wrote:
> Ernie Luzar writes:
> 
> >  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?
> 
> 	Would awk perhaps be a better tool?

Possibly. But it's more elaborate than sed. :-)

	% echo 'ip4="10.111.098.2"' | awk '/^ip4=/ { gsub("ip4=", "", $0); gsub("\"", "", $0); print $0 }'
	10.111.098.2

Compared to:

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

However, awk can eliminate a possible grep invocation to
only process matching lines, which might be an advantage.



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


More information about the freebsd-questions mailing list