Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
Does anyone know how, in a command prompt, to remove the last character in each line of a .txt file?
I need to open a family table instance, do one operation, close that instance then open the next instance and do the same thing. And so on.
I am combining a mapkey with a batch file and I am close. But I have to remove the last character in each line and I can't figure out how.
Solved! Go to Solution.
I saved each page out as an .mht file; hopefully you can read them.
Steve,
you can do following (for example):
1.] install sed (stream editor) ... available at sed for Windows
2.] use command prompt command ... sed "s/.$//" a.txt > b.txt
a.txt ... source file
b.txt ... removed last character in each line
MH
You don't have to install external sfotware. You might be able to do this with a batch, but vbs has a little more utility. In your config.pro, just change the .bat to .vbs and use the code below (with modifications as required)
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
strTemp = "c:\test\temp.txt"
Set objFile = objFS.OpenTextFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
' Remove last character from strLine
'This removes the carriage return and the last character and then adds the carriage return back
strFile = Left(strFile, Len(strLine) - 2) & vbCrLf
objOutFile.Write(strLine)
Loop
objOutFile.Close
objFile.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile
Martin,
should have mentioned I can't install external software not approved by out IT dept. But thank you for it.
Chris,
Thank you for the suggestion. I might try that but I'd prefer straight command line commands.
Here's are some links with suggestions that may help:
Delete last 2 characters from every line in a text file - Stack Overflow
for loop - Use Batch File Processing To Remove Last Character - Stack Overflow
Unfortunately I do not have access to some sites from work. Stack Overflow is one of them.
I saved each page out as an .mht file; hopefully you can read them.
That's awesome. Thank you for uploading the pages.
Unforturnately, it doesn't look like it will work. The for command doesn't like the tilde character. I can do it outside the for command but that won't help. And using what they suggested doesn't output the line.
I DID IT!
I tried many different variations of what was on those pages. But I never tried the example where they used the CALL command. This is what worked. Or at least seems to have worked. I'l know once I get the mapkey and batch file all set.
Thank you so much.
SETLOCAL
for /f "delims=" %%a in (abc.pro) do (
set str=%%a
CALL :remove "%str%"
)
goto EOF
:remove
set str=%str:~0,-3%
echo %str% >> def.pro
:EOF
And just a little more information.
I also got a space at the end with this code. It's easy to remove that actually. I did some searching and found the answer.
Remove the spaces between the ">>" in the line echo %str% >> def.pro. That space adds the space at the end of the line in def.pro.
So now it's:
echo %str%>>def.pro