-1

ファイルから特定の情報を取り出すにはどうすればよいですか。

例えば:

ファイル.txtから多くのデータがあります

1)a=123    
2)b=234     
3)c=345  
4)e=456  

情報を取り出したい' a ';
列、行、または単語を指定する必要がありますか?

どのようにすればいいのか、どのコマンドを使うべきなのかわかりません。
バッチファイルのコマンドを使用してこれを行うことはできますか、それとも他のプログラムソフトウェアを使用する必要がありますか?

希望は私を助けることができます。

4

2 に答える 2

0

簡単な方法:

@Echo OFF
Type "Text.txt" | Find "a="

途中:

@Echo OFF
For /F "Tokens=1,* Delims==" %%a in ('Type "Text.txt" ^| Find "a="') Do (Echo %%a, %%b)

便利な方法:

@Echo OFF

:: TEXT MANIPULATOR ROUTINE v0.1
:: by Elektro H@cker

REM USE:
::TEXTMAN [ACTION] [IN LINE (Optional)] [FILE] [TEXT (Optional)]


REM ACTIONS:
REM 
REM  AL  = ADD_LEFT           * Add text to the beginning of a line
REM  AR  = ADD_RIGHT          * Add text to the end of a line
REM  E   = ERASE              * Delete a line
REM  I   = INSERT             * Add a empty line (Or a line with text)
REM  RL  = REPLACE_LINE       * Replace a line
REM  RS  = REPLACE_STRING     * Replace words of a line
REM  RSA = REPLACE_STRING_ALL * Replace words of all line
REM  C+  = CHARACTER_PLUS     * Delete the first "X" characters in all lines
REM  C-  = CHARACTER_LESS     * Delete the last  "X" characters in all lines
REM  L+  = LINE_PLUS          * Cut the first "X" lines
REM  L-  = LINE_LESS          * Cut the last  "X" lines


REM EXAMPLES:

:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
::
:: Deletes the line 3
:: Call :TEXTMAN E 3 "Test.txt"
::
:: Add a phrase to the beginning of line 3
:: Call :TEXTMAN AL 3 "Test.txt" "Elektro H@cker"
::
:: Add a phrase to the end of line 3
:: Call :TEXTMAN AR 3 "Test.txt" "Elektro H@cker"
::
:: Add a empty line at line 3
:: Call :TEXTMAN I 3 "Test.txt"
:: 
:: Add a line with a word at line 3
:: Call :TEXTMAN I 3 "Test.txt" "Elektro H@cker"
::
:: Replaces the line 3 with "Elektro H@cker"
:: Call :TEXTMAN RL 3 "Test.txt" "Elektro H@cker"
::
:: Replaces the words "Elektro" to "H@cker" in line 3
:: Call :TEXTMAN RS 3 "Test.txt" "Elektro" "H@cker"
::
:: Replaces the words "Elektro" to "H@cker" in all lines
:: Call :TEXTMAN RSA "Test.txt" "Elektro" "H@cker"
::
:: Deletes the first 3 characters in all lines
:: Call :TEXTMAN C+ 3 "Test.txt"
::
:: Deletes the last 3 characters in all lines
:: Call :TEXTMAN C- 3 "Test.txt"
::
:: Deletes the first 3 lines
:: Call :TEXTMAN L+ 3 "Test.txt"
::
:: Deletes the last 3 lines
:: Call :TEXTMAN L- 3 "Test.txt"
::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::

:TEXTMAN
(SET /A "A=0", "LINE=0", "TOTAL_LINES=0")  &  (CALL :%~1 %* || (ECHO Parametro incorrecto & Exit /B 1)) & (GOTO:EOF)
:AL
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %~4%%@ >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:AR
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %%@%~4 >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:E
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:I
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF     "%%LINE%%" EQU "%~2" (IF NOT "%~4" EQU "" (Echo %~4 >> "%~nx3.NEW") ELSE (Echo. >> "%~nx3.NEW"))" & Echo %%@ >> "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RL
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %~4 >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RS
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (CALL SET "STRING=%%@" && CALL ECHO %%STRING:%~4=%~5%% >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RSA
(For /F "usebackq tokens=*" %%@ in ("%~2") DO (CALL SET "STRING=%%@" && (CALL ECHO %%STRING:%~3=%~4%% >> "%~2.NEW"))) && (CALL :RENAMER "%~2") & (GOTO:EOF)
:C+
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set   "LINE=%%@" && (CALL ECHO %%LINE:~%~2%% >>    "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:C-
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set   "LINE=%%@" && (CALL ECHO %%LINE:~0,-%~2%% >> "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L+
(Call SET /A "A=%~2-1") && (Call TYPE "%~3" | @MORE +%%A%% > "%~nx3.NEW") && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L-
(FOR /F %%X IN ('TYPE "%~3"') DO (CALL SET /A "TOTAL_LINES+=1")) & (CALL SET /A "TOTAL_LINES-=%~2-1") & (For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" & Call echo " %%LINE%%!!| FINDSTR " %%TOTAL_LINES%% " && CALL :RENAMER "%~3" && GOTO:EOF || (Echo %%@ >> "%~nx3.NEW")))
:RENAMER
(REN "%~1" "%~nx1.BAK") & (MOVE /Y "%~nx1.BAK" "%TEMP%\" >NUL) & (REN "%~nx1.NEW" "%~nx1") & (GOTO:EOF)
于 2012-11-28T15:40:23.487 に答える
0

等号 (=) を列区切り記号として扱いたいですか? もしそうなら、そしてあなたは awk を持っています、あなたはこれを行うことができます:

cat file.txt 
a=123    
b=234     
c=345  
e=456

awk -F = '{print $2}' file.txt 
123    
234     
345  
456  

これをプログラムで行いたい場合は、ライブラリを使用してテキストを等号で分割できます。ほとんどすべてのプログラミング言語には、これを行うためのライブラリがあります。

于 2012-11-28T14:49:41.927 に答える