[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Powershell regex

Posted on 2011-10-23
11
Medium Priority
?
507 Views
Last Modified: 2012-08-13
Hello,

I have the following txt file:

TEAMHAT1 at TEAMHAT2
TEAMHAT3 at TEAMHAT4
TEAMHAT5 vs TEAMHAT6

I want to split at "at" and "vs" and place each team on its own line, like this:

TEAMHAT1
TEAMHAT2
TEAMHAT3
...

In the sample code attached, if I use "at" in the split operator, it splits each line whenever it finds "at"...which is really not what I want...I want to split at the lowercase "at" not at the "AT" within the team's name. If I then try "\w at" it kinda works but then it trims the last character of the first line..like this:

TEAMHAT (missing the digit "1" in the team's name)
TEAMHAT2

I know I'm missing something simple...not so familiar with regular expressions patters but my logic would be to:

Look at the line...find "at" or "vs"...if found...split it into a new line and process the next line

$arr = @()
 
$lines = get-content c:\teams.txt
 
foreach ($line in $lines){
 
	$words = $line -split "at"
 
	foreach ($word in $words){
 
		$arr += $word.trim()
	}
}

$arr

Open in new window

0
Comment
Question by:bndit
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 37015856
Why don't you try splitting it from the spaces:

$arr = gc c:\teams.txt | %{$_.split(" ")[0,2]}


HTH,

Dale Harris
0
 
LVL 2

Author Comment

by:bndit
ID: 37015862
Because some of the team names have spaces....such as "TEAMHAT1 St." ...."TEAMHAT2 WEST"....
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 37015873
Well that throws a loop into things doesn't it.  Do you mind putting down all the possible combinations?  I'm sure I'll come up with something.

DH
0
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
LVL 2

Author Comment

by:bndit
ID: 37015878
TROY at FLA INTERNATIONAL
CONNECTICUT at PITTSBURGH
RICE at HOUSTON
VIRGINIA at MIAMI
BYU vs TCU
S CAROLINA ST at HOWARD
BOWLING GREEN at KENT ST
VMI at THE CITADEL
ALBANY at WAGNER
WYOMING at SAN DIEGO ST
IOWA at MINNESOTA
WAKE FOREST at N CAROLINA
MICHIGAN ST at NEBRASKA
ILLINOIS at PENN ST
SACRED HEART at ROBERT MORRIS
W VIRGINIA at RUTGERS
BALL ST at W MICHIGAN
MISSISSIPPI ST at KENTUCKY
NORTHWESTERN at INDIANA
SYRACUSE at LOUISVILLE
UAB at MARSHALL
BOSTON COLLEGE at MARYLAND
PURDUE at MICHIGAN
VIRGINIA TECH at DUKE
NC STATE at FLORIDA ST
CLEMSON at GEORGIA TECH
C MICHIGAN at AKRON
OLE MISS at AUBURN
ARKANSAS at VANDERBILT
KANSAS at TEXAS
YALE at COLUMBIA
FURMAN at CHATTANOOGA
HAMPTON at SAVANNAH ST
AIR FORCE at NEW MEXICO
DAYTON at VALPARAISO
S DAKOTA ST at MISSOURI ST
MCNEESE ST at SF AUSTIN
WEBER ST at MONTANA
NAVY at NOTRE DAME
MISSOURI at TEXAS A&M
IOWA ST at TEXAS TECH
SMU at TULSA
TULANE at E CAROLINA
BUFFALO at MIAMI OHIO
W KENTUCKY at LA MONROE
FORDHAM at ARMY
GEORGIA vs FLORIDA
ALABAMA A&M at ALABAMA ST
COLORADO at ARIZONA ST
TENNESSEE TECH at JACKSONVILLE ST
MEMPHIS at UCF
SAN JOSE ST at LOUISIANA TECH
PORTLAND ST at E WASHINGTON
HAWAII at IDAHO
BUCKNELL at LAFAYETTE
DELAWARE at TOWSON
COLORADO ST at UNLV
OREGON ST at UTAH
CALIFORNIA at UCLA
ARIZONA at WASHINGTON
S CAROLINA at TENNESSEE
BAYLOR at OKLAHOMA ST
WASHINGTON ST at OREGON
OKLAHOMA at KANSAS ST
NORTH TEXAS at ARKANSAS ST
LA LAFAYETTE at MIDDLE TENN ST
WISCONSIN at OHIO ST
NEVADA at NEW MEXICO ST
SOUTHERN MISS at UTEP
STANFORD at USC
ALCORN ST at SOUTHERN
COASTAL CAROLINA at STONY BROOK
N CAROLINA A&T at NORFOLK ST
RHODE ISLAND at NEW HAMPSHIRE
IDAHO ST at MONTANA ST
E KENTUCKY at MURRAY ST
W CAROLINA at SAMFORD
CORNELL at PRINCETON
MASSACHUSETTS at RICHMOND
MAINE at VILLANOVA
ELON at WOFFORD
W ILLINOIS at YOUNGSTOWN ST
PRESBYTERIAN at LIBERTY
DRAKE at MARIST
MORGAN ST at DELAWARE ST
DARTMOUTH at HARVARD
GEORGETOWN at HOLY CROSS
LEHIGH at COLGATE
E ILLINOIS at AUSTIN PEAY
PENNSYLVANIA at BROWN
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 37016108
Try the following...
$arr = @()
 
$lines = get-content c:\teams.txt
 
foreach ($line in $lines){
 	if ($line.Contains(" at ")){
	$words = $line -split " at "
 	}
	elseif ($line.Contains(" vs ")){
	$words = $line -split " vs "
	}
	foreach ($word in $words){
 
		$arr += $word.trim()
	}
}

$arr

Open in new window

0
 
LVL 2

Author Comment

by:bndit
ID: 37016130
thx! that was simple....I was stuck trying to come up with a fancy regex filter to find the "at" and "vs" but by using your suggestion it splits at the correct spots...great!
0
 
LVL 2

Author Closing Comment

by:bndit
ID: 37016131
Thanks, simple.
0
 
LVL 72

Expert Comment

by:Qlemo
ID: 37016369
Just for the files: The accepted solution is overcomplicated, and not very PowerShellish.
   get-content c:\teams.txt -split " at | vs "
is all you need.
0
 
LVL 2

Author Comment

by:bndit
ID: 37019558
@Qlemo....tried your way, but it's not working
split.png
0
 
LVL 72

Expert Comment

by:Qlemo
ID: 37019694
Sorry, need to enclose get-content in parens (I was testing with a list of string literals instead of a file):
   (get-content c:\teams.txt) -split " at | vs "

Open in new window

0
 
LVL 2

Author Comment

by:bndit
ID: 37019721
ok, that did the trick...very elegant solution....thx
0

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question