gpt4 book ai didi

bash - 如何删除unix中特定多个字段中的括号

转载 作者:行者123 更新时间:2023-11-29 09:37:34 25 4
gpt4 key购买 nike

我需要从一行中的特定字段(第 2、4、5、7 字段)中删除括号“{”,我尝试使用以下命令但没有帮助

文件中的数据

a,{1},hello{hi}hello,{+2A},{3},un{i}x,{4}

期望的输出

a,1,hello{hi}hello,+2A,3,un{i}x,4

尝试过 unix 命令

awk -v FS="," -v OFS="," 'BEGIN { split("2,4,5,7", F); } { for(N in F) sub("\\(\\)","", $(F[N])) }1' testfile | sed 's/,,/,/g'

最佳答案

你可以使用这个awk:

s='a,{1},hello{hi}hello,{2},{3},un{i}x,{4}'

awk -v k='2,4,5,7' 'function strip(n){gsub(/[{}]/, "", $n)}
BEGIN{FS=OFS=","; n=split(k, a, /,/)}{for (i=1; i<=n; i++) strip(a[i])} 1' <<< "$s"

输出:

a,1,hello{hi}hello,+2 A,3,un{i}x,4

关于bash - 如何删除unix中特定多个字段中的括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45938894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com