Linux : transform blocks of X lines string into 1 line string - linux

I have in my PHP file this string :
$sql = "SELECT p.pac_rut a, ";
$sql .= " p.pac_dig b, ";
$sql .= " p.pac_apepat c, ";
$sql .= " p.pac_apemat d, ";
$sql .= " p.pac_nombre e, ";
$sql .= " to_char(p.pac_fecha_nac,'dd/mm/yyyy') as f, ";
$sql .= " (to_char(sysdate,'yyyy')-to_char(p.pac_fecha_nac,'yyyy')) as g, ";
$sql .= " p.sex_cod h ";
$sql .= "FROM mytable ";
$sql .= "WHERE p.pac_rut=$rut";
And sed ':a;N;$!ba;s/\n//g' | sed 's/"; $sql .= "//g' | sed 's/ //g' | sed 's/=\$/=:/g' gives me the result ok like : $sql= "SELECT p.pac_rut a,p.pac_dig b,p.pac_apepat c,p.pac_apemat d,p.pac_nombre e,to_char(p.pac_fecha_nac,'dd/mm/yyyy') as f,(to_char(sysdate,'yyyy')-to_char(p.pac_fecha_nac,'yyyy')) as g,p.sex_cod h FROM salud.pacientes p WHERE p.pac_rut=:rut";
But if a have 2 (or more) blocks of sql queries, i need a function which can build 2 (or more) queries sql on 2 (or more) lines.
For ejample, i have :
$sql = "SELECT p.pac_rut a ";
$sql .= "FROM mytable ";
$sql .= "WHERE p.pac_rut=$rut";
and
$sql2 = "SELECT p.pac_rut a ";
$sql2 .= "FROM mytable ";
$sql2 .= "WHERE p.pac_rut=$rut";
in the same file, i need this result :
$sql = "SELECT p.pac_rut a FROM mytable WHERE p.pac_rut=$rut";
$sql2 = "SELECT p.pac_rut a FROM mytable WHERE p.pac_rut=$rut";
How can i do that with awk or sed or other linux tool ?

Related

powershell output to .txt file wont print the for loop

