executable
executable

Reputation: 3600

How to grep value from a php array

I have simple php array in a php file. Here is the content :

<?php

$arr = array(
    'fookey' => 'foovalue',
    'barkey' => 'barvalue'
);

How can I fetch value foovalue using grep command ?

I have tried :

cat file.php | grep 'fookey=>'

Or

cat file.php | grep 'fookey=>*'

but always return the full line.

Upvotes: 0

Views: 1048

Answers (5)

Jotne
Jotne

Reputation: 41460

This should do:

awk -F "'" '$2~/fookey/ {print $4}' file

or in your case

awk -F "'" '$2~/secret/ {print $4}' file

It searches for all lines where second filed contains fookey/secret and the print fort field with your password.

Upvotes: 1

tripleee
tripleee

Reputation: 189936

grep 'fookey=>' doesn't return any matches because this regex is not matched. Your example shows a record with single quotes around fookey and a space before the =>.

Also, you want to lose the useless use of cat.

Because your regex contains literal single quotes, we instead use double quotes to protect the regex from the shell.

grep "'fookey' =>" file.php

If your goal is to extract the value inside single quotes after the => the simple standard solution is to use sed instead of grep. On a matching line, replace the surrounding text with nothing before printing the line.

sed "/.*'fookey' => '/!d;s///;s/'.*//" file.php

In some more detail,

  • /.*'fookey' => '/!d skips any lines which do not match this regex;
  • s/// replaces the matched regex (which is implied when you pass in an empty regex) with nothing;
  • s/'.*// replaces everything after the remaining single quote with nothing;
  • and then sed prints the resulting line (because that's what it always does)

If you get "event not found" errors, you want to set +H or (in the very unlikely event that you really want to use Csh history expansion) figure out how to escape the !; see also echo "#!" fails -- "event not found"

Other than that, we are lucky that the script doesn't contain any characters which are special within double quotes; generally speaking, single quotes are much safer because they really preserve the text between them verbatim, whereas double quotes in the shell are weaker (you have to separately escape any dollar signs, backquotes, or backslashes).

Upvotes: 1

Zeshan
Zeshan

Reputation: 2657

You can use cut in combination with grep to get what you need.

cat file.php | grep 'fookey' | cut -c18-25

cut is used to get substring. In -cN-M, N and M are starting and ending position of the substring.

Upvotes: 0

deavapriya
deavapriya

Reputation: 77

To fetch a value from an array why can't you use array_search method instead of grep?

<?php
$arr = array(
    'fookey' => 'foovalue',
    'barkey' => 'barvalue'
);
echo array_search("foovalue",$arr);
?>

Upvotes: 0

Mihir Luthra
Mihir Luthra

Reputation: 6779

Your grep command shouldn’t have worked if you are doing it just the way you posted it here. But if you are getting that line from grep whatever way you are doing,

Pass the output you got from grep through a pipe to

awk -F"'" '{print $4}'

I tested it this way on my pc:

echo "'fookey' => 'foovalue'" | awk -F"'" '{print $4}'

Upvotes: 1

Related Questions