Quick ETL Hacks With PowerShell
Get Number of Fields And Values In A Delimited File
This is extremely useful if we need to get every 124th value (or another Nth value) from every line in a delimited file. Often, this is the case for a quick comparison or a quick grab of the data to see if a value may be what we're looking for.
Function Return-Fields {
Param(
$file
)
Process
{
$data = Get-Content $file
$lineNo = 0
$valueNo = 0
foreach ($line in $data)
{
$lineNo++
Write-Output ("=====> Line $lineNo" + [Environment]::NewLine)
$valueNo = 0
foreach ($value in $line.Split("|"))
{
$valueNo++
Write-Output ("Value $valueNo : " + $value)
}
}
}
}
Return-Fields -file "myfile.txt"
We could further use logic such as $line.Split("|")[3] if we wanted the 4th element or something similar. This allows us to drill further into the data.
Get Breakdown of Delimited File By Lines and Values
This is a very helpful script if we're quickly trying to get a specific value from a delimited file without importing the file. It gives us the line number and the value number. So if we need to find the 4th's lines 22nd value, it becomes easy. This also optionally outputs the entire headers in a line, so if we need these stacked, we can get them quickly. If we want to add a further delimiter (like html), then we can insert that as well.
$string = "ID#,IDidItRight,MoMoney$,I Need Some Space"
$iterate = $string.Split(",")
$finalstring = ""
foreach ($i in $iterate)
{
Write-Output ('"' + $i.ToLower().Replace("#","").Replace("$","").Replace(" ","") + '"')
$finalstring += $i.ToLower().Replace("#","").Replace("$","").Replace(" ","")
}
Write-Output ([Environment]::NewLine)
Write-Output ($finalstring)
Keep in mind that with PowerShell when using Get-Content, we can select the first number of rows.
More ETLHelp posts:
- Maven Build Project Without Unit Tests and Reporting Errors
- Spark Join Hacks With Scala
- Quick ETL Hacks With PowerShell
SQL In Six Minutes (YouTube) | SQL In Six Minutes (Odysee) | Automating ETL | T-SQL In 2 Hours | Consumer Guide To Digital Security
Leave Quick ETL Hacks With PowerShell to:
Read more #data posts
Best Posts From SqlinSix
We have not curated any of sqlinsix's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From SqlinSix
- Solution When ISJSON May Not Be A Recognized Function Name
- Creating a GUID For Each GUID
- Get A Past Bash Command Quickly
- Connecting A Data Factory To An Existing Runtime In Azure
- Backing Up A Database With MongoDump
- SQL Tutorial: Using UNION ALL or UNION and Why
- T-SQL: How To UNION ALL Tables and Why
- SQL Tutorial: Solving Data Differentials With LEFT JOINs Only
- Solving Data Differentials With LEFT JOINs
- Spark Scala: Grouping Values In A Key-Value Pair
- Transaction Log Becoming Full Due To Replication
- Why We Should Be Skeptical About Data Regarding the Physical World
- Does the Same Data Mean the Same Conclusions?
- SQL Tutorial: How To Use ROW NUMBER() and Why
- Not Able To Drop Table Because of Reference
- T-SQL: How To Use DENSE_RANK()
- The Decentralized Myth - Beyond the Hype
- SQL Tutorial: INNER JOINs vs LEFT JOINs
- T-SQL: How To Use PARTITION BY and Why
- Weekly Tech Lesson: CROSS JOINs Versus CROSS APPLY