I have one file and would like to do search and replace – as shown
My_name group_1 \ (TAB)(SPACE)
(TAB)(SPACE) -class { student_1 }
My_name group_2 \ (TAB)(SPACE)
(TAB)(SPACE) -class { student_1 }
My_name group_3 \ (TAB)(SPACE)
(TAB)(SPACE) -class { student_3 }
Please note that – before ‘-class’ there is TAB and SPACE
I am using vi. From above strings would like to do something like this –
My_name group_1 -class { student_1 }
My_name group_2 -class { student_1 }
My_name group_3 -class { student_3 }
I tried search and replace with :%s, selected the entire file and did - :s/..
But no success. Can you help me?
First replace all tab with a single space using the below command:
1,$s/\t/ /g
Next, search for two spaces followed by a newline and remove them.
1,$s/ \n//g
If there are any more trailing spaces, you can remove them by :
s/ \{2,}/ /g
The last command would replace 2 or more spaces with a single space.
Related
I need to pull first and last names from accounts that were very poorly named from the previous IT administrator and recreate them in the same source OU.
There are characters in the account name I that I cant seem to get past. I need to pull First,Last and the remaining DN so I can recreate the account in the same OU structure without. For testing I'm only using one user in one OU, but there will be many different OU structures. "\" and "(Vendor)" are causing
My end goal is to feed the script a list of Accounts DN and recreate the account without the undesired "\" and "(vendor)" and do first.last or some variation of that.
$source = "CN=last\, first (Vendor),OU=vendors,DC=somecompany,DC=com"
$arg = "(vendor)"
($source -split ',*..=')[1].Remove($arg)
I've also tried
($source -split ',*..=')[1] -replace "`(Vendor)'", ""
The brackets and \ seem to cause everything I've tried to fail. I've searched and there doesnt seem to be a simple escape char like ^ for Batch files.
The backtick is the escape character usually for just powershell code. In the regex though, you will want to double your backslashes.
$source = "CN=last\, first (Vendor),OU=vendors,DC=somecompany,DC=com","CN=last\, first (Vendor),OU=vendors,DC=somecompany,DC=com"
$r = ($source[0] -split ',*..=|\\,').Replace("(Vendor)","").Where({ $_ -ne ''})
$r
For .remove() the argument is supposed to be a number, the number or characters to display of a string (and removing every character after the number of characters you give specify). Feeding it a string when it's expecting an int will just error so I used .replace() instead.
.remove() takes a number as an argument:
($source -split ',*..=')[1].Remove
OverloadDefinitions
-------------------
string Remove(int startIndex, int count)
string Remove(int startIndex)
Using backslashes to escape regex characters:
($source -split ',*..=')[1] -replace "\(Vendor\)", ""
last\, first
Confirmed by
[regex]::escape("(Vendor)")
\(Vendor\)
Thanks for all of the help.
$source = "CN=Doe\, John (Vendor),OU=vendors,DC=somecompany,DC=com"
$r = ($source -split ',*..=|\\,').Replace("(Vendor)","").Where({ $_ -ne ''})
$first = $r[1].replace(" ","")
$last = $r[0].replace(" ","")
$DN = ($source -split "OU=",2)[1]
$DN = "OU=" + $DN
$first
$last
$DN
Have a CSV file with multiple columns with information. Need to remove the opening and closing " in the Employee Name as well as the , as seen below.
Employee Name,Employee #,column3, column4 etc. <br>
"Lastname, Firstname",123,abc,xyz<br>
"Lastname, Firstname",123,abc,xyz<br>
Result:
Employee Name,Employee #,column3, column4 etc.<br>
Lastname Firstname,123,abc,xyz<br>
Lastname Firstname,123,abc,xyz<br>
Tried using the following Powershell script:
(gc C:\pathtocsv.csv) | % {$_ -replace '"', ""} | out-file C:\pathtocsv.csv -Fo -En ascii
This only removes the " " around Lastname , Firstname but the comma is still present when opening the csv file in a text editor. Need this format to send to data to another company. Everything I have tried removes every comma. Novice in powershell and other languages, I am sure this is an easy fix. Please help!
Powershell has a lot of built-in handling for CSV files, instead of trying to treat is as a text file you can use the following to remove just the comma you want:
Import-Csv .\a.csv | % {
$_."Employee Name" = ($_."Employee Name" -replace ',','')
$_ #return modified rows
} | Export-Csv .\b.csv -notype -delim ','
this will by default export everything with double quotes, so you may need to go back and run something like:
(gc .\b.csv -raw) -replace '"','' | Out-File .\c.csv
to also remove all the double quotes.
Warning: quotes are important if text contains special characters (i.e. comma, quote)
If you really want to strip lines, you can process your csv as regular text file:
#sample data
#'
"Lastname, Firstname",123,abc,xyz
"Lastname, Firstname",123,abc,xyz
'# | out-file c:\temp\test.csv
Get-Content c:\temp\test.csv | % {
$match = [Regex]::Match($_,'"([^,]*), ([^"]*)"(.*)')
if ($match.Success) {
$match.Groups[1].Value+' '+$match.Groups[2].Value+$match.Groups[3].Value
} else {
$_ #skip processing if line format do not match pattern
}
}
Using Powershell version 3 & reading the contents of a file, which I then need to see if I have one of several strings that are contained in the file and replace them if they are. The issue in my case is that one of the strings I need to match on may have a variable amount of blank spaces in it (or none at all).
The string I'm matching on has double quotes in it, which is followed by a colon (:) then whitespace (or none) and then any number of statuses (can be either alpha or numeric) followed by a comma. For simplicity, I'm just using a number in the code below.
$txt = (Get-Content $file)
$oldstr = "`"status`": 1,"
$newstr = '`"status`": 0,"
if (($txt.Contains($old1)) -or ($txt.Contains($oldstr)) -or ($txt.Contains($old2))) {
$txt.Replace($oldstr, $newstr).Replace($old1, $new1).replace($old2, $new2)| Set-Content -Path $file
}
The problem I'm having is matching the $oldstr which may have none, one or more spaces between the colon and the status code, which in this example is a number but it may also be several different numbers or strings. The $newstr has no need to replicate the whitespace from the $oldstr. Also, in the above example, it is using one of three conditions in the Contains. The actual data may contain none, one, two, or all three of those strings.
How can you do the match/contains and the replace of the strings which can have whitespace in them?
Use a regular expression with the -replace operator:
PS C:\> '"status": 0' -replace '"status":\s*0','"status": 1'
"status": 1
PS C:\> '"status": 0' -replace '"status":\s*0','"status": 1'
"status": 1
PS C:\> '"status":0' -replace '"status":\s*0','"status": 1'
"status": 1
In the pattern I use above:
"status": just matches the literal string "status":
\s* matches 0 or more whitespace characters
0 matches a literal 0
Here is an interessant solution to having several match/replace pairs with a hashtable converted into a combined regex. But I didn't get a Regex into the hash key, so I do both the table and a RegEx to the $_ in the foreach.
# Build hashtable of search and replace values.
$file = ".\testfile.txt"
$replacements = #{
'something2' = 'somethingelse2'
'something3' = 'somethingelse3'
'morethings' = 'morethingelses'
'blabla' = 'blubbblubb'
}
# Join all keys from the hashtable into one regular expression.
[regex]$r = #($replacements.Keys | foreach { [regex]::Escape( $_ ) }) -join '|'
[scriptblock]$matchEval = { param( [Text.RegularExpressions.Match]$matchInfo )
# Return replacement value for each matched value.
$matchedValue = $matchInfo.Groups[0].Value
$replacements[$matchedValue]
}
$fileCont = Get-Content $file
# Perform replace over every line in the file and append to log.
$Newfile = $fileCont | ForEach {
$r.Replace( ( $_ -replace '"status":\s*0','"status": 1'), $matchEval )
}
$fileCont
"----"
$Newfile
Gives this output form my testfile.txt
> .\Replace-Array.ps1
"Status": 0, something2,morethings
"Status": 0, something3, blabla
----
"status": 1, somethingelse2,morethingelses
"status": 1, somethingelse3, blubbblubb
I'm using Powershell to trim spaces between strings, I need help. I'm reading the values into a variable using Get-Content
Here is my input data:
04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q
[ARMADA MUSIC]
12:37 Chakra - Home (Alexander Popov Remix) http://goo.gl/3janGY
[SOUNDPIERCING]
See the space between the two songs? I want to eliminate these. so that the output is:
04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q
[ARMADA MUSIC]
12:37 Chakra - Home (Alexander Popov Remix) http://goo.gl/3janGY
[SOUNDPIERCING]
I put the contents in a file called foo.txt.
foreach ($line in get-content foo.txt) {
if ($line -ne '') {
$line
}
}
$noEmptyLines = Get-Content -Path C:\FilePath\File.Txt | Where-Object{$_ -notmatch "^\s*$"}
You would have a variable where any lines that contained only whitespace would be removed.
What is the best way to remove all text in a string after a specific character? In my case "=" and after another character in my case a ,, but keep the text between?
Sample input
=keep this,
Another way to do this is with operator -replace.
$TestString = "test=keep this, but not this."
$NewString = $TestString -replace ".*=" -replace ",.*"
.*= means any number of characters up to and including an equals sign.
,.* means a comma followed by any number of characters.
Since you are basically deleting those two parts of the string, you don't have to specify an empty string with which to replace them. You can use multiple -replaces, but just remember that the order is left-to-right.
$a="some text =keep this,but not this"
$a.split('=')[1].split(',')[0]
returns
keep this
This should do what you want:
C:\PS> if ('=keep this,' -match '=([^,]*)') { $matches[1] }
keep this
This is really old, but I wanted to add my slight variation for anyone else who may stumble across this. Regular expressions are powerful things.
To keep the text which falls between the equal sign and the comma:
-replace "^.*?=(.*?),.*?$",'$1'
This regular expression starts at the beginning of the line, wipes all characters until the first equal sign, captures every character until the next comma, then wipes every character until the end of the line. It then replaces the entire line with the capture group (anything within the parentheses). It will match any line that contains at least one equal sign followed by at least one comma. It is similar to the suggestion by Trix, but unlike that suggestion, this will not match lines which only contain either an equal sign or a comma, it must have both in order.
I referenced #benjamin-hubbard 's answer above to parse the output of dnscmd for A records, and generate a PHP "dictionary"/key-value pairs of IPs and Hostnames. I strung multiple -replace args together to replace text with nothing or tab to format the data for the PHP file.
$DnsDataClean = $DnsData `
-match "^[a-zA-Z0-9].+\sA\s.+" `
-replace "172\.30\.","`$P." `
-replace "\[.*\] " `
-replace "\s[0-9]+\sA\s","`t"
$DnsDataTable = ( $DnsDataClean | `
ForEach-Object {
$HostName = ($_ -split "\t")[0] ;
$IpAddress = ($_ -split "\t")[1] ;
"`t`"$IpAddress`"`t=>`t'$HostName', `n" ;
} | sort ) + "`t`"`$P.255.255`"`t=>`t'None'"
"<?php
`$P = '10.213';
`$IpHostArr = [`n`n$DnsDataTable`n];
?>" | Out-File -Encoding ASCII -FilePath IpHostLookups.php
Get-Content IpHostLookups.php