Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting multiple values from text file

New to PowerShell and trying hard to get something out of the text file.

Here is sample data:

RDFC1111 Z MED 22 23:18:39 MPHSFHFKD OF THE AAAOAAAY GAAAAAA~ PAGE; 1 
DEFSDLSERD FSGHS CONRFGL CEERTE 
ASDF DFGF ASDA ERDFG REEVHT 
QWERTY SDFGTE: 3160 - ASDMPBVC FGHJINCIAL OFGTFDSS 
JHGTR ------- ASDV---------- FIELD IN ERROR ERROR DESCRIPTION 
TYPE JINYNGT HJUBGD N[ID7BER CRT JUR YR INFORMATION NJHGJVGFW -------------------- --------------------- 
PD31 MK56502 3160 311 50 02156334001 LOPJUT SURPKJH 
„ ERROR MESSAGE LOPJUT PKJH IS MANDATORY 
PD31 CD15622 3160 311 50 03214114001 LOPJUT ADDRESS - STREET 
„ ERROR MESSAGE LOPJUT ADDRESS IS MANDATORY 
PD31 AK12102 3160 311 50 02652224001 LOPJUT ADDRESS - CITY 
„ ERROR MESSAGE LOPJUT ADDRESS IS MANDATORY 
PD31 A4833-00009-61001 HJPOOL 3160 311 50 06585527001 LOPJUT GIVEN PKJH 
AKASHDEEP „ ERROR MESSAGE LOPJUT PKJH IS MANDATORY 
PD31 A5709-00000-00322 RAAK 3160 311 50 02197133001 LOPJUT GIVEN PKJH 
AMANDEEP „ ERROR MESSAGE LOPJUT PKJH IS MFiNDATORY 
PD31 A4781-00009-90503 NUMJ13 3160 311 50 05501950001 LOPJUT GIVEN PKJH 
AJAYKARPN „ ERROR MESSAGE LOPJUT PKJH IS MANDATORY 
~#~ 3~ ~•~5~ 9~4 -~o aa<a•„webnuai u~,~~~L Lt~«<.uc ~, 
- w 
J, _ 
si~_o_. ,.._,~r~':L _ ~r.;~~n< r~~ n~in:~r~~P t+m5'1' P~ EQ~rc~. 'r0 ~S~ta`~I~ 7 
PD31 50394-00008-80406 AY51619 3160 311 50 02177107001 LOPJUT GIVEN PKJH 

I'm trying to extract the bold fields from each line.

The output I'm looking for:

MK12001 C2100123
MK13103 C2100124
MRDOOP C21005237
JPPK C2123133

What I'm getting currently:

MK12001 3160
C2100123
MK13103 3160
C2100124
MRDOOP 3160
C21005237
JPPK 3160
C2123133

Problem:

Because I'm using "3160" for my match criteria for my first field so it's showing up in the results as well and for my second field which is a ticket# (C1234567), due to the use of "pipe" operator or "second" search/match criteria its going in the next line, If someone can help me to keep the ticket # in the same field then I guess I can live with having "3160" in between so it will look like

MK12001 3160 C2100123

or if someone can suggest me to only display the bold fields i.e before the 3160 then that would be awesome.

MK12001 C2100123

P.S: with my script, I'm already changing the "0" to "C" in ticket # field (C1234567)

Here is the code so far:

#Location of original file
$Location = "C:\Temp\Ap8.txt"
#Location of file where the "0" is replaced with "C"
$Location2 = "C:\Temp\results9.txt"
#Final results
$Location3 = "C:\Temp\tickets9.txt"

#get the original file
$Change = Get-Content $Location

# replace C with 0
$Change | ForEach-Object {$_ -Replace "3160 311 50 0",  "3160 311 50 C"} | 

#write the results to staging file
Set-Content $Location2

#get the staging/udpated file
Get-Content $Location2 -Raw |

#look up for a specific fileds, I have to fetch two fields from each line therefore using pipe operator inbetween
Select-String "\s\w{1,8}\s3160| C\d{7}" -AllMatches |


  % { $_.Matches.Groups.Value } |
  Out-File $Location3 -Encoding ascii -Force
like image 547
G R Avatar asked Nov 28 '25 04:11

G R


2 Answers

i am truly bad with complex regex patterns, so this is done with string operators and only very simple regex patterns. [grin]

the code ...

#region >>> fake reading in a text file
#    when ready to do thiw with real data, use Get-Content
$InStuff = @'
RD32 MK12001 3160 211 50 02100123001 SERVER LOCAL - STREET„ ERROR MESSAGE SERVER LOCAL IS MANDATORY 
RD32 MK13103 3160 211 50 02100124001 SERVER LOCAL - CITY„ ERROR MESSAGE SERVER LOCAL IS MANDATORY 
RD32 J4834-00009-92051 MRDOOP 3160 211 50 021005237001 SERVER GIVEN NAME PETER „ ERROR MESSAGE SERVER NAME IS MANDATORY 
RD32 B5509-00000-00522 JPPK 3160 211 50 02123133001 SERVER GIVEN NAME SUNNY „ ERROR MESSAGE SERVER NAME IS MFiNDATORY
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a text file

$Result = foreach ($IS_Item in $InStuff)
    {
    $TempBlock = ($IS_Item -split 'server')[0].trim()
    $First = (($TempBlock -split '3160')[0].Trim().Split())[-1]
    $Second = (($TempBlock -split '3160')[1].Trim().Split())[-1] -replace '\d{3}$' -replace '^0', 'C'

    '{0} {1}' -f $First, $Second
    }

$Result

output ...

MK12001 C2100123
MK13103 C2100124
MRDOOP C21005237
JPPK C2123133

what it does ...

  • fakes reading in a text file
    when doing this with real data, use Get-Content.
  • iterates thru the collection of lines
  • grabs the block that has the wanted data
  • splits out, trims, and saves the 1st target value
  • splits out, trims, and saves the 2nd target value
  • builds the output string from the 2 above values
  • sends that to the $Result collection
  • shows that collection on screen
like image 200
Lee_Dailey Avatar answered Nov 29 '25 22:11

Lee_Dailey


I suggest a regex approach based on the -match and -replace operators:

# The substring that the lines of interest must contain
# Note:
#  [regex]::Escape() escapes the literal string so that the regex
#  engine uses it literally - which isn't strictly necessary in this case.
#  Alternatively, omit [regex]::Escape() and formulate the string
#  *as a regex* to begin with.
$searchStr = [regex]::Escape(' 3160 311 50 ')

# Filter the lines down to those of interest with -match,
# then use -replace to extract the tokens on either side of the search string.
@(Get-Content $Location) `
  -match $searchStr `
  -replace "^.+ (\w+)${searchStr}0(\w+)\d{3} .+$", '$1 C$2'

The above outputs to the display; pipe to Out-File (or, with text input, preferably, Set-Content) as needed.

For an explanation of the regex and the ability to experiment with it, see this regex101.com page.

Output with your sample data:

MK56502 C2156334
CD15622 C3214114
AK12102 C2652224
HJPOOL C6585527
RAAK C2197133
NUMJ13 C5501950
AY51619 C2177107

Alternatively, use a switch statement, which requires only one regex operation, but requires calling via a script block (& { ... }) in order to be able to pipe to a file-writing cmdlet:

& {
  switch -Regex -File $Location {
    ' (\w+) 3160 311 50 0(\w+)\d{3} ' { '{0} C{1}' -f $Matches[1], $Matches[2] }
  }
} # | Set-Content ...
like image 23
mklement0 Avatar answered Nov 29 '25 23:11

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!