Start the writing in a text file from a specific column
Moderator: DosItHelp
-
- Posts: 19
- Joined: 01 Mar 2013 11:44
Start the writing in a text file from a specific column
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
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
If you mean you want to remove a certain number of characters from each line try something like that
Regards
aGerman
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
-
- Posts: 19
- Joined: 01 Mar 2013 11:44
Re: Start the writing in a text file from a specific column
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
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
Just prepend the spaces to the outputted text.
Regards
aGerman
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
-
- Posts: 19
- Joined: 01 Mar 2013 11:44
Re: Start the writing in a text file from a specific column
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
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
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
Seems you want to adjust the first part of the line to a fixed length.
Regards
aGerman
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
-
- Posts: 19
- Joined: 01 Mar 2013 11:44
Re: Start the writing in a text file from a specific column
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
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
-
- 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
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
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
Yep. It should be thought of as a character offset.