Replace text in file

 

Windows

 

@echo off

if %1%.==. (
  echo Usage: %0% FILE_FULL_PATH SOURCE_STRING DESTINATION_STRING
  echo Where: FILE_FULL_PATH is the full path of the file to be replaced in
  echo        SOURCE_STRING is string to be replaced
  echo        DESTINATION_STRING is string to be replaced as
  pause 
  exit
)

if %2%.==. (
  echo Usage: %0% FILE_FULL_PATH SOURCE_STRING DESTINATION_STRING
  echo Where: FILE_FULL_PATH is the full path of the file to be replaced in
  echo        SOURCE_STRING is string to be replaced
  echo        DESTINATION_STRING is string to be replaced as
  pause 
  exit
)

if %3%.==. (
  echo Usage: %0% FILE_FULL_PATH SOURCE_STRING DESTINATION_STRING
  echo Where: FILE_FULL_PATH is the full path of the file to be replaced in
  echo        SOURCE_STRING is string to be replaced
  echo        DESTINATION_STRING is string to be replaced as
  pause 
  exit
)

setlocal enabledelayedexpansion 

set file=
rem set /p replaced= Please input the file full path: 
set "file=%1%"
for %%i in ("%file%") do set file=%%~fi 
echo. 

set replaced= 
rem set /p replaced= Source String: 
set "replaced=%2%"  
echo. 

set all= 
rem set /p all= Destination String: 
set "all=%3%"
echo.

for /f "delims=" %%i in ('type "%file%"') do ( 
  set str=%%i 
  set "str=!str:%replaced%=%all%!" 
  echo !str!>>"%file%".tmp 
) 

copy "%file%" "%file%".bak >nul 2>nul 
move "%file%".tmp "%file%" 
rem start "" "%file%" 

 

 

Linux

 

sed -in-place -e 's/old/new/g' text.txt

你可能感兴趣的:(linux,windows,F#)