Check last word of each line

March 9, 2009 at 15:36:19
Specs: Win XP, Pent 4 / 1 GB
I need to parse through a text file and check/change the last word of each line if it meets the criteria (a backslash, random text and then the end of the line).

Here is what I have based on some other checks I am using. The problem I am having is that with the other check/changes I know the text to check but with this one the text is random [but it will always be preceeded with a backslash and be the last word in the line].

FOR /F "tokens=* delims=\^&" %%i in (newtext2.txt) do (
if "%%i"=="^<b^>%%i^</b^>" (
set newline=%%i
set newline=^<b^>%%i^</b^>
echo !newline! >> newtext2.txt

Jesus is the Rock - ROCK ON!

See More: Check last word of each line

Report •

March 9, 2009 at 16:20:51
What text do you want to change to?

If at first you don't succeed, you're about average.


Report •

March 9, 2009 at 22:54:00
I'm sure there is a better way, but the below should get your "last word" and should tolerate the usual (<,>,|, & !) special characters without modification.

It's a bit over complicated......

for /f "usebackq delims=" %%a in ("file name.txt") do (
set lword="%%a"
for /f "tokens=1* delims=\" %%b in ("!lword!") do (
if "%%c"=="" (
set lword=%%b
) else (
set lword="%%c
call :recur kill
if "%~1"=="kill" exit /b
set lword=!lword:~1,-1!
echo !lword!
echo "%%a"

In the section with the echo's you should have you word, !lword!, and whole line, %%a, iteration by iteration.

Report •

March 10, 2009 at 13:59:25
Mechanix2Go to answer your question I want to take the text and wrap it in some HTML but since it is multiple lines of random text this isn't as easy as it sounds. ;-)

I am still working on it and am getting very close. Now I have another problem that is related to the first. I am new to batch files and I need to know if there is an expression for 'does not equal' that can be applied to an empty string [I need to check if one of my variables doesn't contain any text].

For example, in JavaScript I would say:

if (somevariable!=""){
do this
do that

How would I do that in a batch file?

Jesus is the Rock - ROCK ON!

Report •

Related Solutions

March 12, 2009 at 00:44:25
Provided the variable contains nothing:

if not defined var (

Just don't add in % or !(if applicable) to the variable name as the check would then be performed on it's contents.

Another way to do the check is shown below. The below method can cause problems if the variable is non-dynamic and contains double quotes.

if not "%var%"=="" (

There is also:

if x neq x (

But is primarily for number comparison.

Report •

March 16, 2009 at 16:11:08
Okay, this was turning out to be much more difficult than I thought. I wanted to possibly add some html markup to the last word of a line possibly with some additional markup following [for line breaks and the such].

I dropped trying to format the last word and went with only adding a line break at the end of each line using the code below.

:: ---add a line break at the end of each line---
for /f "Delims=" %%a in (newtext4.txt) do (
echo ^  ^ %%a^<br^> >> %file%

Thanks to all who tried to help me with this. Your help was very much appreciated.

Jesus is the Rock - ROCK ON!

Report •

Ask Question