我如何将 sed 与 unicode 字符一起使用

5 linux bash terminal command-line sed

function change() {\n  for i in {0..28}\n  do\n    echo ",${cryp_data_letter[$i]}" "${org_data[$i]}"\n    sed -i "s/,${cryp_data_letter[$i]}/${org_data[$i]}/g" "./temp.txt"\n    #cat "./temp.txt"\n  done\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我有一个函数可以通过特定的规则更改 temp.txt 中的某些字符,但某些字符(例如 \xc4\xb1、\xc4\x9f、\xc3\xb6 等)会用空字符串更改。我想问题的原因是 UTF-8 那么我如何将 sed 与 unicode 一起应用?或任何其他建议 --> "sed -i "s/,${cryp_data_letter[$i]}/${org_data[$i]}/g" "./temp.txt""

\n\n

这是给定的文件 temp.txt:

\n\n
abc \xc4\x9fh\xc4\xb1i\ndef\njkl\no\xc3\xb6pr\nu\xc3\xbc vy z\n\xc3\xa7gm ns\n\xc5\x9ft\n
Run Code Online (Sandbox Code Playgroud)\n\n

和输出:

\n\n
IDK ,\xc4\x9fS,\xc4\xb1T\nNMY\nBO\xc3\x9c\nG,\xc3\xb6H\xc3\x87\nP,\xc3\xbc \xc3\x96F ,\n,\xc3\xa7U\xc5\x9e Z\xc4\x9e\n,\xc5\x9fV\n
Run Code Online (Sandbox Code Playgroud)\n\n

顺便说一句,在返回过程中,我将把所有字母更改为小写,并在所有字母之前放置“,”,这样它就会变成 sed 之前:

\n\n
,a,b,c ,\xc4\x9f,h,\xc4\xb1,i\n,d,e,f\n,j,k,l\n,o,\xc3\xb6,p,r\n,u,\xc3\xbc ,v,y ,z\n,\xc3\xa7,g,m ,n,s\n,\xc5\x9f,t\n
Run Code Online (Sandbox Code Playgroud)\n\n

区域设置:

\n\n
LANG=en_US.UTF-8\nLANGUAGE=en_US:en\nLC_CTYPE="en_US.UTF-8"\nLC_NUMERIC=tr_TR.UTF-8\nLC_TIME=tr_TR.UTF-8\nLC_COLLATE="en_US.UTF-8"\nLC_MONETARY=tr_TR.UTF-8\nLC_MESSAGES="en_US.UTF-8"\nLC_PAPER=tr_TR.UTF-8\nLC_NAME=tr_TR.UTF-8\nLC_ADDRESS=tr_TR.UTF-8\nLC_TELEPHONE=tr_TR.UTF-8\nLC_MEASUREMENT=tr_TR.UTF-8\nLC_IDENTIFICATION=tr_TR.UTF-8\nLC_ALL=\n
Run Code Online (Sandbox Code Playgroud)\n

tha*_*guy 1

很抱歉没有回答,但我无法重现您的问题。

\n\n

这是完全独立的脚本中的代码(请下次自己执行此操作):

\n\n
#!/bin/bash\n\nif [[ \xc3\xb6 != $\'\\xC3\\xB6\' ]]\nthen\n  echo "You didn\'t save this file as UTF-8"\n  exit 1\nfi\n\nfunction change() {\n  for i in {0..28}\n  do\n#    echo ",${cryp_data_letter[$i]}" "${org_data[$i]}"\n    sed -i "s/,${cryp_data_letter[$i]}/${org_data[$i]}/g" "./temp.txt"\n    #cat "./temp.txt"\n  done\n}\n\n# Shift all characters one letter ahead in the alphabet\ncryp_data_letter=({a..z} \xc4\x9f \xc3\xb6 \xc4\xb1)\norg_data=({b..z} \xc4\x9f \xc3\xb6 \xc4\xb1 a)\n\n# Create the file as you say it is before the sed\ncat > temp.txt << "EOF"\n,a,b,c ,\xc4\x9f,h,\xc4\xb1,i\n,d,e,f\n,j,k,l\n,o,\xc3\xb6,p,r\n,u,\xc3\xbc ,v,y ,z\n,\xc3\xa7,g,m ,n,s\n,\xc5\x9f,t\nEOF\n\nchange\n\ncat temp.txt\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我运行时,./testscript我得到以下输出:

\n\n
bcd \xc3\xb6iaj\nefg\nklm\np\xc4\xb1qs\nv,\xc3\xbc wz \xc4\x9f\n,\xc3\xa7hn ot\n,\xc5\x9fu\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xc3\xb6正如您所看到的,包括和在内的字母\xc4\x9f都被很好地替换和插入。

\n