user1797693
user1797693

Reputation:

Formating a text files using batch script

I have a file with the following content.

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

I need a batch script to format the file in such a way that it will look like the following.

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

Thats is filename:Path/from/AM/till/filname

Kindly guide.

Upvotes: 0

Views: 2695

Answers (4)

Bali C
Bali C

Reputation: 31251

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

Upvotes: 1

David Brabant
David Brabant

Reputation: 43589

PowerShell to the rescue:

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
}

Saved as "reformat.ps1". Invoke as .\reformat.ps1 SourceFilePath DestinationFilePath

Upvotes: 0

user1666959
user1666959

Reputation: 1855

In a loop of your choice: 1. Split the line into two

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. reassemble the pieces as you wish with

    echo "${fname}:${sufffix}"

Alternatively, you can do it with a single line of sed.

Upvotes: 0

Khalefa
Khalefa

Reputation: 2304

You may use awk to accomplish this. Or use simple text editor to replace http://server.in.oracle.com:18080/svn/SVN_DEMO/branches/CI_TEST with empty string. Then, we can import the file to Excel to divide the string into columns on '/' Finally, use concatenate function (in Excel) to build your string.

I would prefer awk, you need to use split function

awk '{split($0,a,"/"); print a[10],':',a[6],'/',a[7],'/',a[8],'/',a[9],'/',a[10]}'`

Upvotes: 0

Related Questions