Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can remove the line match before?

7 views
Skip to first unread message

Blue Fish

unread,
Sep 4, 2008, 1:35:03 AM9/4/08
to
Hello:

May I have idea how can I remove the line and one line above if the
char. was matched?

E.G.
Before:
Line 1:<tr>
Line 2:<td>BIOS Version</td><td>PTLTD - 6040000 PhoenixBIOS 4.0 Release
6.0 </td></tr>
Line 3:<tr>
Line 4:<td>User Name</td><td>user01</td></tr>
Line 5:<tr>
Line 6:<td>System Uptime</td><td>11 Days, 17 Hours, 55 Minutes</td></tr>
Line 7:<tr>
Line 8:<td>Local Time</td><td>2008-08-07 16:48:47</td></tr>

After: (If the line match "user name" then remove this line and the line
above.
So, Original Line 3 & Line 4 will be removed as below

Line 1:<tr>
Line 2:<td>BIOS Version</td><td>PTLTD - 6040000 PhoenixBIOS 4.0 Release
6.0 </td></tr>
Line 3:<tr>
Line 4:<td>System Uptime</td><td>11 Days, 17 Hours, 55 Minutes</td></tr>
Line 5:<tr>
Line 6:<td>Local Time</td><td>2008-08-07 16:48:47</td></tr>

The file sequence many not be the same patten or same line number.

Thanks a lot!

Pegasus (MVP)

unread,
Sep 4, 2008, 3:08:40 AM9/4/08
to

"Blue Fish" <blue...@isp.com> wrote in message
news:OHePLAlD...@TK2MSFTNGP03.phx.gbl...

I am a little confused by your post. Here is what I think you're trying to
say - please confirm:

- I have a text file of the form below.
- I wish to generate a copy of this text file.
- The copy should contain all lines from the original text file,
except the line that contains the string "User Name" and
the line immediately preceding it.
- How would I do this with a batch or a script file?

<tr>


<td>BIOS Version</td><td>PTLTD - 6040000 PhoenixBIOS 4.0 Release 6.0
</td></tr>

<tr>


<td>User Name</td><td>user01</td></tr>

<tr>


<td>System Uptime</td><td>11 Days, 17 Hours, 55 Minutes</td></tr>

<tr>

billious

unread,
Sep 4, 2008, 3:13:17 AM9/4/08
to

"Blue Fish" <blue...@isp.com> wrote in message
news:OHePLAlD...@TK2MSFTNGP03.phx.gbl...


This solution developed using XP
It may work for NT4/2K

----- batch begins -------
[1]@echo off
[2]del zout1.txt 2>nul
[3]setlocal
[4]set yst=0
[5]for /f "tokens=1*delims=[]" %%i in ( 'find /n "<td>" ^<zout.txt ') do set
yst=%%i
[6]if %yst%==0 echo string not found&goto :eof
[7]set /a yft=%yst% - 1
[8]for /f "tokens=1*delims=[]" %%i in ( 'find /v /n "" ^<zout.txt ') do if
not %%i==%yft% if not %%i==%yst% >>zout1.txt echo\%%j
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof

*Will NOT work properly for lines beginning with "[" or "]"

ZOUT.txt is the source file, ZOUT1.txt is the destination file; select the
string to find in [5] appropriately.


Blue Fish

unread,
Sep 4, 2008, 10:34:03 AM9/4/08
to
Hello:

Sorry for my English was not enough. Let me try to explain as much as I can.

I have a html file which is a text base.
The file contain like as follow:

*** parts of the html file ***
<tr>
<td>BIOS Version</td><td>PhoenixBIOS 4.0 Release 6.0 </td></tr>


<tr>
<td>User Name</td><td>user01</td></tr>
<tr>
<td>System Uptime</td><td>11 Days, 17 Hours, 55 Minutes</td></tr>
<tr>
<td>Local Time</td><td>2008-08-07 16:48:47</td></tr>

*** parts of the html file ***


It was a HTML code of <tr></tr> I need to remove it in pair. From the
about, I need to remove the line with "User Name". So, I need to remove
the line contain with "User Name" and together with the above line of
<tr> as it was a pair. Is it possible to do that?

Thanks!

Pegasus (MVP)

unread,
Sep 4, 2008, 10:59:59 AM9/4/08
to

"Blue Fish" <blue...@isp.com> wrote in message
news:%23NCLYtp...@TK2MSFTNGP06.phx.gbl...

Did you try the solution proposed by "billious"?


Timo Salmi

unread,
Sep 4, 2008, 2:44:35 PM9/4/08
to
Blue Fish <blue...@isp.com> wrote:
> May I have idea how can I remove the line and one line above if the
> char. was matched?

You might wish to study and run the following example code
@echo off & setlocal enableextensions
::
:: Make a test file
set testfile=C:\_M\MyTest.txt
for %%f in ("%testfile%") do if exist %%f del %%f
for /L %%i in (1,1,3) do echo This is row 0%%i>>"%testfile%"
echo.>>"%testfile%"
for /L %%i in (5,1,9) do echo This is row 0%%i>>"%testfile%"
rem type "%testfile%"
::
set targetString=row 07
::
:: Get the line number of the target string
for /f "tokens=1 delims=:" %%a in ('
findstr /n /c:"%targetString%" "%testfile%"') do (
set lineNber=%%a)
::
set /a n1=%lineNber%-2
sed -n 1,%n1%p "%testfile%"
set /a n2=%lineNber%+1
sed -n %n2%,$p "%testfile%"
::
for %%f in ("%testfile%") do if exist %%f del %%f
endlocal & goto :EOF

The test file is
This is row 01
This is row 02
This is row 03

This is row 05
This is row 06
This is row 07
This is row 08
This is row 09

The output is
This is row 01
This is row 02
This is row 03

This is row 05
This is row 08
This is row 09

The solution avoids the empty line catch common in pure batches.

For more information and alternatives see e.g.
23} How do I get the n'th, the first and the last line of a text file?
file:///C:/_G/WWW/~NETIKKA/SALMTI02/INFO/tscmd023.htm

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Timo Salmi

