Shell脚本搜索&替换为文本文件

时间:2012-07-03 08:17:33

标签: android bash shell

我必须在android shell中编辑文本文件。

所以我输入这个shell脚本。

但我的Galaxy Nexus没有Sed,Awk也没有。

shell@android:/ $ sed -e "s/old_pattern/new_pattern/g" file_name > modify_file
/system/bin/sh: sed: not found

它没有用。

如何在文本文件中修改old_patternnew_pattern

是否可以在Shell脚本中使用?

已编辑的Shell脚本

#!/system/bin/sh
ARGS=4
BAD=65

if [ $# -ne "$ARGS" ]
then
    echo "Usage: `basename $0` TARGET_FILE,OLD_PATTERN,NEW_PATTERN,MODIFY_FILE"
    exit $BAD
fi

old_pattern=$2
new_pattern=$3
modify_file=$4

if [ -f "$1" ]
then
    target_file=$1
else
    echo "\"$3\" Does not exist."
    exit $BAD
fi

exit 0

解决方案:

shell@android:/ $ while read STRING
>do
>echo "${STRING//old_pattern/new_pattern}" >> modify_file_name
>done < target_file_name
shell@android:/ $ 

1 个答案:

答案 0 :(得分:1)

可以使用纯bash,但在大文件上它会非常流畅。

while read STRING
do
    echo "${STRING//old_pattern/new_pattern}" >> modify_file
done < file_name

PS。哦,我刚刚提到你的shell不是bash。好像它只是sh。这不适用于sh

相关问题