Page 1 of 1

Start the writing in a text file from a specific column

Posted: 20 Sep 2014 07:39
by autonicknan
Hello,

Is there any command with which you can start writing from a specific column/position in a text file?
I couldn't find one so far...

Thanks
Nikos

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 08:07
by aGerman
If you mean you want to remove a certain number of characters from each line try something like that

Code: Select all

@echo off &setlocal

set "infile=1.txt"
set "outfile=2.txt"
set "n=5"

REM Write a file for testing
>"%infile%" (
  echo 1234567890
  echo foo
  echo(
  echo abcdefg
)

REM Display its content
echo "%infile%":
type "%infile%"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

REM Skip first n characters from each line and write linewise to another file
setlocal EnableDelayedExpansion
<"!infile!" >"!outfile!" (
  for /f %%i in ('type "!infile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" &set /p "line="
    if not defined line (
      echo(
    ) else (
      echo(!line:~%n%!
    )
  )
)
endlocal

REM Display its content
echo "%outfile%":
type "%outfile%"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pause

Regards
aGerman

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 08:19
by autonicknan
Hello aGerman,

Actually I want to write in an empty text file ,not at the beginning of each line but lets say at the 10th column:

Nikos --> At the beginning of the line
__________Nikos --> at the 10th column of the line(consider the underscores as spaces)


I just wonder if there any command with which I can access a specific column of a line or if I have to write my own FOR loop for this.

Thanks
Nikos

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 08:28
by aGerman
Just prepend the spaces to the outputted text.

Code: Select all

@echo off &setlocal

set "infile=1.txt"
set "outfile=2.txt"
set "spaces=          "

REM Write a file for testing
>"%infile%" (
  echo 1234567890
  echo foo
  echo(
  echo abcdefg
)

REM Display its content
echo "%infile%":
type "%infile%"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

REM Prepend 10 spaces to each line and write linewise to another file
setlocal EnableDelayedExpansion
<"!infile!" >"!outfile!" (
  for /f %%i in ('type "!infile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" &set /p "line="
    echo(!spaces!!line!
  )
)
endlocal

REM Display its content
echo "%outfile%":
type "%outfile%"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pause

Regards
aGerman

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 08:39
by autonicknan
Hi aGerman,

Its not that easy because the way the I need to use the "set position" in the text file is more complicated.

For your better understanding, I need my final output to be like below:

blabla__________renamed to nikos
blablabla________renamed to nikos1
bla_____________renamed to nikos2

What I actually need is the "renamed" word to begin from a certain position on each line

Thanks
Nikos

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 08:54
by Squashman
Might make it easier to understand your problem if you show us a real world example of your input and a real world example of your output and explain why you need to do this.

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 08:57
by aGerman
Seems you want to adjust the first part of the line to a fixed length.

Code: Select all

@echo off &setlocal
set "line1=123"
set "line2=abc def"

call :fixlength line1 10
call :fixlength line2 10

echo %line1%renamed to nikos
echo %line2%renamed to nikos

pause
exit /b


:fixlength ByRef_String  ByVal_NumberOfCharacters
setlocal EnableDelayedExpansion
set "string=!%~1!"
set "n=%~2"
set "spaces="
for /l %%i in (1 1 %n%) do set "spaces=!spaces! "
set "string=!string!!spaces!"
set "string=!string:~0,%n%!"
endlocal &set "%~1=%string%"
exit /b

Regards
aGerman

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 09:27
by autonicknan
Yes aGerman this was what i wanted :)

I just need one explanation regarding one line of your code:

set "string=!string:~0,%n%!"

Does this mean that the string will begin from the zero position up to nth?

Thanks

Regards,
Nikos

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 10:12
by ShadowThief
autonicknan wrote:Yes aGerman this was what i wanted :)

I just need one explanation regarding one line of your code:

set "string=!string:~0,%n%!"

Does this mean that the string will begin from the zero position up to nth?

Thanks

Regards,
Nikos

yep, that's exactly what it means

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 14:17
by foxidrive
autonicknan wrote:set "string=!string:~0,%n%!"

Does this mean that the string will begin from the zero position up to nth?


It actually means to: skip 0 characters and read n characters from the string.

Re: Start the writing in a text file from a specific column

Posted: 20 Sep 2014 15:42
by Squashman
Yep. It should be thought of as a character offset.