unread,
Sep 4, 2008, 3:03:20 PM9/4/08
to
Timo Salmi <t...@uwasa.fi> wrote:
> For more information and alternatives see e.g.
> 23} How do I get the n'th, the first and the last line of a text file?
> file:///C:/_G/WWW/~NETIKKA/SALMTI02/INFO/tscmd023.htm

http://www.netikka.net/tsneti/info/tscmd023.htm

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland

Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Blue Fish

unread,
Sep 5, 2008, 12:56:29 AM9/5/08
to
May I know where I can have the "sed" command?

Thanks!

Blue Fish

unread,
Sep 5, 2008, 12:59:11 AM9/5/08
to
Yes I have try but it didn't work. I didn't know where I can input the
target for searching the keyword. It was because beside of "User Name" I
also need to filter the others.

Thanks!

billious

unread,
Sep 5, 2008, 1:53:08 AM9/5/08
to

"Blue Fish" <blue...@isp.com> wrote in message
news:uvLQ1QxD...@TK2MSFTNGP04.phx.gbl...

> Pegasus (MVP) wrote:
>> Did you try the solution proposed by "billious"?
>>
>>
>

> Yes I have try but it didn't work. I didn't know where I can input the
> target for searching the keyword. It was because beside of "User Name" I
> also need to filter the others.
>
> Thanks!
>

The string to be found is the string in quotes following the "find" keyword
in [5] - I was looking for "<td>" in testing.

Replace "<td>" in [5] with "User Name"

Add /i after the /n to make the search case-insensitive (ie. use

FIND /n /i

instead of

FIND /n

)

OR

replace <td> in [5] with %~1

You could then run your batch as

BATCHNAME "User Name"
or
BATCHNAME Something

to search for "User Name" or "Something" (If there are spaces in your target
string, enclose in quotes)

Or do you want to wait for user-input to get your target string?


Blue Fish

unread,
Sep 5, 2008, 2:48:16 AM9/5/08
to
Yes, thanks you. It works, I am now understand the logic. It will record
down the line number and the above line number. If the line number was
not match, it will redirect this line to another file.

Thanks a lot!

foxidrive

unread,
Sep 5, 2008, 3:27:22 AM9/5/08
to
>Pegasus (MVP) wrote:
>> Did you try the solution proposed by "billious"?


On Fri, 05 Sep 2008 12:59:11 +0800, Blue Fish <blue...@isp.com> wrote:

>Yes I have try but it didn't work. I didn't know where I can input the
>target for searching the keyword. It was because beside of "User Name" I
>also need to filter the others.

That is a common problem with requests for information - the task changes
once the OP has a solution. billious' code works fine for what you asked.

Describe exactly what you need to do and maybe billious will be generous
enough to rework what he has already done.


BTW, regarding your other post SED is a 3rd party utility. GNUSED is the
version that I use.

Timo Salmi

unread,
Sep 5, 2008, 3:29:00 AM9/5/08
to
Blue Fish <blue...@isp.com> wrote:
> May I know where I can have the "sed" command?

Explained e.g. in the FAQ collection I referred to
http://www.netikka.net/tsneti/info/tscmd.htm#sed

foxidrive

unread,
Sep 5, 2008, 3:30:57 AM9/5/08
to
On Thu, 4 Sep 2008 15:13:17 +0800, "billious" <billio...@hotmail.com>
wrote:

>[8]for /f "tokens=1*delims=[]" %%i in ( 'find /v /n "" ^<zout.txt ') do if
>not %%i==%yft% if not %%i==%yst% >>zout1.txt echo\%%j

Do you know how long I have been labouring under the mistaken belief that
echoing a for loop variable with poison characters won't work? :) Thanks
for correcting me.

0 new messages