使用批处理脚本格式化文本文件

时间:2012-12-18 08:49:43

标签: windows batch-file dos

我有一个包含以下内容的文件。

http://server.in.oracle.com:18080/svn/SVN_DEMO/branches/CI_TEST/AM/SQL/ampks_amdtronl_kernel.sql
http://server.in.oracle.com:18080/svn/SVN_DEMO/branches/CI_TEST/AM/SQL/ampks_amdtronl_utils.sql
http://server.in.oracle.com:18080/svn/SVN_DEMO/branches/CI_TEST/AM/SQL/ampks_fundupload.sql
http://server.in.oracle.com:18080/svn/SVN_DEMO/branches/CI_TEST/AM/SQL/ampks_amdtronl_main.sql
http://server.in.oracle.com:18080/svn/SVN_DEMO/branches/CI_TEST/AM/SQL/ampks_validate.sql

我需要一个批处理脚本来格式化文件,使其看起来如下所示。

ampks_amdtronl_kernel.sql:AM/SQL/ampks_amdtronl_kernel.sql
ampks_amdtronl_utils.sql:AM/SQL/ampks_amdtronl_utils.sql
ampks_fundupload.sql:AM/SQL/ampks_fundupload.sql
ampks_amdtronl_main.sql:AM/SQL/ampks_amdtronl_main.sql
ampks_validate.sql:AM/SQL/ampks_validate.sql

这就是文件名:路径/从/ AM /到/ filname

请指导。

5 个答案:

答案 0 :(得分:1)

for /f %%a in (file.txt) do echo %%~nxa:AM/SQL/%%~nxa >>temp.txt
del file.txt /f /q
ren temp.txt file.txt

答案 1 :(得分:0)

在您选择的循环中: 1.将线分成两个

suffix=${line##.*/AM}   # this deletes everything before /AM so you have the suffix
fname=`basename $line`  # this gives you the name of the file
  1. 按照您的意愿重新组装

    echo“$ {fname}:$ {sufffix}”

  2. 或者,您可以使用一行sed

    来完成此操作

答案 2 :(得分:0)

您可以使用awk来完成此任务。 或者使用简单的文本编辑器将http://server.in.oracle.com:18080/svn/SVN_DEMO/branches/CI_TEST替换为空字符串。 然后,我们可以将文件导入Excel,将字符串分成'/'列 最后,使用连接函数(在Excel中)来构建字符串。

我更喜欢awk,你需要使用分割功能

  

awk'{split($ 0,a,“/”);打印[10],':',a [6],'/',a [7],'/',a [8],'/',a [9],'/',a [10] }'`

答案 3 :(得分:0)

PowerShell救援:

param ([string] $inFileName, [string] $outFileName)

function Reformat-String
{
    param (
        [string] $str
    )

    $split = $str.Split("/")

    return $split[9] + ":" + $split[7] + "/" + $split[8] + "/" + $split[9]
}

$file = Get-Content $inFileName

foreach($line in $file)
{
    $newLine = Reformat-String $line
    $newLine | Out-File $outFileName -Append
}

保存为“reformat.ps1”。调用。\ reformat.ps1 SourceFilePath DestinationFilePath

答案 4 :(得分:0)

@echo off
For /F "tokens=7-9 delims=/" %%a in (thefile.txt) do echo %%c:%%a/%%b/%%c