I have some audio, video & jpg files.
I am using ffmpeg to get all the duration
all thing works perfect, I have got all durations from a for loop and set it to a variable, when I echo it, it works perfectly, but when I output it to a .txt file it prints only the last loop
any suggestion
below is my code
$file = "D:\edl\input\aa.xlsx"
$sheetName = "Sheet1"
#Create an instance of Excel.Application and Open Excel file
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
#Count max row
$rowMax = ($sheet.UsedRange.Rows).count
#Declare the starting positions
$rowName,$colName = 1,1
$rowAge,$colAge = 1,2
$rowCity,$colCity = 1,3
$rowMessage,$colMessage = 1,9
#loop to get values and store it
for ($i=0; $i -le $rowMax-1; $i++)
{
$id = $sheet.Cells.Item($rowName+$i,$colName).text
$name = $sheet.Cells.Item($rowAge+$i,$colAge).text
$subtitle = $sheet.Cells.Item($rowCity+$i,$colCity).text
$filename2 = "$id.jpg"
$filename = "$id.mp4"
$filename1 = "$id.m4a"
$duration2 = if ((ffmpeg -i $filename2 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
$duration1 = if ((ffmpeg -i $filename1 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
$duration = if ((ffmpeg -i $filename 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
if($duration2){
$duration2 = "02:00:11:15"
$bb = $duration2
$out = "$i" + " " + "AX" + " " + "V" + " " + "C" + " " + "00:00:00:00" + " " + $bb + " "+ "* FROM CLIP NAME: " + $id+".jpg"
}
if($duration1){
$out1 = "$i"+ " " + "AX" + " " + "AA" + " " + "C" + " " + "00:00:00:00" + " " + $duration1 + " " + "* FROM CLIP NAME: " + $id+".m4a"
}
if(!$duration){
}else{
$out2 = "$i" + " " + "AX" + " " + "AA/V" + " " + "C" + " " + "00:00:00:00" + " " + $duration + " "+ "* FROM CLIP NAME: " + $id+".mp4"
}
$gg = $out+"`n"+"`n"+$out1+"`n"+"`n"+$out2+"`n"
echo $gg
$gg | Out-File -FilePath d:\edl\input\output.txt
}
#close excel file
$objExcel.quit()
when i echo variable $gg
this is the output
0 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.jpg
0 AX AA C 00:00:00:00 00:00:03.48 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.m4a
0 AX AA/V C 00:00:00:00 00:00:07.64 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4
1 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 9a52a954-ffe9-4420-a701-68988923f81f.jpg
1 AX AA C 00:00:00:00 00:00:03.46 * FROM CLIP NAME: 9a52a954-ffe9-4420-a701-68988923f81f.m4a
0 AX AA/V C 00:00:00:00 00:00:07.64 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4
2 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.jpg
2 AX AA C 00:00:00:00 00:00:03.52 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.m4a
2 AX AA/V C 00:00:00:00 00:00:09.75 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.mp4
3 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 73aa1f8e-1067-4efd-b2bd-6748a088f33a.jpg
3 AX AA C 00:00:00:00 00:00:03.21 * FROM CLIP NAME: 73aa1f8e-1067-4efd-b2bd-6748a088f33a.m4a
2 AX AA/V C 00:00:00:00 00:00:09.75 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.mp4
4 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.jpg
4 AX AA C 00:00:00:00 00:00:03.05 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.m4a
4 AX AA/V C 00:00:00:00 00:00:06.93 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.mp4
but my txt file output
4 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.jpg
4 AX AA C 00:00:00:00 00:00:03.05 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.m4a
4 AX AA/V C 00:00:00:00 00:00:06.93 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.mp4
By default, if a file exists in the specified path, Out-File overwrites the file without warning. Use -Append parameter (Adds the output to the end of an existing file):
$gg | Out-File -FilePath d:\edl\input\output.txt -Append
BTW, as an alternative to
echo $gg
$gg | Out-File -FilePath d:\edl\input\output.txt -Append
use Tee-Object cmdlet (It stores the output in a file or variable and also sends it down the pipeline. If Tee-Object is the last command in the pipeline, the command output is displayed at the prompt):
$gg | Tee-Object -FilePath d:\edl\input\output.txt -Append

How do I export a to an excel spreadsheet using powershell

I have this PowerShell script that strips html tags and just leaves the text and have it display the word count for that html file when the script is executed. My question is when I execute:
function Html-ToText {
param([System.String] $html)
# remove line breaks, replace with spaces
$html = $html -replace "(`r|`n|`t)", " "
# write-verbose "removed line breaks: `n`n$html`n"
# remove invisible content
#('head', 'style', 'script', 'object', 'embed', 'applet', 'noframes', 'noscript', 'noembed') | % {
$html = $html -replace "<$_[^>]*?>.*?</$_>", ""
}
# write-verbose "removed invisible blocks: `n`n$html`n"
# Condense extra whitespace
$html = $html -replace "( )+", " "
# write-verbose "condensed whitespace: `n`n$html`n"
# Add line breaks
#('div','p','blockquote','h[1-9]') | % { $html = $html -replace "</?$_[^>]*?>.*?</$_>", ("`n" + '$0' )}
# Add line breaks for self-closing tags
#('div','p','blockquote','h[1-9]','br') | % { $html = $html -replace "<$_[^>]*?/>", ('$0' + "`n")}
# write-verbose "added line breaks: `n`n$html`n"
#strip tags
$html = $html -replace "<[^>]*?>", ""
# write-verbose "removed tags: `n`n$html`n"
# replace common entities
#(
#("&bull;", " * "),
#("&lsaquo;", "<"),
#("&rsaquo;", ">"),
#("&(rsquo|lsquo);", "'"),
#("&(quot|ldquo|rdquo);", '"'),
#("&trade;", "(tm)"),
#("&frasl;", "/"),
#("&(quot|#34|#034|#x22);", '"'),
#('&(amp|#38|#038|#x26);', "&"),
#("&(lt|#60|#060|#x3c);", "<"),
#("&(gt|#62|#062|#x3e);", ">"),
#('&(copy|#169);', "(c)"),
#("&(reg|#174);", "(r)"),
#("&nbsp;", " "),
#("&(.{2,6});", "")
) | % { $html = $html -replace $_[0], $_[1] }
# write-verbose "replaced entities: `n`n$html`n"
return $html + $a | Measure-Object -word
}
And then run:
Html-ToText (new-object net.webclient).DownloadString("test.html")
it displays 4 words that are displayed in the output in PowerShell. How do I export that output from the PowerShell window into a an excel spreadsheet with the column words and the count 4?
The CSV you want just looks like this:
Words
4
it's easy enough to just write that to a text file, Excel will read it. But you're in luck, the output of Measure-Object is already an object with 'Words' as a property and '4' as a value, and you can feed that straight into Export-Csv. Use select-object to pick just the property you want:
$x = Html-ToText (new-object net.webclient).DownloadString("test.html")
# drop the Lines/Characters/etc fields, just export words
$x | select-Object Words | Export-Csv out.csv -NoTypeInformation
I'd be tempted to see if I could use
$x = Invoke-WebResponse http://www.google.com
$x.AllElements.InnerText
to get the words out of the HTML, before I tried stripping the content with replaces.
I figured it out. What I did was added
+ $a | Measure-Object -Word after the #html variable in the script and then ran:
Html-ToText (new-object net.webclient).DownloadString("test.html") + select-Object Words | Export-Csv out.csv -NoTypeInformation and it exported the word count – josh s 1 min ago

Opencart: USPS not showing International Services on checkout

opencart2.0.1.1
USPS setting:-Weight Class= Pound, Geo Zone= All Zones, Dimensions (L x W x H)=10x10x10, Zip Code=33446
product:- Weight Class = Pound, Dimensions (L x W x H)=10x10x10, Weight =4.
it is working properly in Domestic Services But International Services is not showing when i m checkout using other country(Mexico),
I just find out $this->config->get('usps_girth') not returning any value,
So i use formula to find out girth = L + W * 2 + H * 2 ;
In \catalog\model\shipping\usps.php i have changed at line 300 aprx.
From usps.php
$xml .= ' <Container>' . $this->config->get('usps_container') . '</Container>';
$xml .= ' <Size>' . $this->config->get('usps_size') . '</Size>';
$xml .= ' <Width>' . $this->config->get('usps_width') . '</Width>';
$xml .= ' <Length>' . $this->config->get('usps_length') . '</Length>';
$xml .= ' <Height>' . $this->config->get('usps_height') . '</Height>';
$xml .= ' <Girth>' . $this->config->get('usps_girth') . '</Girth>';
$xml .= ' <CommercialFlag>N</CommercialFlag>';
$xml .= ' </Package>';
$xml .= '</IntlRateV2Request>';
$request = 'API=IntlRateV2&XML=' . urlencode($xml);
To usps.php
//$this->config->get('usps_girth') -> to -> girth = L + W * 2 + H * 2
$girth =($this->config->get('usps_width'))+(($this->config->get('usps_length'))*2)+(($this->config->get('usps_height'))*2);
$xml .= ' <Container>' . $this->config->get('usps_container') . '</Container>';
$xml .= ' <Size>' . $this->config->get('usps_size') . '</Size>';
$xml .= ' <Width>' . $this->config->get('usps_width') . '</Width>';
$xml .= ' <Length>' . $this->config->get('usps_length') . '</Length>';
$xml .= ' <Height>' . $this->config->get('usps_height') . '</Height>';
$xml .= ' <Girth>' . $girth . '</Girth>';
$xml .= ' <CommercialFlag>N</CommercialFlag>';
$xml .= ' </Package>';
$xml .= '</IntlRateV2Request>';
$request = 'API=IntlRateV2&XML=' . urlencode($xml);

export the pipeline to a csv file

I am trying to figure out how to export to a csv file. I have no problem exporting it to excel, yet when I try to export it to a csv file I screw it up. I left out the ad pull.
Foreach($result in $results){
$User = $result.GetDirectoryEntry()
$DistinguishedName = $USER.DistinguishedName -replace "'", "''"
$CN = $USER.CN -replace "'", "''"
$name = $USER.name -replace "'", "''"
$userAccountControl = $USER.userAccountControl
$extensionAttribute7 = $User.extensionattribute7 -replace "'", "''"
$extensionAttribute1 = $User.extensionattribute1 -replace "'", "''"
$operatingSystem = $USER.operatingSystem -replace "'", "''"
$pwdLastSet = [datetime]::FromFileTimeUtc((ConvertADSLargeInteger $USER.pwdLastSet[0]))
if( ( [datetime]"1900-01-01" - $pwdLastSet ).Days -gt 0 )
{
$pwdLastSet = [datetime]"1900-01-01"
}
Populate General Sheet(1) with information
foreach ($objItem in $result){
$Sheet1.Cells.Item($intRow, 1) = $CN
$Sheet1.Cells.Item($intRow, 2) = $operatingSystem
$Sheet1.Cells.Item($intRow, 3) = $pwdLastSet
$Sheet1.Cells.Item($intRow, 4) = $DistinguishedName
$Sheet1.Cells.Item($intRow, 5) = $extensionAttribute7
$Sheet1.Cells.Item($intRow, 6) = $extensionAttribute1
}
Simply pipe $result into a ForEach-Object loop and select the relevant properties from the object and export:
$results | % { $_.GetDirectoryEntry() } |
select CN, operatingSystem, pwdLastSet, DistinguishedName,
extensionattribute7, extensionattribute1 |
Export-Csv 'C:\output.csv' -NoType
You can use calculated properties to replace properties with modified versions of themselves:
$results | % { $_.GetDirectoryEntry() } |
select #{n='CN';e={$_.CN -replace "'","''"}},
#{n='operatingSystem';e={$_.operatingSystem -replace "'","''"}},
#{n='pwdLastSet';e={
$d = [DateTime]::FromFileTimeUtc($_.pwdLastSet[0])
if ( $d.Date -lt [DateTime]"1900-01-01" ) {
[DateTime]"1900-01-01"
} else {
$d
}
}},
#{n='DistinguishedName';e={$_.DistinguishedName -replace "'","''"}},
#{n='extensionattribute7';e={$_.extensionattribute7 -replace "'","''"}},
#{n='extensionattribute1';e={$_.extensionattribute1 -replace "'","''"}} |
Export-Csv 'C:\output.csv' -NoType
I'd make an array, fill it with objects based on what you have in your script, then you can loop through that to fill out the spreadsheet, and can also use it to export to CSV.
$Users = #()
Foreach($result in $results){
$User = $result.GetDirectoryEntry()
$pwdLastSet = [datetime]::FromFileTimeUtc((ConvertADSLargeInteger $USER.pwdLastSet[0]))
if( ( [datetime]"1900-01-01" - $pwdLastSet ).Days -gt 0 )
{
$pwdLastSet = [datetime]"1900-01-01"
}
$Users+=[PSCustomObject][Ordered]#{
User = $User
DistinguishedName = $USER.DistinguishedName -replace "'", "''"
CN = $USER.CN -replace "'", "''"
name = $USER.name -replace "'", "''"
userAccountControl = $USER.userAccountControl
extensionAttribute7 = $User.extensionattribute7 -replace "'", "''"
extensionAttribute1 = $User.extensionattribute1 -replace "'", "''"
operatingSystem = $USER.operatingSystem -replace "'", "''"
pwdLastSet = $pwdLastSet
}
}
For($intRow=1;$intRow -le $Users.count;$intRow++){
$Sheet1.Cells.Item($intRow, 1) = $Users[$intRow-1].CN
$Sheet1.Cells.Item($intRow, 2) = $Users[$intRow-1].operatingSystem
$Sheet1.Cells.Item($intRow, 3) = $Users[$intRow-1].pwdLastSet
$Sheet1.Cells.Item($intRow, 4) = $Users[$intRow-1].DistinguishedName
$Sheet1.Cells.Item($intRow, 5) = $Users[$intRow-1].extensionAttribute7
$Sheet1.Cells.Item($intRow, 6) = $Users[$intRow-1].extensionAttribute1
}
$Users | Export-Csv C:\Path\To\NewFile.csv -NoTypeInformation

import data from excel sheet using codeigniter

I want to import sing column of excel sheet into database table name admin_record and excel sheet is only consist of one column and name of the column is bar codes and rows can be many.
I want to save it in database my table name is admin_record and its consists of only two fields
id (which is primary key)
bar_code.
find only this code which i totally cant understand
public function read_file($table = 'admin_record', $filename = 'example.xls') {
$pathToFile = 'uploads/' . $filename;
$this->load->library('Spreadsheet_Excel_Reader');
$data = new Spreadsheet_Excel_Reader($pathToFile);
$sql = "INSERT INTO $table (";
for($index = 1;$index <= $data->sheets[0]['numCols']; $index++){
$sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", ";
}
$sql = rtrim($sql, ", ")." ) VALUES ( ";
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
$valuesSQL = '';
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
$valuesSql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", ";
}
echo $sql . rtrim($valuesSql, ", ")." ) <br>";
}
}
your library file is wrong please use following link to download
Spreadsheet_Excel_Reader Library
public function my_test($table = 'admin_record', $filename = 'resources/uploads/c.xls')
{
$pathToFile = $filename;
$valuesSql="";
$this->load->library('Spreadsheet_Excel_Reader');
$data = new Spreadsheet_Excel_Reader($pathToFile);
$sql = "INSERT INTO $table (";
for($index = 1;$index <= $data->sheets[0]['numCols']; $index++){
$sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", ";
}
$sql = rtrim($sql, ", ")." ) VALUES ( ";
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
$valuesSQL = '';
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
$valuesSql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", ";
}
echo $sql . rtrim($valuesSql, ", ")." ) <br>";
} // add this line
}

Resources