1

假设我有一个文件:

它的格式应该是:数字,string1,[string2],....

这里string1不应该包含',',因为我们使用','来分隔每一列但是由于某种原因,string1现在包含一些',',所以我们需要用其他符号替换它,例如'-'

1,aaa,bbb,ccc,[x,y,z],eee,fff,ggg
2,q,w,[x],f,g
3,z,[y],g,h
4,zzz,xxx,ccc,vvv,[z],g,h
....

应修改为:

1,aaa-bbb-ccc,[x,y,z],eee,fff,ggg
2,q-w,[x],f,g
3,z,[y],g,h
4,zzz-xxx-ccc-vvv,[z],g,h
....

什么是不编程的最佳方法,我的意思是我们只使用 awk、sed、vim 而不是 shell 编程、python、c++ 等

谢谢

4

3 回答 3

1

有点长,但你可以sed这样使用:

sed ':loop; s/\([0-9]\+,.*\)\([^,]*\),\([^,]*\)\(.*,\[\)/\1\2-\3\4/; t loop' \
     input_file

略短的一个:

sed ':loop; s/\([0-9]*,[^\[,]*\),\([^\[,]*,\[\)/\1-\2/; t loop' input_file

第二个说明:

loop while there are matches                   # :loop;
  1) find numbers followed by a comma,         #   \([0-9]*,
       followed by anything not comma or '[',  #   [^\[,]*\)
  2) find comma                                #   ,
  3) find anything not ',' or '['              #   \([^\[,]*
  4) followed by a ',' and '['                 #   ,\[\)/
  5) replace the whole thing with
       match of step 1 and '-' and matches 
       from steps 3-4                          #   /\1-\2/;
end loop

                                   # t loop
于 2012-07-19T03:35:53.750 回答
1
$ awk -F, 'BEGIN{OFS=FS} {two=$0;sub($1 FS,"",two);sub(/,[[].*/,"",two);gsub(/,/,"-",two); rest=$0;sub(/^[^[]*/,"",rest); print $1,two,rest}' input.txt 
1,aaa-bbb-ccc,[x,y,z],eee,fff,ggg
2,q-w,[x],f,g
3,z,[y],g,h
4,zzz-xxx-ccc-vvv,[z],g,h
$ 

让我们分解 awk 脚本以便于注释。

$ awk -F, '
  BEGIN { OFS=FS }
  {
    two=$0;                # Second field is based on the line...
    sub($1 FS,"",two);     # Remove the first field,
    sub(/,[[].*/,"",two);  # Remove everything from the [ onwards,
    gsub(/,/,"-",two);     # Replace commas in whatever remains.

    rest=$0;               # Last part of the line, after "two"
    sub(/^[^[]*/,"",rest); # Strip everything up to the [

    print $1,two,rest;     # Print it.
  }
' input.txt 
于 2012-07-19T03:47:13.780 回答
0

这可能对您有用(GNU sed):

sed -e 's/,\[/\n&/;h;s/\n.*//;s/,/-/2g;G;s/\n.*\n//' file
于 2012-07-19T06:11:38.593 回答