首页 > 解决方案 > 用文件中的shell脚本替换字符

问题描述

我在 unix 中有一个 txt 文件,其中有一个特殊字符。我需要替换特殊字符的所有实例在 shell 脚本中我该怎么做?

这是我的代码。我需要在 sftp 之前替换所有特殊字符

elif [[ $module = "get_ce_xml_to_xcent" ]]
  then
    echo "get_ce_xml_to_xcent"

    ssh -T -o ConnectTimeout=5 oracle@GL-REL-CEAPP.abanet.local <<ENDSSH 
    cd /srv/exports/promotion 
    chmod 777 *.txt
sftp oracle@GL-REL-XSTAPP <<ENDFTP
cd /fileuploads/org69
mput *.txt
quit
ENDFTP
    #for i in `ls *.txt 2>/dev/null`; do echo $i; mv $i /srv/exports/promotion/arch/$i.`date "+%Y%m%d%H%M%S"` ; done
    mv *.txt arch/
        exit
ENDSSH

标签: shellunixreplace

解决方案


假设您有一个在多个位置.txt包含特殊字符()的文件:ý

mayankp@mayank:~/$ cat test.txt 
"123","qweeerr","qqqqqq ý
rrrrr
hhhhhh",ý "sdfsfs" 

ý您可以从如下文件中替换:

mayankp@mayank:~/$ sed -i 's/ý//g' test.txt 
mayankp@mayank:~/$ cat test.txt 
"123","qweeerr","qqqqqq 
rrrrr
hhhhhh", "sdfsfs"

您只需要在命令sed -i 's/ý//g' test.txt之前在脚本中附加该行sftp

让我知道它是否有效。


推荐阅读