我有两个文本文件,内容如下:
file1.txt:
ProcessId VirtualSize
5752 74649600
3932 76843610
1357 90215638
& so on....file2.txt:
Notepad.exe pid: 3932 Linux
Notepad.exe pid: 1357 Macos
Notepad.exe pid: 5752 Windows
& so on....现在,我们可以看到两个文件中的进程i是相同的(匹配),因此我希望生成一个统一的单个输出文件(在两个文件中匹配processId ),该文件应该具有以下内容:
Output.txt:
Windows 74649600
Linux 76843610
Macos 90215638
& so on....我在下面尝试过,它正在运行,但没有获得所需的输出:
@echo off
(for /f "skip=1 tokens=1,2" %%a in (file1.txt) do (
for /f "tokens=5" %%c in ('find " %%a " ^< file2.txt ') do echo %%c %%b
))>Output.txt EDIT1:如果我想用字符串永久地修复/设置'Output.txt‘的前两行,应该添加什么:
This output is for first server
Applcation Memory(GB )即:
Output.txt:
This output is for first server
Applcation Memory(GB)
Windows 74649600
Linux 76843610
Macos 90215638
& so on....发布于 2013-10-16 04:36:43
那麽:
@echo off
echo This output is for HFM server > out.txt
echo Applcation Memory(GB) >>out.txt
for /f "skip=1 tokens=1,2" %%a in (file1.txt) do (
for /f "skip=2 tokens=5" %%c in ('find " %%a " file2.txt 2^>nul') do (
echo %%c %%b >>out.txt
)
) 发布于 2013-10-16 03:54:05
你的第二个目标应该如下所示。"tokens=5“部分选择第五个令牌,没有其他任何内容。你需要令牌3和5。
(for /f "skip=1 tokens=1,2" %%a in (file1.txt) do (
for /f "tokens=3,5" %%x in ('find " %%a " ^< file2.txt ') do echo %%b %%y
))如果File1和File2是长的,那么这个脚本就能工作,但是会非常慢。我编写了一个脚本,该脚本对file1和file2进行了重新格式化,以便在每一行中首先使用PID。然后,使用排序按照PID来组织文件。最后,扫描排序输出,寻找具有匹配PID的对行。
有点长,但很容易看出它是如何工作的。
@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set INPUT1=file1.txt
set INPUT2=file2.txt
set OUTFILE=Outfile.txt
set TMPFILE=OutfileTMP.txt
set OUT=^>^>%OUTFILE%
if exist %OUTFILE% del %OUTFILE%
if exist %TMPFILE% del %TMPFILE%
call :ReadFile1
call :ReadFile2
sort < %OUTFILE% > %TMPFILE%
del %OUTFILE%
echo This output is for HFM server%OUT%
echo Applcation Memory(GB )%OUT%
set LASTPID=-
set LASTSIZE=-
for /f "tokens=1,2,3" %%a in (%TMPFILE%) do (
if "%%b"=="1" set LASTPID=%%a&set LASTSIZE=%%c
if "%%b"=="2" (
if "%%a"=="!LASTPID!" (
echo %%c !LASTSIZE! %OUT%
) else (
echo Error: Not Matched: 1:!LASTPID!,!LASTSIZE!, 2:%%a %%c
)
)
)
del %TMPFILE%
goto :EOF
:ReadFile1
for /f "skip=1 tokens=1,2" %%a in (%INPUT1%) do echo %%a 1 %%b %OUT%
goto :EOF
:ReadFile2
for /f "tokens=3,5" %%a in (%INPUT2%) do echo %%a 2 %%b %OUT%
goto :EOFhttps://stackoverflow.com/questions/19394483
复制相似问题