Add values between special chars using sed - sed

I want to add respective values between '' for database, username etc.
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => '',
'username' => '',
'password' => '',
'host' => '',
'port' => '',
'driver' => '',
'prefix' => '',
),
),
);
I already tried following, but didn't work.
sed -ie 's/'database' => ''/'database' => 'testdb'/g' file

Just like this (Using double quotes around your sed command:
sed -i "s/'database' => ''/'database' => 'testdb'/g" file
Or a better regex to handle 0 or more spaces:
sed -i "s/'database' *=> *''/'database' => 'testdb'/g" file

sed "/username/ s/''/'blabla'/" inputfile
This will search for the string username if it is matched then replace '' with 'blabla in the inputfile.
You can use sed -i.bak option to reflect changes in the file.

Related

VSCode shortcut to select ALL values inside quotes, and then replace a ton of repeated values?

I basically have to modify a 2,000+ line PHP file.
I have to replace all the 'null' values, with the original paremeter with a $ at the start. Now, by manually doing it line by line will take forever. Is there a shortcut or extension I can use to automate this for me?
This is a small block of code which I have:
'SecuredLoanDetails' => array(
'ExitStrategy' => null,
'ChangeOfUseRequired' => null,
'ProjectRequiresPlanning' => null,
'ProjectPlanningGranted' => null,
'BridgingDetails' => array(
'BridgingPaymentMethod' => 'RolledUp',
'PropertyPreviouslyBridged' => null,
'NumberOfMonthsSincePropertyBridged' => null,
'BridgingLoanPurpose' => 'First_Charge',
'OccupiedByClientOrFamilyMember' => null,
'LimitedCompany' => null,
'BridgingPropertyUse' => null,
'BridgingRefurbishmentType' => null,
'BridgingAdditionalPropertiesTotalValue' => null,
'BridgingAdditionalPropertiesTotalOutstanding' => null,
),
),
This is what I want:
'SecuredLoanDetails' => array(
'ExitStrategy' => $ExitStrategy,
'ChangeOfUseRequired' => $ChangeOfUseRequired,
'ProjectRequiresPlanning' => $ProjectRequiresPlanning,
'ProjectPlanningGranted' => $ProjectPlanningGranted,
'BridgingDetails' => array(
'BridgingPaymentMethod' => 'RolledUp',
'PropertyPreviouslyBridged' => $PropertyPreviouslyBridged,
'NumberOfMonthsSincePropertyBridged' => $NumberOfMonthsSincePropertyBridged,
'BridgingLoanPurpose' => 'First_Charge',
'OccupiedByClientOrFamilyMember' => $OccupiedByClientOrFamilyMember,
'LimitedCompany' => $LimitedCompany,
'BridgingPropertyUse' => $BridgingPropertyUse,
'BridgingRefurbishmentType' => $BridgingRefurbishmentType,
'BridgingAdditionalPropertiesTotalValue' => $BridgingAdditionalPropertiesTotalValue,
'BridgingAdditionalPropertiesTotalOutstanding' => $BridgingAdditionalPropertiesTotalOutstanding,
),
),
You can do this with VSCode's find and replace menu. You can enable regex and then use '(\w*)' => null as the find string and '$1' => $$$1 as the replace string.
This works by matching a quoted word followed by => null and capturing the quoted word into the group $1. Then the replacement string is built as '(captured word)' => $(captured word) where (captured word) is the contents of group $1.
Given your text, you can replace (Ctrl + H) the occurrences of 'identifier' => null with 'identifier' => $identifier using the following regex and replace pattern. Make sure to turn on the use of regular expressions in the Find/Replace box (Alt + R or the button on the right side of the Find text field that looks like .*).
('[^']*')(\s*=>\s*)(null)
$1$2\$$1
This captures group 1 'identifier', group 2 =>, and group 3 null and substitutes group 1, group 2, and your variable $identifier.

TYPO3 9.5: TCA type 'slug' always adds '-1' to URL

I have the following configuration in my extbase TCA configuration:
'path_segment' => array(
'exclude' => 1,
'l10n_mode' => 'mergeIfNotBlank',
'label' => 'Path Segment',
'config' => [
'type' => 'slug',
'generatorOptions' => [
'fields' => ['productname'],
'replacements' => [
'/' => '-',
'.' => '',
'®' => '',
',' => '',
'|' => '',
' ' => '-',
],
],
'fallbackCharacter' => '-',
'eval' => 'unique'
]
),
When I save, I get URLs with -1 added to the URL (f.e.g "myproduct-1" instead of just "myproduct")
I can only avoid this when clicking the Icon which is labled "Recalculate URL Segment from page title" in the List-Module when editing a record.
Our Editors do not mind much about this field and would change the URL every time they save the record.
What can I do to have this "recalculation" automatically" done? Or what am I doing wrong in my configuration?
I'm using TYPO 3 9.5.17.
You should update to 9.5.18. This behaviour is a regression:
2020-05-14 ccd6da5027 [BUGFIX] Exclude current record when checking slug's uniqueness (thanks to Xavier Perseguers)
See Release Notes.

Using sed in terminal to replace text in file

I have this text in this file:
test.php
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'original',
'username' => 'root',
'password' => 'root',
'host' => 'localhost'
),
),
);
In terminal run this line to replace 'original' with 'new'
sed -i 's/original/new/g' test.php
UPDATE: The error message is:
sed: 1: "test.php": undefined label 'est.php'
What is the problem?
UPDATE 2:
If I just run: ( I removed '-i')
sed 's/original/new/g' test.php
I see the file text modified in the terminal. But then the file is not saved.
On BSDish platforms (including Mac OSX), the -i option requires an argument.
sed -i '' 's/original/new/g' test.php
Notice the empty argument ''.

