How can remove the line match before?

  • Thread starter Thread starter Blue Fish
  • Start date Start date
B

Blue Fish

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!
 
Blue Fish said:
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!

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>
<td>Local Time</td><td>2008-08-07 16:48:47</td></tr>
 
Blue Fish said:
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!


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.
 
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!
 
Blue Fish said:
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!

Did you try the solution proposed by "billious"?
 
Blue Fish said:
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
 
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!
 
Blue Fish said:
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?
 
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!
 
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.
 
[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.
 
Back
Top