Smarty OR statement - prestashop

This is working:
{if $filter.id_key='68'}
This doesn't:
{if ($filter.id_key='68' || $filter.id_key='63')}
Not sure what's wrong here (tried also without the round brackets). There are plenty of examples of the same OR statement in the same file.

In {if $filter.id_key='68'} you are assigning 68 to $filter.id_key, so it's always true. You should use double equal ==:
{if ($filter.id_key == '68' || $filter.id_key == '63')}

Related

awk compare adjacent lines and print based on if statements

I have one file with multiple lines (reads from a genome) and they are sorted (based on their locations). Now I want to loop over these lines and if multiple lines have the same ID (column 4), I want to keep either keep the first, if column 3 is a plus or the last, if column three is a minus. This is m code but it seems like my variable (lastID) is not properly updated after each line.
Tips are much appreciated.
awk 'BEGIN {lastline=""; lastID=""}
{if ($lastline != "" && $4 != $lastID)
{print $lastline; lastline=""};
if ($3 == "+" && $4 != $lastID)
{print $0; lastline=""}
else if ($3 == "+" && $4 == $lastID)
{lastli=""}
else if ($3 == "-")
{lastline=$0};
lastID=$4
}' file
To access the value of a variable in awk you just use the name of the variable, just like in C and most other Algol-based languages. You don't stick a $ in front of it like you would with shell. Try changing:
$lastline != "" && $4 != $lastID
to:
lastline != "" && $4 != lastID
etc.
This might be what you're trying to do (your BEGIN section was doing nothing useful so I just removed it):
awk '
(lastline != "") && ($4 != lastID) {
print lastline
lastline=""
}
$3 == "+" {
if ($4 == lastID) {
lastli=""
}
else {
print $0
lastline=""
}
}
$3 == "-" {
lastline=$0
}
{ lastID=$4 }
' file
When formatted sensibly like that you can see that lastli is never used anywhere except where it's set to "" so that's probably a bug - maybe it's supposed to lastline in which case it can be made common rather than being set in both the if and else legs?
you may want to utilize awk's own condition{statement} structure. Note that code layout is not universally accepted but I find it easier to read for short statements.
$ awk '$lastline!="" && $4 != $lastID {print lastline; lastline=""}
$3=="+" && $4 != $lastID {print; lastline=""}
$3=="+" && $4 == $lastID {lastli=""}
$3=="-" {lastline=$0}
{lastID=$4}' file

Using an if statment within awk to check if one variable meets a format if so follow this path

I want to be able to check the data held in one variable if the data inside is "B" then use this regex if it contains something else use a different regex
awk '{if ($1 == "B")
($2 ~ /^".+"$/) && (length($2) <= 10) {print "45th field invalid-HEADER-FILE";}
else
($2 ~ /^".+"|""$/) && (length($2) <= 10) {print "45th field invalid-HEADER-FILE";}
'
Sample input
$1 == "B"
$2 == "random string"
Expected output
there should be no output as the regex passed
alt sample input
$1 == "B"
$2 == "null/empty
Expected output
there should be 45th field invalid-HEADER-FILE displayed on screen
Update:
The conditions can combine:
($45 ~ /^".+"$/) && (length($45) <= 2502) to ($45~/^".{1,2500}"$/).
($45 ~ /^".+"|""$/) && (length($45) <= 2502) to ($45~/^".{0,2500}"$/).
Also, if there's no quote inside the quotes (and should be like that), more exactly:
($45~/^"[^"]{1,2500}"$/) and ($45~/^"[^"]{0,2500}"$/).
So you can do the checking like this:
awk '
$44 == "B" && ($45~/^"[^"]{1,2500}"$/) {print "45th field invalid-HEADER-FILE";} # <-- You can add next inside, after the semicolon, if there are no other codes need to execute.
$44 != "B" && ($45~/^"[^"]{0,2500}"$/) {print "45th field invalid-HEADER-FILE";}
'
Since it's simply equal or not, so just AND the different conditions of $44 == "B" and $44 != "B" to other conditions will serve your need.
Or, put them all inside the main block, and quote them correctly, like this:
awk '
{
if ($44 == "B") {
if ($45~/^"[^"]{1,2500}"$/) {
print "45th field invalid-HEADER-FILE";
}
} else {
if ($45~/^"[^"]{0,2500}"$/) {
print "45th field invalid-HEADER-FILE";
}
}
}'
When properly quoted and indented, you can see the structure clearly.
BTW, you can change length($45) <= 2502 to length($45) < 2503 for conciseness, since length returns an integer.
I really wish you'd post some sample data, rather not 45 fields wide and with 2502 chars in any of them. Post sample with 2 fields and reduce the width to something reasonable, like 3:
$ cat file
A ""
A "123"
A "1234"
B ""
B "123"
B "1234"
Script:
$ awk '$1=="B" && $2~/^".{,3}"$/{print $0}' file
And its output (these should be your fail message but for demonstrational purposes):
B ""
B "123"
That would translate roughly to:
$ awk '$44=="B" && $45~/^".{,2500}"$/{print "45th field invalid-HEADER-FILE"}' file
Is this what you wanted?