Cakephp Form Labels Encoding Utf8

In my php application since the beginning that i set everything with utf8 to avoid future problems. I set my database:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'aquitex',
'prefix' => '',
'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'aquitex',
'prefix' => '',
'encoding' => 'utf8',
);
}
The file core.php:
Configure::write('App.encoding', 'UTF-8');
And the default layout of the views:
<?php echo $this->Html->charset(); ?>
However, i'm still having problems in some elements like labels of forms.
In my index.ctp file, this line:
echo $this->Html->link("Segurança", array('controller' => 'Posts','action'=> 'add'), array( 'class' => 'button'));
works perfectly and there's no problem with the 'ç' character.
But in forms, like this:
echo $this->Form->create('Post');
echo $this->Form->input('Nome Produto');
echo $this->Form->input(utf8_encode("Código Produto"));
echo $this->Form->input("Versão");
echo $this->Form->input('Data');
//echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Criar Ficha');
there's no way i can get the words on the labels of the form with 'ó" or 'ç' characters showing properly. As you can see i even tried the utf8encode() in one of them.
Any hints? Thank you!
there is no need to use utf8_encode() in your views.
you simply forgot to save the view file properly.
save it as "utf8 without bom" and you will be fine.
files that do not contain any special utf8 char can still stay as ansi (since there is no difference between them then).
but every file that does contain such a character you need to save as utf8 (even controllers and models if you plan on using utf8 characters there for error messages etc).
PS: in general it is wiser to use english and to translate it via PO file into your language.
this way you can leave the files as they are and you are more flexible (you can add new languages on the fly just by creating a new PO file then).
EDIT
After figuring out together that your inputs() use utf8 chars, I will need to update:
It is wise to use "underscore_field_names" for your db fields (and therefore your input fields) - and in English:
echo $this->Form->input("version"));
you can easily translate them via PO file afterwards or specifying the label:
echo $this->Form->input("version", array('label' => 'Versão'));
but the first way is recommended to keep it dry.
App.encoding just tells Cake to send data in UTF8. If you're using MySQL, make sure the database itself is set to utf8_general_ci collation.

POST web authentication of Nest Thermostat site in Perl (attempting to convert from Ruby)

there is a bit of code I'm trying to replicate in Perl using either LWP::UserAgent or WWW::Mechanize from an existing script.
The original script actually does more than I'm looking to do. I'd just like to log into the Nest website (the part I need help with) and then parse out some data for historical logging (I'm good there).
My current script I would expect to work, but I'm not sure if the authResult/access_token from the Ruby example us actually understood/used by either Perl module.
My code in Perl:
#!/usr/bin/perl
use WWW::Mechanize;
#use HTTP::Request::Common qw(POST);
use HTTP::Cookies;
use LWP::UserAgent;
use Data::Dumper;
use CGI;
my $email; #stores our mail
my $password; #stores our password
my $user_agent = 'Nest/1.1.0.10 CFNetwork/548.0.4';
$email = "email#email";
$password = "mypassword";
my #headers = (
'User-Agent' => 'Nest/1.1.0.10 CFNetwork/548.0.4',
'X-nl-user-id' => $email,
'X-nl-protocol-version' => '1',
'Accept-Language' => 'en-us',
'Connection' => 'keep-alive',
'Accept' => '*/*'
);
# print "Content-type: text/html\n\n";
my $cookie = HTTP::Cookies->new(file => 'cookie',autosave => 1,);
my $browser = WWW::Mechanize->new(cookie_jar => $cookie, autocheck => 1,);
# tell it to get the main page
$browser->get("https://home.nest.com/user/login");
print Dumper($browser->forms);
# okay, fill in the box with the name of the
# module we want to look up
$browser->form_number(1);
$browser->field("username", $email);
$browser->field("password", $password);
$browser->submit();
print $browser->content();
When I submit the form, I just get the same page returned back to me, and I don't know what exactly is causing Nest to not like what I'm submitting. There are two additional fields in the form on their log-in page:
'inputs' => [
bless( {
'maxlength' => '75',
'/' => '/',
'value_name' => 'E-mail address',
'name' => 'username',
'id' => 'id_username',
'type' => 'text'
}, 'HTML::Form::TextInput' ),
bless( {
'/' => '/',
'value_name' => 'Password',
'name' => 'password',
'id' => 'id_password',
'type' => 'password',
'minlength' => '6'
}, 'HTML::Form::TextInput' ),
bless( {
'readonly' => 1,
'/' => '/',
'value_name' => '',
'value' => '',
'name' => 'next',
'type' => 'hidden'
}, 'HTML::Form::TextInput' ),
bless( {
'readonly' => 1,
'/' => '/',
'value_name' => '',
'value' => 'dbbadca7910c5290a13d30785ac7fb79',
'name' => 'csrfmiddlewaretoken',
'type' => 'hidden'
}, 'HTML::Form::TextInput' )
Do I need to use the csrfmiddlewaretoken value in each submission? It appears to change. I thought getting a cookie upon a successful login would be enough.
Any suggestions on what I'm doing wrong?
Shot in the blue:
perl -E'use warnings; $email = "email#email"; say "<$email>"'
Possible unintended interpolation of #email in string at -e line 1.
<email>
I suspect it fails because the form gets the wrong user name, print it out to confirm. Always enable the pragmas strict and warnings to make many common mistakes visible.