We are trying to create regex for the following string:-
$last_id . ' where ticket_id=' . $this->getId() {
to test our regex we wrote the following bash one line:-
echo " $last_id . ' where ticket_id=' . $this->getId() {"| grep -i -E ".*(:?.*\$*.WHERE.*) " --color=auto;
The result is:-
. ' where ticket_id=' . ->getId() {
expected result:-
. ' where ticket_id=' . $this->getId() {
But the -E parameter excludes $this. Please help we are stuck.
Your $this gets dropped out before reaching grep, because it's being expanded by shell as a variable named this.
See:
$ echo " $last_id . ' where ticket_id=' . $this->getId() {"
. ' where ticket_id=' . ->getId() {
To prevent parameter expansion in shell, just escape the $:
echo " \$last_id . ' where ticket_id=' . \$this->getId() {"
$last_id . ' where ticket_id=' . $this->getId() {
Related
Opencart: v.1.5.6.4
When someone searches for "iphone nikon" in Opencart's search input text field the results are:
"There is no product that matches the search criteria." (example - https://o-v156x.my-soul.net/index.php?route=product/search&search=iphone%20nikon)
What I want to achieve is to change the search query from AND to OR to get results:
iphone
Nikon D300
or all the products where these words appear in product's name.
I am not interested in live search (auto-complete or auto-suggest or auto-correct).
I am pretty sure that the changes I want to make (by using vQmod) are inside the file /catalog/model/catalog/product.php.
Where exactly is the code needed to be changed?
EDIT
catalog/model/catalog/product.php
Locate the function getProducts()
Find the following code block in relevant function:
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
and replace the code block with:
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" OR ", $implode) . "";
}
Find the following code inside the getTotalProducts() function
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
and replace with following
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" OR ", $implode) . "";
}
Hope it helps, CHEERS!
Look for the function called getProducts() in the file /catalog/model/catalog/product.php. Within this look for something similar to the following line which deals with the variable filter_name
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
You will have to change the AND to OR in the above statement, but remember if you make this change in the core any updates to OpenCart will change this back to what the update is.
I have a folder with many files containing text like the following:
blabla
chargeableDuration 00 01 03
...
timeForStartOfCharge 14 55 41
blabla
...
blabla
calledPartyNumber 123456789
blabla
...
blabla
callingPartyNumber 987654321
I require the output like:
987654321 123456789 145541 000103
I have been trying with following awk:
awk -F '[[:blank:]:=,]+' '/findstr chargeableDuration|dateForStartOfCharge|calledPartyNumber|callingPartyNumber/ && $4{
if (calledPartyNumber != "")
print dateForStartOfCharge, "NIL"
dateForStartOfCharge=$5
next
}
/calledPartyNumber/ {
for(i=1; i<=NF; i++)
if ($i ~ /calledPartyNumber/)
break
print chargeableDuration, $i
chargeableDuration=""
}' file
Cannot make it work. Please help.
Assuming you have a file with text named "test.txt", below linux shell command will do the work for you.
egrep -o "[0-9 ]{1,}" test.txt | tr -d ' \t\r\f' | sort -nr | tr "\n" "\t"
Pretty much like Manishs answer:
tac test_regex.txt | grep -oP '(?<=chargeableDuration|timeForStartOfCharge|calledPartyNumber|callingPartyNumber)\s+([^\n]+)' | tr -d " \t\r\f" | tr "\n" " "
Only difference is, you keep the preceding order instead of sorting the result. So for your example both solutions would produce the same output, but you could end up with different results.
awk '/[0-9 ]+$/{
x=substr($0,( index($0," ") + 1 ) );
gsub(" ","",x);
a[$1]=x
}
END {
split("callingPartyNumber calledPartyNumber timeForStartOfCharge chargeableDuration",b," ");
for (i=1;i<=4;i++){
printf a[(b[i])]" "
}
}'
/[0-9 ]+$/ : Find lines end with number separated with/without spaces.
x=substr($0,( index($0," ") + 1 ) ) : Get the index after the first space match in $0 and save the substring after the first space match(ie digits) to a variable x
gsub(" ","",x) : Remove white spaces in x
a[$1]=x : Create an array a with index as $0 and assign x to it
END:
split("callingPartyNumber calledPartyNumber timeForStartOfCharge chargeableDuration",b," ") : Create array b where index 1,2,3 and 4 has value of your required field in the order you need
for (i=1;i<=4;i++){
printf a[(b[i])]" "
} : for loop to get the value in array a with index as value in array b[1],b[2],b[3] and b[4]
I'm trying to create a PHP File in order to display all my products in a XML format.
The PHP file is in the ROOT dir of the PS installation.
Could you please telle how to initialise PS without it adding html and body tags to the pages ?
I also want to get the PublicPrice et the PriceWithoutReduction of the products.
When I try this code I get a Fatal Error.
The URL : http://www.topludo.fr/xml_guide.php
Thanks for advance.
Pierre.
<?php
ini_set('display_errors', 'on');
header("Content-Type:application/xml");
include(dirname(__FILE__) . '/config/config.inc.php');
Context::getContext()->shop->setContext(Shop::CONTEXT_ALL);
echo '<?xml version="1.0" encoding="UTF-8"?>' . chr(10);
echo '<catalogue lang="FR" date="' . date('Y-m-d H:i') . '" GMT="+1" version="2.0">' . chr(10);
$id_lang = 1;
$front = false;
// RequĂȘte identifiant les produits disponibles dans le catalogue
echo $sql = 'SELECT p.*, pl.* , m.`name` AS manufacturer_name FROM `'._DB_PREFIX_.'product` p '.
' LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`)'.
' LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)'.
' WHERE pl.`id_lang` = '.(int)$id_lang.' ORDER BY name ASC';
$rq = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
foreach ($rq as &$row)
$row = Product::getTaxesInformations($row);
$products_query = $rq;
$product_num = 0;
foreach($products_query as $key => $products) {
$product = new Product($products['id_product']);
$product_num++;
$categories = Product::getProductCategoriesFull($product->id, $id_lang);
$category = end($categories);
//$reducPrice = Product::getPriceStatic($products['id_product'], true, null, 0);
echo $product->getPublicPrice();
$fullPrice = ceil($product->getPriceWithoutReduct());
if ($fullPrice < $reducPrice) {
$discount_price = '';
$regular_price = $fullPrice;
$sale = 0;
} else {
$discount_price = $reducPrice;
$regular_price = $fullPrice;
$sale = 2;
}
$cover_id = $product->getCover($product->id); // L'image du produit
$link = new Link();
$cover_image_link = $link->getImageLink('large-default', $cover_id['id_image']);
$sp = SpecificPrice::getByProductId($product->id); // Pour les dates de reduc
$manufacturer = Manufacturer::getNameById($product->id_manufacturer); // Le fabricant
echo '<product place="' . $product_num . '">' . "\n";
echo '<merchant_category><![CDATA[' . $category['name'] . ']]></merchant_category>' . chr(10);
echo '<offer_id><![CDATA[' . $product->id . ']]></offer_id>' . chr(10);
echo '<name><![CDATA[' . $product->name[1] . ']]></name>' . chr(10);
echo '<description><![CDATA[' . substr(strip_tags(str_replace(array('<BR>', '<br>'), "</P>\n<P>", html_entity_decode($products['description']))), 0, 245) . '...]]></description>' . chr(10);
echo '<regular_price currency="EUR">' . $regular_price . '</regular_price>' . chr(10);
echo '<product_url><![CDATA[' . $product->getLink() . ']]></product_url>' . chr(10);
echo '<image_url><![CDATA[' . $cover_image_link . ']]></image_url>' . chr(10);
echo '<discount_price currency="EUR">' . $discount_price . '</discount_price>' . chr(10);
echo '<price_discounted_from><![CDATA[' . substr($sp[0]['from'], 0, 16) . ']]></price_discounted_from>' . chr(10);
echo '<price_discounted_until><![CDATA[' . substr($sp[0]['to'], 0, 16) . ']]></price_discounted_until>' . chr(10);
echo '<sales>' . $sale . '</sales>' . chr(10);
echo '<delivery currency="EUR">FR;0;</delivery>' . chr(10);
echo '<manufacter>'.''.'</manufacter>' . chr(10);
echo '<brand><![CDATA[' . $manufacturer . ']]></brand>' . chr(10);
echo '<model_number><![CDATA[' . $product->reference . ']]></model_number>' . chr(10);
echo '<manufacturer_product_id><![CDATA[]]></manufacturer_product_id>' . chr(10);
echo '<ean13>'.$product->ean13.'</ean13>' . chr(10);
echo '<guarantee unit="year">1</guarantee>' . chr(10);
echo '<used>0</used>' . chr(10);
echo '<used_condition><![CDATA[]]></used_condition>' . chr(10);
echo '<update_date><![CDATA[' . substr($product->date_upd, 0, 16) . ']]></update_date>' . chr(10);
echo '<promo_text><![CDATA[]]></promo_text>' . chr(10);
echo '<offer_valid_from><![CDATA[' . substr($sp[0]['from'], 0, 16) . ']]></offer_valid_from>' . chr(10);
echo '<offer_valid_until><![CDATA[' . substr($sp[0]['to'], 0, 16) . ']]></offer_valid_until>' . chr(10);
echo '<size unit="cm"></size>' . chr(10);
echo '<weight unit="kg">0.00</weight>' . chr(10);
echo '<color><![CDATA[]]></color>' . chr(10);
echo '</product>';
flush();
}
echo '</catalogue>';
To initialize PrestaShop use:
require_once __DIR__.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.inc.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'init.php';
Instead of using header to set your content type to xml and then echo your tags, I recommend using SimpleXML. It's much easier to manipulate your XML and it will result in easier to read code. After you create your SimpleXMLElement and add to it all your product properties as children, you can use asXML method to get the XML string or you can save the XML to a file by passing a file name argument to asXML.
I have a C++ program in which I want to execute the following command:
cmd = "(diff <(perl -ne 's/^\\S+\\s//; if ((/aaa/ .. /bbb/) && /ccc/)"
" { print \"$_\"}' file1)"
"<(perl -ne 's/^\\S+\\s//; if ((/aaa/ .. /bbb/) && /ccc/)"
" { print \"$_\"} file2)) ";
I get this error when I want to execute this command:
Search pattern not terminated at -e line 1.
I've noticed that the following commands work like this:
cmd = "diff <(echo aa) <(echo bb)"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(strCall.c_str(),"r"); // doesn't work popen(**str**.c_str(),"r")
and an example perl command containing '"' works like this:
cmd = "perl -ne '{print \"$1\"}' file"
stream = popen(str.c_str(),"r"); // doesn't work popen(**strCall**.c_str(),"r");
but if the perl command doesn't contains '"', it works both ways:
cmd = "perl -ne '{print $1}' file"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(str.c_str(),"r"); // also works popen(**strCall**.c_str(),"r");
How can I do to use both diff and perl in the same command. I assume I have to use strCall.
I've tried also to escape the perl cmd like this, but it doesn't work:
cmd = "perl -ne '{print \\\"$1\\\"}' file" // one '/' for '/', one for "'" and one for '"'.
Also it didn't worked this, but I am however not allowed to use R("str"):
cmd = R"(perl -ne '{print \"$1\"}' file)"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(strCall.c_str(),"r")
Thanks.
I know I am not answering your question, but a common solution once you reach this many levels of quoting is to write a simple shell script and then call that from popen.
E.g., popen("/path/diffscript.sh", "r");
In my c++ program I want to execute a perl comand and read the output returned by the execution. I use popen for that, but I get an error when executing my command:
Command:
string cmd = "perl -ne 's/^\\S+\\s//; if ((/" +
pattern1+ " START/ .. /" + pattern2+ " END/) && /find/)"
" { print \"$_\"}' file";
stream = popen(cmd.c_str(),"r");
If I execute this command in the command line it works, but in C++ i get this error:
Search pattern not terminated at -e line 1.
The command that works in command line is, in C++ I already escaped the '\' and '"':
perl -ne 's/^\\S+\\s//; if ((/aaa START/ .. /bbb END/) && /find/) { print "$_"}' file
If I execute this command, it works: "perl -ne print $_ file".
But my initial command doesn't.
What I am doing wrong. Thanks.
It's your escape characters \. You'll have to double them up in the C++ string as \\ gets turned into \. Then the shell does it's processing as you see on the command line. i.e. another round of \\ turned into \.
You need to escape your backslashes (by adding more backslashes!).
std::string cmd = "perl -ne 's/^\\\\S+\\\\s//; if ((/" +
pattern1 + " START/ .. /" +
pattern2+ " END/) && /find/)"
" { print \"$_\"}' file";
In C++0x you can use raw R"(strings)" to avoid adding slashes. Compile with GCC like
g++ -std=c++0x -Wall popen.cpp
example:
std::string cmd_raw = R"(perl -ne 's/^\\S+\\s//; if ((/)" +
pattern1 + R"( START/ .. /)" +
pattern2 + R"( END/) && /find/))"
R"( { print \"$_\"}' file)";
This worked:
cmd = "perl -ne 's/^\\\\S+\\\\s//; if ((/" +
pattern1+ " START/ .. /" + pattern2+ " END/) && /find/)"
" { print \"$_\"}' file";
stream = popen(cmd.c_str(),"r");