경로

2012-08-27 2 views
1

을 대체 내가 파일에경로

(setq myFile "/some/path") 

에 경로를 대체합니다.

find ./_build/html -type f -name '*.html' | while read myFile; do 
    MyFile=`readlink -f "$myFile"` 
    sed -i "s/setq myFile [)]*/setq myFile \"$MyFile\"/" sphinx_nowrap.el 
    # and then some actions on file 
done 

와 펄과 : 내가 나오지도 함께 할 것을 시도

find ./_build/html -type f -name '*.html' | while read myFile; do 
    MyFile=`readlink -f "$myFile"` 
    perl -ne "s/setq myFile .+/setq myFile \"$MyFile\")/" sphinx_nowrap.el 
    # and then some actions on file 
done 

을하지만 모두 오류를 제공합니다.

나는 thisthisthis으로 읽었지만 제대로 작동하지 않습니다.

편집 :

sed: -e expression #1, char 34: unknown option to `s' 

편집 2 : 여기

Having no space between pattern and following word is deprecated at -e line 1. 
Bareword found where operator expected at -e line 1, near "s/setq myFile .+/setq myFile "/home" 
String found where operator expected at -e line 1, at end of line 
     (Missing semicolon on previous line?) 
syntax error at -e line 1, near "s/setq myFile .+/setq myFile "/home" 
Can't find string terminator '"' anywhere before EOF at -e line 1. 

및 오류 나오지도 있어요 :

그래서 솔루션이다

여기에 펄 오류입니다 기음 delimeter char를 hange하십시오. 또한 SED 식을 변경해야합니다 :

sed -i "s!setq myFile .*!setq myFile \"$MyFile\")!" sphinx_nowrap.el 
+0

그리고 그 오류가 무엇을 할 수 있는가? –

+1

오류를 추가 할 수 있습니다. – Stephan

답변

4

펄 (와 sed)와 같은 정규식 구분 기호로 파일 경로에 슬래시를 인식 본다.

find ./_build/html -type f -name '*.html' | while read myFile; do 
    MyFile=`readlink -f "$myFile"` 
    sed -i "s!setq myFile [)]*!setq myFile \"$MyFile\"!" sphinx_nowrap.el 
    # and then some actions on file 
done 
2

$MyPath/foo/bar/baz를 개최하여 가정 수 있습니다 :

find ./_build/html -type f -name '*.html' | while read myFile; do 
    MyFile=`readlink -f "$myFile"` 
    perl -ne "s!setq myFile .+!setq myFile \"$MyFile\")!" sphinx_nowrap.el 
    # and then some actions on file 
done 

또는 SED의 경우 : 다른 구분 기호를 사용할 수 있습니다. 귀하의 정규식 세 번째 / 문자로 종료

perl -ne "s/setq myFile .+/setq myFile \"/foo/bar/baz\")/" sphinx_nowrap.el 

: 그런 다음 펄 코드는 다음과 같이 읽습니다. 이 문제를 해결하기 위해, 우리는 s{}{} 같은 다른 구분 기호를 사용할 수 있습니다 뭔가 실제로 인쇄 얻을 수 있도록

perl -ine "s{setq myFile .+}{setq myFile \"/foo/bar/baz\")}; print" sphinx_nowrap.el 

나는 또한 -i 옵션 (인플레 이스 편집)과 print 문을 추가했다.

그러나 아마 명령 줄 인수로 $MyPath AOF 값을 통과 할 더 우아한 것 :

perl -ne 's{setq myFile .+}{setq myFile "$ARGV[0]")}; print' $MyPath <sphinx_nowrap.el >sphinx_nowrap.el