Start the writing in a text file from a specific column

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
autonicknan
Posts: 19
Joined: 01 Mar 2013 11:44

Start the writing in a text file from a specific column

#1 Post by autonicknan » 20 Sep 2014 07:39

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 20 Sep 2014 08:07

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

autonicknan
Posts: 19
Joined: 01 Mar 2013 11:44

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

#3 Post by autonicknan » 20 Sep 2014 08:19

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 20 Sep 2014 08:28

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

autonicknan
Posts: 19
Joined: 01 Mar 2013 11:44

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

#5 Post by autonicknan » 20 Sep 2014 08:39

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#6 Post by Squashman » 20 Sep 2014 08:54

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#7 Post by aGerman » 20 Sep 2014 08:57

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

autonicknan
Posts: 19
Joined: 01 Mar 2013 11:44

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

#8 Post by autonicknan » 20 Sep 2014 09:27

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#9 Post by ShadowThief » 20 Sep 2014 10:12

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#10 Post by foxidrive » 20 Sep 2014 14:17

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#11 Post by Squashman » 20 Sep 2014 15:42

Yep. It should be thought of as a character offset.

Post Reply