在Powershell脚本的csv /文本文件中将字母$替换为; $

时间:2019-07-12 11:57:35

标签: powershell

我在PowerShell中有代码,我需要将字母$替换为;$或插入;

CSV文件:

"11/23/2018 17:10:08"$"https://www.google.com"
"11/23/2018 17:10:20"$"https://www.yahoo.com"

我需要文件:

11/23/2018 17:10:08;$https://www.google.com
11/23/2018 17:10:20;$https://www.yahoo.com

PS:是否可以找到URL的标题?

(Get-Content C:\Users\user\Desktop\test\import.txt) | 
Foreach-Object {$_ -replace '"'} |
Foreach-Object {$_ -replace "$", ';$'} | 
Set-Content C:\Users\user\Desktop\test\export.txt

错误的结果是:

11/23/2018 17:03:46$https://www.seznam.cz;$

3 个答案:

答案 0 :(得分:5)

您需要对$进行转义,因为它在正则表达式(文本结尾)中具有特殊含义。

#!/bin/bash

echo "Do you want to Login?"
read ans
if [ "$ans" = "no" ]
then
    /sbin/ifconfig
    echo "Find your IPv4 and type it in"
    read ipr4
    clear
    echo "type in a password"
    read pwd
    touch $pwd &
    touch $ipr4 &
    read ip
else
    clear
    echo "type in your IPv4"
    read pwed
    clear

    if [ -f $pwed ]; then
        echo "Type in your password"
        read pass
        clear

        if [ -f $pass ]; then
            echo "Want to be a listener (yes/no)"
            read y
            if [ "$y" ==  "yes" ]
            then
                clear
                echo "Which port"
                read port
                clear
                echo "You are on port: $port"
                nc -l $port
            else
                clear
                echo "Which port do you want to chat on"
                read p2
                echo "What is the listeners IPv4"
                read ipi4
                clear
                echo "You are chatting to $ipi4 on Port: $p2"
                nc $ipi4 $p2
            fi
        fi
    else
        clear
        sleep 3
        echo "Login Failed"
    fi
fi

答案 1 :(得分:0)

这是单线的:

$inputFile  = 'C:\test.txt'
$outputFile = 'C:\test1.txt'

[System.IO.File]::ReadAllLines($inputFile) | % { $_.Replace( '"$"', ';$' ).Trim('"') } | Out-File $outputFile

答案 2 :(得分:-1)

您可以在一个命令中使用多个-replace,如下所示:

(Get-Content C:\Users\user\Desktop\test\import.txt) | 
Foreach-Object {$_ -replace '"' -replace '$', ';$'}|
Set-Content C:\Users\user\Desktop\test\export.txt
相关问题