Clean format smarty html coda 2? - smarty

I use Coda 2 , do you know a way to format clean smarty code ? Something to reorder all tag ? Or maybe on other good editor on Mac ?
From that :
{if $order.state_name == "Pending"}
{$order.state_name|escape:'html':'UTF-8'}
{elseif $order.state_name == "Cancel"}
{$order.state_name|escape:'html':'UTF-8'}
{else}
{if $demandes}
{if $demandes != $benefits}
{l s='Payé' mod='jmarketplace'}
{else}
{l s='Payé' mod='jmarketplace'}
{/if}
{else}
{$order.state_name|escape:'html':'UTF-8'}
{/if}
{/if}
to that :
{if $order.state_name == "Pending"}
{$order.state_name|escape:'html':'UTF-8'}
{elseif $order.state_name == "Cancel"}
{$order.state_name|escape:'html':'UTF-8'}
{else}
{if $demandes}
{if $demandes != $benefits}
{l s='Payé' mod='jmarketplace'}
{else}
{l s='Payé' mod='jmarketplace'}
{/if}
{else}
{$order.state_name|escape:'html':'UTF-8'}
{/if}
{/if}

Related

ERROR! Syntax Error while loading YAML. AWX/Ansible

I am using AWX and trying to run this task:
- name: cleanup
shell: awk '{ a[$1 OFS] = a[$1 OFS] ( a[$1 OFS] == "" ? "" : OFS) $2 }END{ for (i in a){print i,a[i]} } OFS="\t" latest.txt "{{ inventory_hostname }}".txt > ./output/"{{ inventory_hostname }}".txt
But I am getting this Error:
> ERROR! Syntax Error while loading YAML. did not find expected key
> The error appears to be in
> '/tmp/awx_8887_nv3efn13/project/Simsek/test/test.yml': line 141,
> column 60, but may be elsewhere in the file depending on the exact
> syntax problem. The offending line appears to be:
> - name: cleanup
> shell: "awk '{ a[$1 OFS] = a[$1 OFS] ( a[$1 OFS] == "" ? "" : OFS) $2 }END{ for (i in a){print i,a[i]} } OFS="\t" latest.txt "{{
> inventory_hostname }}".txt > ./output/"{{ inventory_hostname }}".txt"
> ^ here We could be wrong, but this one looks like it might be an issue with
> missing quotes. Always quote template expression brackets when they
> start a value. For instance:
> with_items:
> - {{ foo }} Should be written as:
> with_items:
> - "{{ foo }}"
try this:
awk '{ a[$1 OFS] = a[$1 OFS] ( a[$1 OFS] == "" ? "" : OFS) $2 }END{ for (i in a){print i,a[i]} } OFS="\t"' latest.txt {{ inventory_hostname }}.txt > ./output/{{ inventory_hostname }}.txt

Convert tabular file to an HTML table

I need to print the following i/p in table format for mail using html tags in unix shell scripting.
i/p file : list.txt
a 2019 a 2020
b 2020 b 2001
c 2019 c 2013
i tried, but i got all the values in one column:
echo "<html>
<head>
<title>Table Load Status</title>
<style>
table, th, td {
border: 1px solid blue;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body>
<table style='width:100%'>
<tr bgcolor='#808080'>
<th>HEading 1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>"
for contents in $(cat list.txt)
do
echo "<tr>
<td>$contents</td>
<td>$contents</td>
<td>$contents</td>
</tr>"
done
current O/p:
Heading1 heading2 heading3 heading4
a
2019
a
2020
b
....
desired O/p:
Heading1 heading2 heading3 heading4
a 2019 a 2020
b 2020 b 2001
c 2019 c 2013
Please help. thanks in advance
EDIT: Adding 1 more solution which adds headings too in HTML output file, considering that very first line is heading if this is the case then try following.
awk -v s1="\"" '
BEGIN{
print "<html>" ORS "<title>My title goes here...</title>" ORS "<head>"\
ORS "<style>" ORS "table, th, td {" ORS " border: 1px solid black;" ORS\
"}" ORS "</style>" ORS "</head>" ORS "<body>" ORS "<table cellpadding=" s1 "10" s1 ">"
}
FNR==1{
print "<tr>"
for(i=1;i<=NF;i++){
print "<th>" $i "</th>"
}
print "</tr>"
}
{
print "<tr>"
for(i=1;i<=NF;i++){
print "<td>"$i"</td>"
}
print "</tr>"
}
END{
print "</table>" ORS "</body>" ORS "</html>"
}' Input_file
Could you please try following.
awk -v s1="\"" '
BEGIN{
print "<html>" ORS "<title>My title goes here...</title>" ORS "<head>"\
ORS "<style>" ORS "table, th, td {" ORS " border: 1px solid black;" ORS\
"}" ORS "</style>" ORS "</head>" ORS "<body>" ORS "<table cellpadding=" s1 "10" s1 ">"
}
{
print "<tr>"
for(i=1;i<=NF;i++){
print "<td>"$i"</td>"
}
print "</tr>"
}
END{
print "</table>" ORS "</body>" ORS "</html>"
}' Input_file
Above will generate following html file.
<html>
<title>My title goes here...</title>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table cellpadding="10">
<tr>
<td>a</td>
<td>2019</td>
<td>a</td>
<td>2020</td>
</tr>
<tr>
<td>b</td>
<td>2020</td>
<td>b</td>
<td>2001</td>
</tr>
<tr>
<td>c</td>
<td>2019</td>
<td>c</td>
<td>2013</td>
</tr>
</table>
</body>
</html>
When we open html file with browser then it will look like:

AWK syntax error while using the IF statement

I am very new to AWK although I have previously used the command prompt/terminal.
I have this script below where I am creating subsets of data based on Country Code and State Code. But I get a syntax error.
BEGIN{
FS = "\t"
OFS = "\t"
}
# Subset data from the states you need for all years
if ($5 == "IN-GA" || $5 == "IN-DD" || $5 == "IN-DN" || $5 == "IN-KA" || $5 == "IN-KL" || $5 == "IN-MH" || $5 == "IN-TN" || $5 == "IN-GJ"){
if (substr($17, 1, 4) == "2000"){
print $5, $12, $13, $14, $15, $16, $17, $22, $23, $24, $25, $26, $28 > "Y2000_India_sampling_output.txt"
}
}
On Cygwin, I refer to the script and I run the below lines of code and you see the syntax error immediately:
$ gawk -f sampling_India.awk sampling_relFeb-2017.txt
gawk: sampling_India.awk:20: gawk if ($5 == "IN-GA" || $5 == "IN-DD" || $5 == "IN-DN" || $5 == "IN-KA" || $5 == "IN-KL" || $5 == "IN-MH" || $5 == "IN-TN" || $5 == "IN-GJ"){
gawk: sampling_India.awk:20: ^ syntax error
Any thoughts?
Your if condition is not enclosed in {...} block.
Have it like this:
BEGIN {
FS = OFS = "\t"
}
# Subset data from the states you need for all years
$5 ~ /^IN-(GA|DD|DN|KA|KL|MH|TN|GJ)$/ && substr($17, 1, 4) == "2000" {
print $5, $12, $13, $14, $15, $16, $17, $22, $23, $24, $25, $26, $28 > "Y2000_India_sampling_output.txt"
}
Note how using regex you can combine multiple == conditions into a single condition.

Regex negative match on multiple column with and operator in AWK

I have simple file with sample log:
192.168.1.117 60921 224.0.0.252 5355 yahoo.com
192.168.1.117 60900 192.168.1.118 5356 118.1.168.192.in-addr.arpa
192.29.1.221 5943 211.29.132.12 4325 webex.com
192.168.1.221 5943 192.29.132.12 4325 lizzard.com
I am using awk in following way to filter internal traffic and arpa domain:
awk '($1 !~ ^([192.168]|[192.29]) && $3 !~ ^([192.168]|[192.29])) || $5 ~ (in\-addr\.arpa$)' < filein
I am not sure what I am doing wrong as I am not getting any output?
You are missing to wrap the regular expression between //. Furthermore the use of [] is wrong.
It should be:
awk '($1 !~ /^192\.(168|29)/ && $3 !~ /^192\.(168|29)/) || $5 ~ /in\-addr\.arpa$/' file

How to remove from file specific content from another file?

I have a file foo.txt:
$cat foo.txt
<ul>
<li>
<p>something</p>
</li>
<li>
<p>something else</p>
</li>
</ul>
And a bar.txt:
$cat bar.txt
<li>
<p>something</p>
</li>
And I want the desired output:
<ul>
<li>
<p>something else</p>
</li>
</ul>
I have tried:
$sed '{/r bar.txt/} d' foo.txt
But it didn't work, and I cannot do:
$sed '/<li>/,/</li>/ d' foo.txt
because there are other elements.
This awk one-liner works for your example:
awk -v RS="" '{gsub(/\n/,"\x99")}NR==FNR{t=$0;next}{gsub(t,"");gsub(/\x99/,"\n");print}' bar foo
not exactly the same output (empty line), but you got the idea. see the short explanation below the example.
see the example below:
kent$ head foo bar
==> foo <==
<ul>
<li>
<p>something</p>
</li>
<li>
<p>something else</p>
</li>
</ul>
==> bar <==
<li>
<p>something</p>
</li>
kent$ awk -v RS="" '{gsub(/\n/,"\x99")}NR==FNR{t=$0;next}{gsub(t,"");gsub(/\x99/,"\n");print}' bar foo
<ul>
<li>
<p>something else</p>
</li>
</ul>
Add short explanation
The basic idea is, replace linebreak with invisible char (in example I used \x99), then we have two single line strings. we can do the match and replacement. after we processed the strings, replace all \x99 back to linebreak to get the original format. This idea works for sed too, but a bit complicated, you have to make a label and play with pattern/hold spaces...
In the example I just used RS="" (I am a bit lazy). you could use sprintf function to build the one-line string, it would be more generic, since both of your real files could have empty lines. (your example doesn't however)
The point is the invisible char replacement part.
Good luck!
sed is an excellent tool for simple subsitutions on a single line, for anything else use awk. Here is a GNU awk solution:
$ gawk -v RS='\0' -v ORS= 'NR==FNR{re=$0;next} {sub(re,"")} 1' bar.txt foo.txt
<ul>
<li>
<p>something else</p>
</li>
</ul>
If "bar.txt" can contain RE metacharacters and you find those causing undesirable matches in the sub() (unlikely when matching large amounts of text) then you need to switch to an index()+substr()s solution to work with strings instead of REs, e.g.:
$ gawk -v RS='\0' -v ORS= '
NR==FNR { str=$0; rlength=length(str); next }
rstart = index($0,str) { $0 = substr($0,1,rstart-1) substr($0,rstart+rlength) }
1' bar.txt foo.txt
<ul>
<li>
<p>something else</p>
</li>
</ul>

Resources