Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Shell 文本处理-根据字符将第一列拆分为两列_Shell_Awk_Sed_Text Processing_Cut - Fatal编程技术网

Shell 文本处理-根据字符将第一列拆分为两列

Shell 文本处理-根据字符将第一列拆分为两列,shell,awk,sed,text-processing,cut,Shell,Awk,Sed,Text Processing,Cut,根据字符将文件中的第一列拆分为两列。 括号()内的数据应移到新列中删除括号 给定csv文件: Col1(col2),col3,col4,col5 a(23),12,test(1),test2 b(30),15,test1(2),test3 预期文件: Col1,col2,col3,col4,col5 a,23,12,test(1),test2 b,30,15,test1(2),test3 我尝试了下面的代码。我无法提取括号之间的数据,而且每次出现“()”都需要提取数据 请选择: $ sed

根据字符将文件中的第一列拆分为两列。 括号()内的数据应移到新列中删除括号

给定csv文件:

Col1(col2),col3,col4,col5
a(23),12,test(1),test2
b(30),15,test1(2),test3
预期文件:

Col1,col2,col3,col4,col5
a,23,12,test(1),test2
b,30,15,test1(2),test3
我尝试了下面的代码。我无法提取括号之间的数据,而且每次出现“()”都需要提取数据

请选择:

$ sed 's/(\([^)]*\))/,\1/' file
Col1,col2,col3,col4,col5
a,23,12,test(1),test2
b,30,15,test1(2),test3

$ sed 's/(/,/; s/)//' file
Col1,col2,col3,col4,col5
a,23,12,test(1),test2
b,30,15,test1(2),test3

最后2个参数分别要求gensub()和第3个参数使用GNU awk进行匹配()。还有其他选择。

选择:

$ sed 's/(\([^)]*\))/,\1/' file
Col1,col2,col3,col4,col5
a,23,12,test(1),test2
b,30,15,test1(2),test3

$ sed 's/(/,/; s/)//' file
Col1,col2,col3,col4,col5
a,23,12,test(1),test2
b,30,15,test1(2),test3

最后2个参数分别要求gensub()和第3个参数使用GNU awk进行匹配()。还有其他选择。

为了完整性

posix外壳

while IFS= read -r line; do
    car=${line%%)*}
    caar=${car%%(*}
    cdar=${car##*(}
    cdr=${line#*)}
    printf '%s\n' "$caar,$cdar$cdr"
done < file
而IFS=read-r行;做
car=${line%%)*}
caar=${car%%(*}
cdar=${car###*(}
cdr=${line#*)}
printf'%s\n'$caar,$cdar$cdr“
完成<文件

我认为你不能单独使用
cut
来解决问题。

为了完整性

posix外壳

while IFS= read -r line; do
    car=${line%%)*}
    caar=${car%%(*}
    cdar=${car##*(}
    cdr=${line#*)}
    printf '%s\n' "$caar,$cdar$cdr"
done < file
当IFS=read-r行时;执行
car=${line%%)*}
caar=${car%%(*}
cdar=${car###*(}
cdr=${line#*)}
printf'%s\n'$caar,$cdar$cdr“
完成<文件


我认为您不能单独使用
cut
解决问题。

请您再尝试一个
sed
解决方案,这可能会对您有所帮助

sed 's/\([^(]*\)(\([^)]*\))\(.*\)/\1,\2\3/'  Input_file

请您再试一种可能对您有帮助的
sed
解决方案

sed 's/\([^(]*\)(\([^)]*\))\(.*\)/\1,\2\3/'  Input_file

awk-F“[(),][+”'$1=$1'OFS=“,”file
?@WiktorStribiżew仅在需要时使用操作的结果作为条件,否则会混淆代码并引入潜在的bug。例如,在这种情况下,当
$1
0
-write
{$1=$1}时,脚本将失败1
而不仅仅是
$1=$1
@EdMorton我没有写:)它是复制/粘贴。我只关注字段分隔符模式。@WiktorStribiżew我不清楚您没有建议一个完整的解决方案,所以OP可能也没有意识到。
awk-F“[(),]”+“$1=$1 OFS=“,”文件
?@WiktorStribiżew仅在需要时使用操作的结果作为条件,否则会混淆代码并引入潜在的bug。例如,在这种情况下,当
$1
0
-write
{$1=$1}1
而不是
$1=$1
@EdMorton我没有写它:)时,脚本将失败。它是复制/粘贴。我只关注字段分隔符模式。@WiktorStribiżew我不清楚您没有提出完整的解决方案,因此OP可能也没有意识到。如果您解释了每个解决方案,您可以大大改进您的答案。纯代码的答案对这么多社区来说没有那么大的价值。@WiktorStribiżew我完全不同意。通过查找手册页了解这些琐碎的脚本,OP将学到比我为每个脚本编写解释多得多的东西。教一个人钓鱼……如果你解释了每一个解决方案,你可以大大提高你的答案。纯代码的答案对这么多社区来说没有那么大的价值。@WiktorStribiżew我完全不同意。通过查找手册页了解这些琐碎的脚本,OP将学到比我为每个脚本编写解释多得多的东西。教一个人钓鱼。。。