简体   繁体   中英

Powershell script to replace only the tab characters in substrings enclosed in double quotes

I'm working with a tab separated values (tsv) text file. I'm importing it into Excel but for some reason it is also treating tab chars in strings (denoted with double quotes) as delimiters.

A line in the file looks like this:

Name  Quote  Random text
Harry  "I like putting    tabs in sentences"  "Here is another tab   "

I'm trying to write a powershell script to read the whole text file, find only tab chars enclosed in quotes and replace them with a space character. I know this will be possible but the issue is I'm not an expert in Powershell or regex :D

Any ideas?

Try this -

(Get-Content \\PathToYourTextFile\TabFile.txt).Replace("`t", " ")

OR

Get-Content \\PathToYourTextFile\TabFile.txt | % {$_ -replace ("`t", " ")}

Both will replace the tabs in your text file to single spaces.

If you want to completely replace tabs with spaces, use solution provided by Vivek Kumar Singh .

However, if you want to keep it as TSV you can import it to variable, replace tabs with space (in values only) and then export:

$content = Import-Csv .\file.tsv -Delimiter "`t"
foreach ($line in $content) {
    $line.Name = $line.Name.replace("`t", " ")
    $line.Quote = $line.Quote.replace("`t", " ")
    $line.'Random text' = $line.'Random text'.replace("`t", " ")
}
$content | Export-Csv .\output.tsv -Delimiter "`t" -NoTypeInformation

By the way, this might not be needed as I for me Excel imports data correctly. Maybe some issue with the file/particular line?

在替换命令中使用引号和水平制表符的十六进制值

(gc "\\Path to Your\file") -replace "[\x22][\x09][\x22]"," " | set-content "\\Path to Your\file"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM