regex - sed: How to extract only first occurance of value -
i have file settings.inc.php
following content:
<?php define('_db_server_', 'mariadb'); define('_db_name_', 'organic'); define('_db_user_', 'prestashop'); define('_db_passwd_', 'prestashop');
i want extract these values bash
, managed create following command:
sed -rn 's/^.*_db_name_'\'', '\''(\w+)'\''\);/\1/p' settings.inc.php
this return organic
, should, improve further. let's have kind of file:
<?php define('_db_server_', 'mariadb'); define('_db_name_', 'organic1'); define('_db_name_', 'organic2'); define('_db_user_', 'prestashop'); define('_db_passwd_', 'prestashop');
using above command on file get:
organic1 organic2
the thing is: want command return 1 value, let's first one. can achieve without piping result second command?
if convert substitute command followed print command block operating on lines addressed pattern (containing) _db_name_
, can quit after first match/print:
$ sed -rn "/_db_name_/ { s/.*'(\w+)'\);$/\1/p;q }" settings.inc.php organic1
note q
command after p
.
also, sed
script can simplified using outer double quotes , anchoring on end.
Comments
Post a Comment