rss logo

PowerShell text formating examples

I will put here some PowerShell examples about text formating.

Examples

Extract words from sentence

PS C:\Users\utilisateur> $text="R006 : Picture n°1 : OK 001.png"
PS C:\Users\utilisateur> $text.split(":")[0] -replace ' ', ''
R006
PS C:\Users\utilisateur> $text="R006 : Picture n°1 : OK 001.png"
PS C:\Users\utilisateur> $text -replace ".*(\d\d\d.png).*",'$1'
001.png
PS C:\Users\utilisateur> $text="00686;R006;""This is a text."";NOK;;Random text;;;"
PS C:\Users\utilisateur> $text.split(";")[2] -replace '"', ''
This is a text.

Get lines from a file which matches a word

PS C:\Users\utilisateur> Get-Content 'c:\01.csv' | Where-Object { $_ -match "R006" }
PS C:\Users\utilisateur> 00686;R006;This is a text.;NOK;;Random text;;;

A concrete case

I had two files. A text and csv formats. I wanted to mix informations of both to build LaTeX output.

01.txt

R006 : Picture n°1 : OK 001.png
R007 : ?

R010 : OK 001.png
R011 : OK 002.png
R012 : OK 002.png
R014 : OK 003.png

R015 : OK 004.png

R017 : OK 006.png -> Random comment here

01.csv

00429;R001;And the Lord said to Moses,;NOK;;Random Text;;;;
00444;R002;Yes, you will be like him who takes his rest on the sea, or on the top of a sail-support.;NOK;;Random Text;;;;
00173;R003;To the chief music-maker on Aijeleth-hash-shahar. A Psalm. Of David. My God, my God, why are you turned away from me? why are you so far from helping me, and from the words of my crying?;NOK;;Random Text;;;;
00040;R004;Then Moses and Aaron went in to Pharaoh, and said to him, This is what the Lord, the God of the Hebrews, says: How long will you be lifted up in your pride before me? let my people go so that they may give me worship.;OK;;Random Text;;;;
00378;R005;And turning to the people again, he said to them, Give ear to me all of you, and let my words be clear to you:;NOK;;Random Text;;;;
00579;R006;And do not take overmuch wine by which one may be overcome, but be full of the Spirit;;NOK;;Random Text;;;;
00445;R007;Let me say what is in my mind, and after that, go on making sport of me.;NOK;;Random Text;;;;
00575;R008;This is what the Lord of armies, the God of Israel, has said: Go and say to the men of Judah and the people of Jerusalem, Is there no hope of teaching you to give ear to my words? says the Lord.;OK;;Random Text;;;;
00496;R009;They even made offerings of their sons and their daughters to evil spirits,;OK;;Random Text;;;;
00171;R010;Now there was an old prophet living in Beth-el; and one of his sons came and gave him word of all the man of God had done that day in Beth-el, and they gave their father an account of the words he had said to the king.;NOK;;Random Text;;;;
00485;R011;And Jesus said to them, I will put to you one question; give me an answer, and I will say by what authority I do these things.;NOK;;Random Text;;;;
00130;R012;The sun to have rule by day: for his mercy is unchanging for ever.;NOK;;Random Text;;;;
00209;R013;And after everything which has come on us because of our evil-doing and our great sin, and seeing that the punishment which you, O God, have given us, is less than the measure of our sins, and that you have kept from death those of us who are here;;OK;;Random Text;;;;
00258;R014;Then, stretching yourself out on your left side, take the sin of the children of Israel on yourself: for as long as you are stretched out, so long will the sin of the children of Israel be on you.;NOK;;Random Text;;;;
00098;R015;Now if you give ear to the voice of the Lord your God, and keep with care all these orders which I have given you today, then the Lord your God will put you high over all the nations of the earth:;OK;;Random Text;;;;
00081;R016;It may be that their prayer for grace will go up to the Lord, and that every man will be turned from his evil ways: for great is the wrath and the passion made clear by the Lord against this people.;OK;;Random Text;;;;
00168;R017;And the Lord God said, Now the man has become like one of us, having knowledge of good and evil; and now if he puts out his hand and takes of the fruit of the tree of life, he will go on living for ever.;OK;;Random Text;;;;

Format.ps1

$txtFile = Get-Content 'c:\01.txt'

$txtFile | ForEach-Object {

#test, to avoid empty line
if ($_) { 
    $R = $_.split(":")[0] -replace ' ', ''
    $P = $_ -replace ".*(\d\d\d.png).*",'$1'
    $subject = Get-Content 'c:\01.csv' | Where-Object { $_ -match "$R" }
    $S = $subject.split(";")[2] -replace '"', ''
    "\section{$R - $S}" | Out-File -FilePath .\Latex.txt -Append

    $C = $_ -replace "[OK][KO] (.*)\d\d\d\.png.*",'$1'
    $C2 = $C.split(":")[1] -replace '^\s', ''

    if ($_ -match "KO")
    {
        "\textcolor{Red}{\textbf{KO : $C2}}" | Out-File -FilePath .\Latex.txt -Append
    }
    else {
        "\textcolor{LimeGreen}{\textbf{OK : $C2}}" | Out-File -FilePath .\Latex.txt -Append
    }

    "\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/$P}
\end{center}" | Out-File -FilePath .\Latex.txt -Append
    "`n" | Out-File -FilePath .\Latex.txt -Append
    }
}

Output

\section{R006 - And do not take overmuch wine by which one may be overcome, but be full of the Spirit}
\textcolor{LimeGreen}{\textbf{OK : Picture n°1 }}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/001.png}
\end{center}


\section{R007 - Let me say what is in my mind, and after that, go on making sport of me.}
\textcolor{LimeGreen}{\textbf{OK : ?}}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/R007 : ?}
\end{center}


\section{R010 - Now there was an old prophet living in Beth-el}
\textcolor{LimeGreen}{\textbf{OK : }}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/001.png}
\end{center}


\section{R011 - And Jesus said to them, I will put to you one question}
\textcolor{LimeGreen}{\textbf{OK : }}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/002.png}
\end{center}


\section{R012 - The sun to have rule by day: for his mercy is unchanging for ever.}
\textcolor{LimeGreen}{\textbf{OK : }}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/002.png}
\end{center}


\section{R014 - Then, stretching yourself out on your left side, take the sin of the children of Israel on yourself: for as long as you are stretched out, so long will the sin of the children of Israel be on you.}
\textcolor{LimeGreen}{\textbf{OK : }}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/003.png}
\end{center}


\section{R015 - Now if you give ear to the voice of the Lord your God, and keep with care all these orders which I have given you today, then the Lord your God will put you high over all the nations of the earth:}
\textcolor{LimeGreen}{\textbf{OK : }}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/004.png}
\end{center}


\section{R017 - And the Lord God said, Now the man has become like one of us, having knowledge of good and evil}
\textcolor{LimeGreen}{\textbf{OK : }}
\begin{center}
\includegraphics[width=80mm]{20200120_The_Book/006.png}
\end{center}
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address