# Cutting line in powershell

**URL:** <https://forums.powershell.org/t/cutting-line-in-powershell/9793>\
**Category:** PowerShell Help\
**Created:** [December 1, 2017, 6:07am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793 "2017-12-01T06:07:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![henk](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@henk](https://forums.powershell.org/u/henk)\
**Post date:** [December 1, 2017, 6:07am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/1 "2017-12-01T06:07:37Z")

</div>

Hi there,

I am new in Powershell scripting, so sorry for the questions.  
I am busy writing a script and need some help. I will try explaining step by step what I want to do:

1. I am pulling data from a DB into a flat file (c:\temp\db.txt). The info what is in the file:  
Output:  
Submitted/Executing Jobs  
ORDERID NAME TYPE ODATE PID RUNCNT

* * *

00000005|Test |FLD |20171201| |22 |  
00000006|sleep |CMD |20171201|12688 |22 |

Result: Success.

1. I now need to grab the one line and then grab the one column / string. I have defined doing this in Powershell, and here my line I use and I pump that into a flat file, c:\temp\output.txt:

- My command:  
cat c:\temp\db.txt | where {$\_-match “sleep”} \> c:\temp\output.txt
- output of the file:  
00000006|sleep |CMD |20171201|12688 |22 |

Problem:  
I need to grab now the first 8 characters on this line and pipe it to a variable which I will then use to execute an external command using this variable.

I tried the following: (I use output file to see if I pass the correct information - I need ot pass it as a variable)  
cat c:\temp\db.txt | where {$_-match “sleep”} | foreach{$_.split()[0]} \> c:\temp\output.txt  
But the output bring me the following:  
00000006|sleep  
So it sees this as 1 string.

1- How can I get only those characters? (00000006)  
2- How can I pass that to a variable which I then can use again for external execution?

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [December 1, 2017, 7:35am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/2 "2017-12-01T07:35:59Z")

</div>

You seem like someone who came from a long background of working with Linux where we do a lot of text parsing and manipulation. My advise is not to try to use PowerShell in the same manner. PowerShell like Windows deals in objects not text. Unlike text, objects have properties and methods that lend themselves very handy in scripting and processing.

Having said that I recommend pulling the data out of the DB and processing it from there bypassing the unnecessary step of dumping it into text file (I’m assuming that the only reason we’re using a text file is to read, parse, and process the data).

You’ll see that the code I will recommend will take the data from the text file and turns it back to more structured objects.

For example, in this code block:

```
$TextFile = '.\db1.txt'
$CSVFile = '.\db1.csv'

$myTextLines = Get-Content $TextFile
$myTextLines[0].Split(' ').Trim() -join ',' | Out-File $CSVFile 
2..($myTextLines.count-1) | % { $myTextLines[$_].Split('|').Trim() -join ',' | Out-File $CSVFile -Append }

$myRecordSet = Import-Csv $CSVFile 
$myRecordSet | FT -a
```

where .\db1.txt content is

```
ORDERID NAME TYPE ODATE PID RUNCNT
——– —————– —– ——– —————- ——
00000005|Test |FLD |20171201| |22 |
00000006|sleep |CMD |20171201|12688 |22 |
```

.\db1.csv content will be

```
ORDERID,NAME,TYPE,ODATE,PID,RUNCNT
00000005,Test,FLD,20171201,,22,
00000006,sleep,CMD,20171201,12688,22,
```

and console output will be

```
ORDERID NAME TYPE ODATE PID RUNCNT
------- ---- ---- ----- --- ------
00000005 Test FLD 20171201 22    
00000006 sleep CMD 20171201 12688 22
```

Now this is a lot easier to process than parsing text every time you need to use a data element…  
For example, getting the ‘orderid’ where ‘name’ is ‘sleep’ is simply:

```
PS D:\Sandbox> ($myRecordSet | ? { $_.Name -eq 'sleep' }).orderid
00000006
```

You’re simply leveraging the way PS was designed to work…  
To use that data as an executable:

```
& ($myRecordSet | ? { $_.Name -eq 'sleep' }).orderid
```

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 4, 2017, 12:30pm UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/3 "2017-12-04T12:30:02Z")

</div>

Did someone say “cut”?

```
function cut {
  param(
    [Parameter(ValueFromPipeline=$True)] [string]$inputobject,
    [string]$delimiter='\s+',
    [string[]]$field
  )

    process {
    if ($field -eq $null) { $inputobject -split $delimiter } else {
      ($inputobject -split $delimiter)[$field] }
  }
}

PS C:\> echo hi:there | cut -d : -f 1
there
```

---

<div class="post-metadata">

