Page 1 of 2
Fixed Width Text file
Posted: 13 Aug 2015 02:55
by zagix
Hi,
I have an Tab delimited Text file and would like to reset it to fixed width padding spaces where required.
Existing tab text file
11(tab)626350837(tab)6263506492(tab)ID(tab)Friends Limousine Service(tab)111 Glenn Way #-2(tab)Lamar(tab)Ana(tab)y(tab)Racheal
Required fixed width text file
11626350837....6263506492.........ID..Friends Limousine Service.............111 Glenn Way #-2..................Lamar........Ana.............y.Racheal...(white space of 10)
2,15,19,4,38,35,13,15,2,10 (if no data then also needs white space of 10)
Data starts from line 2 as first line is the header.
Re: Fixed Width Text file
Posted: 13 Aug 2015 06:40
by Aacini
Why do you delete the first tab in your output example?
Try this:
Antonio
Re: Fixed Width Text file
Posted: 13 Aug 2015 06:43
by Squashman
Does each tab delimited field need to be a certain length. In other words you may need to pad X amount of spaces to the end of each field?
Re: Fixed Width Text file
Posted: 13 Aug 2015 09:45
by zagix
Hi,
Thanks Squashman for the reply.
Does each tab delimited field need to be a certain length.
Yes 2,15,19,4,38,35,13,15,2,10 (if no data then also needs white space of 10)
Before
11(tab)626350837(tab)6263506492(tab)ID(tab)Friends Limousine Service(tab)111 Glenn Way #-2(tab)Lamar(tab)Ana(tab)y(tab)Racheal
After
11626350837....6263506492.........ID..Friends Limousine Service.............111 Glenn Way #-2..................Lamar........Ana.............y.Racheal...(white space of 10)
Like in Before starts with 2 numeric then no need of x pad space
field 2 has 9 digits but in after file 9 digits + 6 space completes 15
filed 3 has 10 digits but in after file 10 + 9 space completes 19
...
Data starts from line 2 as first line is the header.
Re: Fixed Width Text file
Posted: 13 Aug 2015 10:28
by Aacini
You should try to be clearer in your descriptions! Try this:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "letter=abcdefghij"
set "tokens="
set maxSpaces=0
set i=0
for %%a in (2,15,19,4,38,35,13,15,2,10) do (
for %%i in (!i!) do set "tokens=!tokens!"%%!letter:~%%i,1!" "
set /A i+=1
set "tab[!i!]=%%a"
if %%a gtr !maxSpaces! set "maxSpaces=%%a"
)
set "spaces="
for /L %%i in (1,1,%maxSpaces%) do set "spaces= !spaces!"
rem Insert a TAB char here: -----v
for /F "skip=1 tokens=1-10 delims= " %%a in (test.txt) do (
set "output="
set i=0
for %%A in (%tokens%) do (
set "token=%%~A%spaces%"
set /A i+=1
for %%I in (!i!) do for %%N in (!tab[%%I]!) do (
set "output=!output!!token:~0,%%N!"
)
)
echo !output!
)
In the second line below:
Code: Select all
rem Insert a TAB char here: -----v
for /F "skip=1 tokens=1-10 delims= " %%a in (test.txt) do (
you must delete the three spaces after "delims= " and insert a TAB character using Notepad Editor (or any other editor).
Antonio
Re: Fixed Width Text file
Posted: 13 Aug 2015 20:12
by Squashman
Re: Fixed Width Text file
Posted: 14 Aug 2015 03:39
by Yury
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "in=in.txt"
set "out=out.txt"
set "template=2 15 19 4 38 35 13 15 2 10"
for /f "delims=" %%i in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo.0x09"') do set TAB=%%i
for %%i in (%template%) do set/a m+=1& set !m!=%%i
>"%out%" (
<"%in%" set/p line=& echo.!line!
for /f "usebackq skip=1 delims=" %%i in ("%in%") do (
set line=%%i& for %%j in ("!line:%TAB%=" "!") do (
set/a n+=1& for %%k in (!n!) do for /l %%l in (1 1 !%%k!) do set SPs= !SPs!
<nul set/p=%%~j!SPs!& set SPs=
)
echo.& set n=
)
)
endlocal
exit/b
Re: Fixed Width Text file
Posted: 14 Aug 2015 06:29
by foxidrive
I tested Aacini and Yury solutions with this data (tabs in the blank spots) - results were not what I expected.
Code: Select all
11 626350837 6263506492 ID Friends Limousine Service 111 Glenn Way #-2 Lamar Ana y Racheal
Re: Fixed Width Text file
Posted: 14 Aug 2015 10:09
by Compo
I normally use a powershell script/function for this type of thing.
Code: Select all
param([UInt32] $TabWidth = 8)
process {
$line = $_
while ( $TRUE ) {
$i = $line.IndexOf([Char] 9)
if ( $i -eq -1 ) { break }
if ( $TabWidth -gt 0 ) {
$pad = " " * ($TabWidth - ($i % $TabWidth))
} else {
$pad = ""
}
$line = $line -replace "^([^\t]{$i})\t(.*)$",
"`$1$pad`$2"
}
$line
}
Whilst the above doesn't add the spaces to the line ends, it should convert all tabs to spaces whilst maintaining alignment.
To convert using a tab size of 8 spaces (default) you don't need to add a parameter:
Code: Select all
GC DataIn.txt | .\TabXpand.ps1 | Out-File -encoding ASCII DataOut.txt
To convert using a tab size of 4 spaces just use four as the parameter:
Code: Select all
GC DataIn.txt | .\TabXpand.ps1 4 | Out-File -encoding ASCII DataOut.txt
If your input file is unicode you could of course remove the -encoding ASCII part too.Before everyone complains that this isn't a powershell forum, I think the same option is available directly in cmd.exe by using the
more command.
Example with tabs of 4 spaces
Re: Fixed Width Text file
Posted: 14 Aug 2015 11:01
by Squashman
Compo wrote:Before everyone complains that this isn't a powershell forum,
Not at all. I think we all are better off learning a little bit of Powershell, but your examples do not solve the users problem as they want a specific amount of space for each tab delimited field.
Re: Fixed Width Text file
Posted: 14 Aug 2015 11:27
by Aacini
foxidrive wrote:I tested Aacini and Yury solutions with this data (tabs in the blank spots) - results were not what I expected.
Code: Select all
11 626350837 6263506492 ID Friends Limousine Service 111 Glenn Way #-2 Lamar Ana y Racheal
It works here!
Code: Select all
The original data:
11(tab)626350837(tab)6263506492(tab)ID(tab)Friends Limousine Service(tab)111 Glenn Way #-2(tab)Lamar(tab)Ana(tab)y(tab)Racheal
After changed "(tab)" by an Ascii TAB (that will show different when posted here):
11 626350837 6263506492 ID Friends Limousine Service 111 Glenn Way #-2 Lamar Ana y Racheal
The output from my solution:
11626350837 6263506492 ID Friends Limousine Service 111 Glenn Way #-2 Lamar Ana y Racheal
|/\_____________/\_________________/\__/\____________________________________/\_________________________________/\___________/\_____________/|/\________/
2 15 19 4 38 35 13 15 2 10
Did you inserted the TAB in the specified line?
Antonio
PS - Where is the OP?
Re: Fixed Width Text file
Posted: 14 Aug 2015 13:19
by Aacini
A Batch-JScript hybrid script solution!
EDIT: Small bug fixed (didn't separate lines!)
Code: Select all
@set @Batch=1 /*
@echo off
setlocal EnableDelayedExpansion
set "regExp="
set "retVal="
set /A i=1, maxSpaces=0
for %%a in (2,15,19,4,38,35,13,15,2,10) do (
set "regExp=!regExp!\t(.*)"
set "retVal=!retVal!+($!i!+spaces).substr(0,%%a)"
if %%a gtr !maxSpaces! set "maxSpaces=%%a"
set /A i+=1
)
set "regExp=/^%regExp:~2%$/gm"
set "retVal=%retVal%+'\r\n'"
CScript //nologo //E:JScript "%~F0" "%regExp%" "%retVal:~1%" %maxSpaces% < test.txt
goto :EOF */
// JScript section
var arg = WScript.Arguments, regExp = arg(0), retVal = arg(1), maxSpaces = parseInt(arg(2)), spaces = "";
for ( var i = 1 ; i <= maxSpaces ; i++ ) spaces += " ";
WScript.Stdout.WriteLine(
WScript.Stdin.Readall().replace(
eval(regExp),
function ($0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10) {return (eval(retVal));}
)
);
This solution correctly manage the case when there is "no data", that is, when there are two TAB's together.
PS - I tried to create the function parameters in a variable and then define the function this way:
Code: Select all
function ( eval(funcParams) ) { return ...
but it marked an error. It seems that eval can not be used always; perhaps there are specific cases when certain declarations can not be performed inside an eval...
Antonio
Re: Fixed Width Text file
Posted: 14 Aug 2015 21:21
by foxidrive
Aacini wrote:The output from my solution:
11626350837 6263506492 ID Friends Limousine Service 111 Glenn Way #-2 Lamar Ana y Racheal
I did something wrong last night and the result isn't reproducible. I did replace the TAB but I can't figure
out what it was that I stuffed up...
Yuri, does your solution produce more spaces than required?
The first two terms are also meant to run together, right?
This creates the test file I used:
Code: Select all
@echo off
(
echo(-----BEGIN CERTIFICATE-----
echo(YWFhDQoxMQk2MjYzNTA4MzcJNjI2MzUwNjQ5MglJRAlGcmllbmRzIExpbW91c2lu
echo(ZSBTZXJ2aWNlCTExMSBHbGVubiBXYXkgIy0yCUxhbWFyCUFuYQl5CVJhY2hlYWwN
echo(Cg==
echo(-----END CERTIFICATE-----
)>"test.txt.decode"
certutil /decode /f "test.txt.decode" "test.txt"
del "test.txt.decode"
PS - Where is the OP?
Good question.
Re: Fixed Width Text file
Posted: 14 Aug 2015 23:33
by Yury
foxidrive wrote:Yuri, does your solution produce more spaces than required?
Oh yeah
!
This is the correct solution:
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "in=in.txt"
set "out=out.txt"
set "template=2 15 19 4 38 35 13 15 2 10"
for /f "delims=" %%i in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo.0x09"') do set TAB=%%i
for %%i in (%template%) do set/a m+=1& set !m!=%%i
>"%out%" (
<"%in%" set/p line=& echo.!line!
for /f "usebackq skip=1 delims=" %%i in ("%in%") do (
set line=%%i& for %%j in ("!line:%TAB%=" "!") do (
set/a n+=1& for %%k in (!n!) do (
for /f %%l in ('cmd/u/c echo.%%~j^| more^| findstr .^| find/c /v ""') do (
for /f %%m in ('set/a !%%k!-%%l') do (
for /l %%n in (1 1 %%m) do (
set SPs= !SPs!
)
)
)
)
<nul set/p=%%~j!SPs!& set SPs=
)
echo.& set n=
)
)
endlocal
exit/b
.
Re: Fixed Width Text file
Posted: 15 Aug 2015 00:55
by foxidrive
Aacini wrote:A Batch-JScript hybrid script solution!
Antonio, there seems to be no padding after "Rachael"
Yury wrote:This is the correct solution:
It seems good. The OP will have to confirm them both.