Cut command help

I have file like this ...

$ cat /etc/sysconfig/clock
ZONE="US/Arizona"
UTC=true
ARC=false

I want to get value of ZONE into variable and I am writing like this ..

d_var=`cat /etc/sysconfig/clock | grep ZONE | cut -f2 -d=`

I want to remove " " around the value . How to include it in cut command .

final output should be like this .... US/Arizona ( with out quotes)

Thanks

hi,
try this:

d_var=`cat /etc/sysconfig/clock | grep ZONE | cut -f2 -d= | tr -d \"`

Regards.

1 Like
d_var=`awk -F'[="]+' '$1=="ZONE" {print $2}' /etc/sysconfig/clock`

This allows the value to be quoted or unquoted.

d_var=`grep '^ZONE=' /etc/sysconfig/clock | cut -d'"' -f2`

With both equal sign and double qoutes as field separators, I believe you would want to print $3 instead of $2. Alternatively, you could try:

d_var=$(awk -F'"' '$1=="ZONE="{print $2}' /etc/sysconfig/clock)

or:

d_var=`awk -F'"' '$1=="ZONE="{print $2}' /etc/sysconfig/clock`

Even though awk is usually bigger than grep and cut, on most systems it will be faster to invoke one utility than a pipeline of two utilities.

If you want to try this on a Solaris system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of just awk .

PS You should verify that /etc/sysconfig/clock really sets ZONE rather than TZ.

1 Like

You should use parentheses $() instead of back tics ``

d_var=$(awk -F\" '/^ZONE/ {print $2}' /etc/sysconfig/clock)

As I noted earlier, if your field separator is the double quote, field 1 in the line:

ZONE="US/Arizona"

is:

ZONE=

not:

ZONE