**Author:** ![henk](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@henk](https://forums.powershell.org/u/henk)\
**Post date:** [December 6, 2017, 1:29am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/4 "2017-12-06T01:29:28Z")

</div>

Hi Sam,

Thank you for the code. Yes, I am from a UNIX back ground so bit different on Windows 🙂

I have tried your code and made a small change, now it is the final bash 🙂

CODE:  
$TextFile = ‘c:\temp\db1.txt’  
$CSVFile = ‘c:\temp\db1.csv’  
$myTextLines = Get-Content $TextFile  
$myTextLines[0].Split(’ ').Trim() -join ‘,’ | Out-File $CSVFile  
2..($myTextLines.count-1) | % { $myTextLines[$_].Split(‘|’).Trim() -join ‘,’ | where {$_-match “sleep”} | Out-File $CSVFile -Append }  
$myRecordSet = Import-Csv $CSVFile  
$myRecordSet | FT -a

OUTPUT:  
PS C:\Temp\> C:\Temp\script.ps1

## Output:

00000006

Question:  
How do I now echo “00000006” as a variable? (let’s say variable name is ORDERID)  
I will need to execute an external command from the same script like this:  
&“C:\test\script.exe” %%ORDERID

---

<div class="post-metadata">

**Author:** ![gio122](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@gio122](https://forums.powershell.org/u/gio122)\
**Post date:** [December 6, 2017, 1:54am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/5 "2017-12-06T01:54:24Z")

</div>

Hello.

You can use this

Code:  
$ORDERID = $myRecordSet.ORDERID  
$ORDERID

Output:  
00000006

Or just use $myRecordSet.ORDERID as the variable.

---

<div class="post-metadata">

**Author:** ![henk](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@henk](https://forums.powershell.org/u/henk)\
**Post date:** [December 6, 2017, 3:21am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/6 "2017-12-06T03:21:01Z")

</div>

Hi Anders,

The Output should not be there  
Output:  
00000006

I need to have only the “00000006” as a variable

---

<div class="post-metadata">

**Author:** ![gio122](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@gio122](https://forums.powershell.org/u/gio122)\
**Post date:** [December 6, 2017, 4:13am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/7 "2017-12-06T04:13:45Z")

</div>

Hello again,

If you try and run the code, you will see that it only prints whats inside the variable.  
Output is not shown.

Output is just written so you could see what the code was and what the output is.

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [December 6, 2017, 7:26am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/8 "2017-12-06T07:26:14Z")

</div>

```
$TextFile = '.\db1.txt'
$CSVFile = '.\db1.csv'
$myExePath = 'C:\windows\notepad.exe'

$myTextLines = Get-Content $TextFile
$myTextLines[0].Split(' ').Trim() -join ',' | Out-File $CSVFile 
2..($myTextLines.count-1) | % { $myTextLines[$_].Split('|').Trim() -join ',' | Out-File $CSVFile -Append }

$myRecordSet = Import-Csv $CSVFile 

$myArg = ($myRecordSet | ? { $_.Name -eq 'sleep' }).orderid

& $myExePath $myArg
```

---

<div class="post-metadata">

**Author:** ![henk](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@henk](https://forums.powershell.org/u/henk)\
**Post date:** [December 8, 2017, 3:06am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/9 "2017-12-08T03:06:30Z")

</div>

Guys,

I got so far at the moment as the other coding seems not to work as it should

So here the code:  
cat c:\temp\db1.txt | where {$_-match “sleep”} | foreach{$_.split()[0]} \> c:\temp\ORDERID.txt  
Get-Content .\ORDERID.txt | % { ($\_.Split(‘|’))[0] } \> C:\Temp\ORDERID1.txt  
**Content of ORDERID1.txt is the value I am looking for (example of the file) - THIS WORKS 100%  
0000000i**

**So how do I put this output as a variable?? (Lets say varibale name is “TEST”**

 

**And I need to run a command:  
&“c:\temp\test.exe” %%TEST**

---

<div class="post-metadata">

**Author:** ![gio122](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@gio122](https://forums.powershell.org/u/gio122)\
**Post date:** [December 8, 2017, 7:10am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/10 "2017-12-08T07:10:21Z")

</div>

Hello,

This will work.

$TextFile = ‘c:\temp\db1.txt’  
$CSVFile = ‘c:\temp\db1.csv’  
$myTextLines = Get-Content $TextFile  
$myTextLines[0].Split(’ ').Trim() -join ‘,’ | Out-File $CSVFile  
2…($myTextLines.count-1) | % { $myTextLines[$_].Split(‘|’).Trim() -join ‘,’ | where {$_-match “sleep”} | Out-File $CSVFile -Append }  
$myRecordSet = Import-Csv $CSVFile

$test = $myRecordSet.ORDERID

cmd.exe /C c:\temp\test.exe %%$test

If you try and run the $test you will see it only contains 00000006

---

<div class="post-metadata">

**Author:** ![henk](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@henk](https://forums.powershell.org/u/henk)\
**Post date:** [December 12, 2017, 2:20am UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/11 "2017-12-12T02:20:31Z")

</div>

Hi Guys, Thank you for your help so far.

I have one last question on this. In my txt file that I am reading, I need to search for an **exact string**.

Here is the script so far:  
$source = “C:\temp\dboutput.txt”  
$destination = “C:\temp\output.txt”  
$SearchString = “HENK\_Test01\_TST\_Uptime\_Process”  
Get-Content $source | foreach{$_.split()[0]} | where {$_-match “HENK”} \> $destination

Output file looks like this:  
000004c4|HENK\_Test01\_TST\_Uptime\_Process|CMD  
000004c5|HENK\_Test01\_TST\_Uptime\_Program|CMD

**I only want to see the line:  
000004c4|HENK\_Test01\_TST\_Uptime\_Process|CMD**

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:37pm UTC](https://forums.powershell.org/t/cutting-line-in-powershell/9793/12 "2024-05-16T20:37:01Z")

</div>


