【在 p*u 的大作中提到】 : replace an old string with a new string in a lot of file, please tell me the : command name and necessary arguments. thank you very much!
c*e
3 楼
你可以 man 一下 sed,这个命令很有用 一般情况下,可以用: cat source_filename | sed -e s/"sourceString"/"targetString"/g > target_filename 如果多个文件,可以用 for 循环。
【在 p*u 的大作中提到】 : replace an old string with a new string in a lot of file, please tell me the : command name and necessary arguments. thank you very much!
#!/bin/sh # This is a simple script that replace string in a file. # No argument checking. str_orig=$1 str_new=$2 shift shift for i in $*; do sed -e s/$str_orig/$str_new/g $i > /tmp/$i.$$ mv /tmp/$i.$$ $i done exit 0
【在 p*u 的大作中提到】 : replace an old string with a new string in a lot of file, please tell me the : command name and necessary arguments. thank you very much!
k*n
6 楼
the simplest way to do this is: find . -name "*.c" -exec vi -c "1,$ s/old_string/new_string/g" -c "wq" {} \; will replace all old string with new string in all .c files
【在 p*********r 的大作中提到】 : #!/bin/sh : # This is a simple script that replace string in a file. : # No argument checking. : str_orig=$1 : str_new=$2 : shift : shift : for i in $*; do : sed -e s/$str_orig/$str_new/g $i > /tmp/$i.$$ : mv /tmp/$i.$$ $i
q*t
7 楼
cool!
【在 k*****n 的大作中提到】 : the simplest way to do this is: : find . -name "*.c" -exec vi -c "1,$ s/old_string/new_string/g" -c "wq" {} \; : will replace all old string with new string in all .c files
c*e
8 楼
A more effective way would be a one line comand: for file in *.c; do sed -e s/"old_string"/"new_string"/g $file | cat > $file;done
the
【在 k*****n 的大作中提到】 : the simplest way to do this is: : find . -name "*.c" -exec vi -c "1,$ s/old_string/new_string/g" -c "wq" {} \; : will replace all old string with new string in all .c files