我有文件输入 -
数据 -
A,B,C
"C=abv,O=12,D=gbl,DC=ad,F=fgi,G=c",abc123, "D=12gkh,F=gfkhl,H=jk67"
"C=abv,O=12,D=gbl,DC=adc",abc123, "D=ntms,F=jk4,H=ks9"
"C=abv,O=12,D=gbl,DC=ad,F=fgi,G=c",abc123,
预期产出
B,C
abc123,"D=12gkh,F=gfkhl,H=jk67"
abc123,"D=ntms,F=jk4,H=ks9"
abc123,
我想从第一列删除记录,但由于内部和外部字段分隔符相同而无法删除。
还想删除第三列值为空的行(如-abc123,)
尝试使用下面的sed解决方案,替换了开始和结束字符串并尝试删除字符串upto
cat file_name|sed -e :1 -e 's@\(<ex>[^(<ex>)]*\),\(.*<ex>\)@\1+\2@;t1'
cat file_|sed -e :1 -e 's@\(<ex>[^(</ex>)]*\)&\(.*</ex>\)@\1+\2@;t1'
答案 0 :(得分:1)
在awk中。用",
替换从开头到A,
的所有内容,然后将其删除:
$ awk '{sub(/^.+",/,"A,");sub(/^A,/,"")} 1' foo
B,C
abc123, "D=12gkh,F=gfkhl,H=jk67"
abc123, "D=ntms,F=jk4,H=ks9"
abc123,
答案 1 :(得分:0)
你可以使用rev和awk命令,如下所示;
rev file_name | awk -F, '{print $1","$2}' | rev
rev;一个或多个文件的反向行
-F param:用于输入字段分隔符
或
awk -F, '{print $(NF-1)","$NF}' file_name
NF设置为字段总数
例如
user@host $ rev test | awk -F, '{print $1","$2}' | rev
B,C
abc123, "nil"
abc123, "ntms"
abc123,
user@host $ awk -F, '{print $(NF-1)","$NF}' test
B,C
abc123, "nil"
abc123, "ntms"
abc123,