China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 11:13
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please tell me how to display the line before the line found by findstr. View 2,554 Replies 0
Original Poster Posted 2017-03-02 16:22 ·  中国 上海 联通
初级用户
Credits 179
Posts 35
Joined 2003-06-03 00:00
23-year member
UID 3055
Gender Male
Status Offline
The line before does not include the character to be found. How to display the line before the line found by findstr?

set ACCOUNT=1234
set DATE=20170302
set DIR=d:\log\
for %%i in (%DATE%) do findstr /nisd:%DIR% "%ACCOUNT%" %%i.log >>log.txt

The line in the log that includes the account number has no time, and the previous line has time, but the previous line does not have the account number. We cannot display all the lines with time because there are logs of other accounts.

Under Linux, awk can be used to achieve: awk '/1234/{print a"\n"$1;}{a=$0}' 20170302.log

[ Last edited by wiq on 2017-3-3 at 09:20 ]
Forum Jump: