2012-11-28 104 views
-1

如何從文件中取出某些信息。從文件中取出某些數據

例如:

從文件.TXT有大量的數據

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

我要拿出信息 '一個' 的;
我需要特定的列,行或單詞嗎?

我不知道應該怎麼做,或者應該使用什麼命令。
我可以使用批處理文件中的命令來執行此操作,還是應該使用其他程序軟件?

希望能幫助我..

+0

你可以試着用'findstr'進行搜索,以獲取更多的幫助類型'findstr /?' – jeb

+0

'cat file.txt | grep -v「a =」> file2.txt'。 – 2012-11-28 14:50:09

回答

0

所以你要正確對待等號(=)作爲列分隔符?如果你想這樣做編程,你可以使用庫分割上了等號文本

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

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

:如果是這樣,你有AWK,你可以做到這一點。大多數所有的編程語言都有庫來做到這一點。

+0

如何,如果我想grep幾行,讓說1行到10行 –

+0

@UNA Nh請參閱http://stackoverflow.com/questions/83329/how-can-i-extract-a-range-of -lines從 - 一個文本文件上,UNIX – NoChance

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 [email protected] 

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 [email protected]" 
:: 
:: Add a phrase to the end of line 3 
:: Call :TEXTMAN AR 3 "Test.txt" "Elektro [email protected]" 
:: 
:: 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 [email protected]" 
:: 
:: Replaces the line 3 with "Elektro [email protected]" 
:: Call :TEXTMAN RL 3 "Test.txt" "Elektro [email protected]" 
:: 
:: Replaces the words "Elektro" to "[email protected]" in line 3 
:: Call :TEXTMAN RS 3 "Test.txt" "Elektro" "[email protected]" 
:: 
:: Replaces the words "Elektro" to "[email protected]" in all lines 
:: Call :TEXTMAN RSA "Test.txt" "Elektro" "[email protected]" 
:: 
:: 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)