0

I ran this command in shell script-

echo grep datadir /etc/my.cnf

this is the output i got-

datadir=/var/lib/mysql

What should I do to get the output as-

/var/lib/mysql
1
  • The output does not match the command you posted. Either the echo is wrong, or you have omitted a (useless) pair of backticks. Commented Dec 8, 2012 at 10:54

4 Answers 4

2

How about:

[cnicutar@fresh ~]$ echo $str
datadir=/var/lib/mysql
[cnicutar@fresh ~]$ echo ${str#*=}
/var/lib/mysql

Read more about these operations at the ABS.

Sign up to request clarification or add additional context in comments.

Comments

2

Pipe the output of the grep in a cut:

$ echo 'datadir=/var/lib/mysql' | cut -d '=' -f 2
/var/lib/mysql

You might run with problems if there are spaces in your file though (note the indent on the output):

$ echo 'datadir =  /var/lib/mysql' | cut -d '=' -f 2
  /var/lib/mysql

You could use awk to circumvent this:

$ echo 'datadir =  /var/lib/mysql' | awk -F '[[:space:]]*=[[:space:]]*' '{print $2}'
/var/lib/mysql

Comments

1

use cut with the -d option

grep datadir /etc/my.cnf | cut -d = -f 2

Comments

0

Pipe it through sed 's/^datadir=//' which means "replace the leading datadir= with nothing".

1 Comment

Or better yet only use sed -n 's/^datadir=//p' /etc/my.cnf without any grep or echo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.