Computing.Net > Forums > Programming > Check last word of each line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Check last word of each line

Reply to Message Icon

Name: onecrazycoder
Date: March 9, 2009 at 15:36:19 Pacific
OS: Win XP
CPU/Ram: Pent 4 / 1 GB
Subcategory: Batch
Comment:

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
)else(
set newline=^<b^>%%i^</b^>
)
echo !newline! >> newtext2.txt
)

Jesus is the Rock - ROCK ON!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: March 9, 2009 at 16:20:51 Pacific
Reply:

What text do you want to change to?


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

M2


0

Response Number 2
Name: Judago
Date: March 9, 2009 at 22:54:00 Pacific
Reply:

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......

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

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


0

Response Number 3
Name: onecrazycoder
Date: March 10, 2009 at 13:59:25 Pacific
Reply:

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
}else{
do that
}

How would I do that in a batch file?

Jesus is the Rock - ROCK ON!


0

Response Number 4
Name: Judago
Date: March 12, 2009 at 00:44:25 Pacific
Reply:

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.


0

Response Number 5
Name: onecrazycoder
Date: March 16, 2009 at 16:11:08 Pacific
Reply:

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!


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Check last word of each line

.bat check size of each file in dir www.computing.net/answers/programming/bat-check-size-of-each-file-in-dir/14485.html

Remove last 2 characters from each line.... www.computing.net/answers/programming/remove-last-2-characters-from-each-line/20257.html

Batch to append text to each line of a txt www.computing.net/answers/programming/batch-to-append-text-to-each-line-of-a-txt/18957.html