Why both condition are checked in logical AND operation in csh if first condition return false

#!/bin/csh
set i=0
if ($i == 1 && { -e $HOME_EXIST } )then
echo "Hi"
else
echo "Hello"
endif
Why both condition are checked in logical AND operation in csh if first condition return false ?
I am getting following error :
HOME_EXIST: Undefined variable.
use $? to check if the var is defined:
if ($?HOME_EXIST) then
(do whatever you want)
endif
Your problem is that even though && is lazy, csh will try to substitute $HOME_EXIST before it starts to evaluate the expression: Reference.
You could get around this problem by using nested ifs.
#!/bin/csh
set i=0
if ($i == 1)then
if(-e $HOME_EXIST)then
echo "Hi"
endif
else
echo "Hello"
endif

AWK - if field in $6 contains "StringXY" then skip whole line and proceed

if ($6 = "sum") {next};
Ok, that does not work. I need a way to skip a line if any field (here $6) contains the string "sum".
Important is here skip and not exit so awk continues parsing the following lines.
if ($6 == "Summe") {next};
It should be == rather than =
Or without the if depending:
awk '$6=="Summe"{next} ... '

How to remove space and the specific character in string - awk

Below is a input.
!{ID=34, ID2=35}
>
!{ID=99, ID2=23}
>
!{ID=18, ID2=87}
<
I am trying to make a final result like as following. That is, wanted to remove space,'{' and '}' character and check if the next line is '>' or '<'.
In fact, the input above is repeated. I also need to parse '>' and '<' character so I will put the parsed string(YES or NO) into database.
ID=34,ID=35#YES#NO
ID=99,ID=23#YES#NO
ID=18,ID=87#NO#YES
So, with 'sub' function I thought I can replace the space with blank but the result shows:
1#YES#NO
Can you let me know what is wrong?
If possible, teach me how to remove '{' and '}' as well.
Appreciated if you could show me the awk file version instead of one-liner.
BEGIN {
VALUES = ""
L_EXIST = "NO"
R_EXIST = "NO"
}
/!/ { VALUES = gsub(" ", "", $0);
getline;
if ($1 == ">") L_EXIST = "YES";
else if ($1 == "<") R_EXIST = "YES";
print VALUES"#"L_EXIST"#"R_EXIST
}
END {
}
Given your sample input:
$ cat file
!{ID=34, ID2=35}
>
!{ID=99, ID2=23}
>
!{ID=18, ID2=87}
<
This script produces the desired output:
BEGIN { FS="[}{=, ]+"; RS="!" }
NR > 1 { printf "ID=%d,ID=%d#%s\n", $3, $5, ($6==">"?"YES#NO":"NO#YES") }
The Field Separator is set to consume the spaces and other characters between the parts of the line that you're interested in. The Record Separator is set to !, so that each pair of lines is treated as a single record.
The first record is empty (the start of the first line, up to the first !), so we only process the ones after that. The output is constructed using printf, with a ternary to determine the last part (I assume that there are only two options, > or <).
Let's say you have this input:
input.txt
!{ID=34, ID2=35}
!{ID=36, ID2=37}
>
You can use the following awk command
awk -F'[!{}, ]' 'NR>1{yn="NO";if($1==">")yn="YES";print l"#"yn}{l=$3","$5}' input.txt
to produce this output:
ID=34,ID2=35#NO
ID=36,ID2=37#YES