我如何使用 sed 或 grep 来处理这个

How i can used sed or grep with this

我有一个包含 20000 行的文本文件,其中一些行内容为 1 个符号或 2 个或 3 个来自该范围的符号:

[\x{0990}-\x{099D}]

我想在该行的最后一个符号之后添加新行 \n,例如: 之前:

Alpha beta @#$ gama

之后:

Alpha beta @#$
gama

您需要搜索字符串:

([!@#$%^&*()_+=~`-]{1,3})([^!@#$%^&*()_+=~`-]*)$

并替换为:

\n

测试here.

符号列表可以根据您的需要进行调整。我在键盘上的数字键上添加了大部分符号。

你的意思是这样的?

sed 's/[\x0990-\x099D]\{1,3\} /&\n/' file.txt

说明

s                              # use substitution
/                              # separator
[\x0990-\x099D]\{1,3\}         # 1 to 3 symbols
/                              # separator
&\n                            # replace with symbols\n
/                              # separator

字符为ঐ঑঒ওঔকখগঘঙচছজঝ,您可以在sed中明确使用它们:

sed -E 's/(ঐ|঑|঒|ও|ঔ|ক|খ|গ|ঘ|ঙ|চ|ছ|জ|ঝ){1,3}[[:space:]]*/&\n/g'

完整的就地命令:

sed -i -E 's/(ঐ|঑|঒|ও|ঔ|ক|খ|গ|ঘ|ঙ|চ|ছ|জ|ঝ){1,3}[[:space:]]*/&\n/g' file; # GNU sed
sed -E -i '' 's/(ঐ|঑|঒|ও|ঔ|ক|খ|গ|ঘ|ঙ|চ|ছ|জ|ঝ){1,3}[[:space:]]*/&\n/g' file; # Free BSD sed

看到 online sed demo.

这里,

  • (ঐ|঑|঒|ও|ঔ|ক|খ|গ|ঘ|ঙ|চ|ছ|জ|ঝ){1,3} 是匹配所需字符 1、2 或 3 次的括号表达式
  • [[:space:]]* - 0+ 个空格。

&\n 替换模式插入整个匹配项,然后添加换行符。

提示:要trim这些字符后的空格,请使用

sed -E 's/((ঐ|঑|঒|ও|ঔ|ক|খ|গ|ঘ|ঙ|চ|ছ|জ|ঝ){1,3})[[:space:]]*/\n/g'