2013-07-12 3 views
2

간단한 find 명령을 사용하여 수백 개의 html 파일을 검색 한 다음 각 파일 내의 간단한 텍스트를 바꿉니다.특정 문자열을 포함하는 파일 만 열고 Linux 명령 줄에서 바꿉니다.

  1. 검색 문자열이 포함 된 파일을 찾고 나열하려면. 나에게 파일의 간단한 목록을 제공

    find . -iname '*php' | xargs grep 'search-string' -sl 
    

좋아.

  1. ./javascript_open_new_window_form.php 
        ./excel_large_number_error.php 
        ./linux_vi_string_substitution.php 
        ./email_reformat.php 
        ./online_email_reformat.php 
    

    검색하고 내가 사용하는 문자열을 교체하십시오.

    sed -i 's/search-string/replace-string/' ./javascript_open_new_window_form.php 
    sed -i 's/search-string/replace-string/' ./excel_large_number_error.php 
    sed -i 's/search-string/replace-string/' ./linux_vi_string_substitution.php 
    sed -i 's/search-string/replace-string/' ./email_reformat.php 
    sed -i 's/search-string/replace-string/' ./online_email_reformat.php 
    

그래서 제 질문은 내가 그래서이 명령을 결합 할 수 있습니다 어떻게 수동으로 복사하고 파일 이름마다 붙여 넣을 필요가 없습니다 ...입니다.

미리 도움을 주셔서 감사합니다.

답변

3

이 작업을 시도 할 수 :

find . -iname '*php' | xargs grep 'search-string' -sl | while read x; do echo $x; sed -i 's/search-string/replace-string/' $x; done 
1

파이프 그것을 다시 다른 xargs에. 두 번째 파일을 만들려면 xargs-n 1을 사용하여 기본 동작 대신 입력의 각 파일에 대해 명령을 하나씩 실행하십시오. 좋아요 :

find . -iname '*php' | xargs grep 'search-string' -sl | xargs -n 1 sed -i 's/search-string/replace-string/' 
관련 문제