Sed regex - matching text between two double quotes when a pattern is displayed in a string

Using batch PHP from linux shell I am trying to replace the license number in the configuration file below with another. The configuration line may not always display the same, but it may or may not contain spaces, tabs, etc.

Examples:

$config['license_number'] = "jfur2e2ev9uhuvcfu"; $config['license_number'] ="jfur2e2ev9uhuvcfu"; $config['license_number'] =""; $config['license_number']= "jfur2e2ev9uhuvcfu"; $config[ 'license_number' ] = "jfur2e2ev9uhuvcfu"; 

The key is to find any line with "license_number" (or any other text) and replace everything between double quotes with the new configuration parameter.

I would like to use sed so that I can recursively view all files. I have tried a positive lookbehind, but I can not determine the text of a fixed length for the search.

+4
source share
2 answers

This assumes that there is only one occurrence in each row:

 sed '/license_number/s/"\([^"]*\)"/"foo"/' inputfile 

In Perl, you can use \K for positive lookbehind of variable length.

+2
source

Sometimes a solution can be solved without a lot of regular expression. Try awk

 $ awk 'BEGIN{OFS=FS="="}/license_number/{$2="\"foo\""}1' file $config['license_number'] ="foo" $config['license_number'] ="foo" $config['license_number'] ="foo" $config['license_number']="foo" $config[ 'license_number' ] ="foo" 

This means that when "license_number" is detected, change the value in the second field to "foo". The input field separator and output field separator are set to "=".

0
source

Source: https://habr.com/ru/post/1342465/


All Articles