2013-08-01 2 views
0

파일에서 주어진 텍스트를 찾아 지정된 경로에서 바꾸고 텍스트를 바꾼 후 해당 파일의 이름 바꾸기 주어진 단어로. sed를 사용하는 동안 허가가 거부되었습니다. 내 스크립트 내가 아래에 오류가 발생하고이셸 스크립트 및 sed 사용 - 파일에서 단어 찾기 및 바꾸기 및 파일 이름 바꾸기

`echo "Please Insert the path of the folder" 
read input_variable 

    read -p "You entered: $input_variable is correct y/n " yn 

    read -p "Enter the word to find = " word 
    read -p "Enter word to replace = " replace 
    case $yn in 
     [Yy]*) find "${input_variable}" -type f -iname "${word}.*" | while read filename; do "`echo "${filename}" | sed -i 's/$word/$replace/g' ${filename}| sed -i 's/\$word/\$replace/' ${filename}`"; done ;; 
     [Nn]*) exit;; 
     *) echo "Please answer yes or no.";; 
    esac` 

과 같은

bulk_rename.sh : 34 : bulk_rename.sh : 권한이

어떤 제안이 거부? @vijay에 의해 제안 이제 스크립트

echo "Please Insert the path of the folder" 
read input_variable 

read -p "You entered: $input_variable is correct y/n " yn 

read -p "Enter the word to find = " word 
read -p "Enter word to replace = " replace 
case $yn in 
    [Yy]*) find "${input_variable}" -type f -iname "${word}.*" | while read filename; do 
    perl -pi -e 's/$word/$replace' ${filename} 
    mv ${filename} $word; done;; 

    [Nn]*) exit;; 
    *) echo "Please answer yes or no.";; 
esac 

를 업데이트 한 후

나는 다음과 같은


대체 교체를 얻고 것은 -e 줄에서 종료되지 1

이것은 내가 chmod 출력을 표시 할 때 얻을 수있다.

[email protected]:~/Documents/blog$ chmod +x bulk_rename.sh ; /bin/ls -l bulk_rename.sh 
chmod +x bulk_rename.sh ; /bin/ls -l bulk_rename.sh 
+ chmod +x bulk_rename.sh 
+ /bin/ls -l bulk_rename.sh 
-rwxrwxr-x 1 abc abc 1273 Aug 1 16:51 bulk_rename.sh 
+0

'chmod를 + X bulk_rename.sh' – devnull

+0

을 요구 한이 질문의 도움으로 해당 파일을 편집 할 수있는 권한이 없습니다. – devnull

+0

빠른 답장을 보내 주셔서 감사합니다. @devnull 또한 권한을 변경해 보았습니다. –

답변

1

마지막으로 나는 SED와를 사용하여 내 문제를 내 솔루션 함께 내가 어쩌면 당신을 Question

echo "Please Insert the path of the folder" 
read input_variable 

read -p "You entered: $input_variable is correct y/n " yn 

read -p "Enter the word to find = " word 
read -p "Enter word to replace = " replace 
case $yn in 
    [Yy]*) grep -r -l "$word" $input_variable | while read file; do echo $file; echo $fname; sed -i "s/\<$word\>/$replace/g" $file ; done; find "$input_variable" -type f -name "$word.*" | while read file; do dir=${file%/*}; base=${file##*/}; noext=${base%.*}; ext=${base:${#noext}}; newname=${noext/"$word"/"$replace"}$ext; echo mv "$file" "$dir/$newname"; done;; 
    [Nn]*) exit;; 
    *) echo "Please answer yes or no.";; 
esac 
0

나는 복잡한 것으로 생각합니다. 두 개의 간단한 satement로 단순하게 만들지 않는 이유는 무엇입니까? 당신이 당신의 목적을 위해 아래 문을 사용하는 방법을 당신에게 그것까지 : 오류 MSG가 누락 된`/ '문자를 표시

perl -pi -e 's/$word/$replace' ${filename} 

perl -pi -e "s/$word/$replace/" ${filename} 
--------------^----------------^^-------- 

perl -pi -e 's/wordtofind/wordtoreplace' your_file #for replacing the word in the file 

mv your_file wordtoreplace #for renaming the file 
+0

고마워요. @Vijay .. 쉘 스크립팅을 처음 접했습니다. 나는 내가 편집 할 파일 목록을 가지고있을 것이다. 그래서 당신이 권하는 것은 위의 스크립트가 정확하다는 것입니다. 사용자로부터 모든 것을 요구하고 있습니다. 그리고 어떻게 sed가 이것과 다른가? –

0

변경합니다.


또한 원래 코드로 얻은 오류는 무엇입니까?

sed를 둘러싼 dbl 인용 부호가 perl과 마찬가지로 필요하므로 쉘에서 값을 대체 할 수 있습니다. 즉

..... | sed -i "s/$word/$replace/g" 
    ----------^------------------^ 

이 장난 꾸러기 캐릭터가 특히 /$word 또는 $replace의 내부가 아닌 가정합니다.

IHTH