Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在tcl中使用sed将“{4,5}{6,7}”中的“}{”替换为换行符?我的意思是“{4,5}和{6,7}”应该是不同的行_Sed_Tcl - Fatal编程技术网

如何在tcl中使用sed将“{4,5}{6,7}”中的“}{”替换为换行符?我的意思是“{4,5}和{6,7}”应该是不同的行

如何在tcl中使用sed将“{4,5}{6,7}”中的“}{”替换为换行符?我的意思是“{4,5}和{6,7}”应该是不同的行,sed,tcl,Sed,Tcl,我在一个文本文件中有{4,5}{6,7},我想在使用tcl脚本中的sed关闭每个花括号后获得换行符 我试过了 exec /bin/sed -i {s/\\} \\{/\n/g} file.txt 但由于出现以下错误,它无法工作: Error: /bin/sed: -e expression #1, char 12: Unmatched \\{ 从{4,5}{6,7}我想要: 在tcl脚本中使用sed。我将用泛型\s替换空格,并将重点放在需要替换的内容上: exec /bin/sed -i

我在一个文本文件中有{4,5}{6,7},我想在使用tcl脚本中的sed关闭每个花括号后获得换行符

我试过了

exec /bin/sed -i {s/\\} \\{/\n/g} file.txt
但由于出现以下错误,它无法工作:

Error: /bin/sed: -e expression #1, char 12: Unmatched \\{ 
从{4,5}{6,7}我想要:

在tcl脚本中使用sed。

我将用泛型\s替换空格,并将重点放在需要替换的内容上:

exec /bin/sed -i s/}\s*{/\n/g file.txt

请注意,我现在没有tcl或sed可供测试。

您使用带有sed的POSIX BRE引擎,因为您没有通过-E或-r选项

这意味着要匹配文字{和},需要使用未转义的{和}。如果在POSIX BRE模式中转义一个{,它期望成对的}形成一个范围或限制量词。在常见的NFA正则表达式引擎中,{8}是一个量词,但在POSIX BRE中,{8}匹配文字{8}字符串

因此,只要使用

exec /bin/sed -i "s/} {/\n/g" file.txt
要匹配任何空格,请使用

exec /bin/sed -i "s/}[[:space:]]{/\n/g" file.txt

在Tcl中,这种特定的转换可能更容易在本地完成。在我看来,它是字符串映射的一个很好的候选者:


默认情况下,sed使用BRE,除非用反斜杠转义,否则大括号中没有任何特殊含义。因此,删除反斜杠是否尝试编辑一些JSON?请注意,单引号在Tcl中没有特殊意义。请改用双引号。
exec /bin/sed -i "s/}[[:space:]]{/\n/g" file.txt
# Read the data from the file; not needed if you already have it in Tcl
set f [open file.txt]
set data [read $f]
close $f

# Do the transform itself
set transformed [string map [list "\} \{" "\}\n\{"] $data]