我有一个 csv 文件,其中有分隔符~@~~@~
,有时还有~@~
.同一组特殊字符~@~~@~
(我不知道有多少,但~@~~@~
*)出现在日期字段行的末尾。所以文件中的数据就像
ABC~@~~@~TUV~@~~@~XYZ~@~PQR~@~13470~@~Management~@~Non-Employee IJK~@~T~@~12345~@~12345~@[email protected]~@~B~@~~@~~@~~@~6789~@~09/25/2017 11:31:22.000000~@~09/25/2017 3:22:03.000000~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~~@~\n
我需要删除从“最后日期”字段到“行尾”的字符。我已经尝试过sed 's/~@~~@~~@~\n*//g' File name>Replacedfilename.txt
,但它最后保留了一组特殊字符。
答案1
... | sed 's/\(~@~\)*$//'
删除任意数量的~@~
行尾(当然假设\n
示例中的 代表行结束换行符,而不是文字反斜杠和小写 N。)
或者在支持扩展正则表达式的sed
s中:-E
... | sed -E 's/(~@~)+$//'
或者您可以awk -F'~@~'
同时使用和处理您需要的字段。
答案2
假设上述数据在文件中f1.txt
。我们可以使用 sed 命令并删除所有出现的~@~~@~
和~@~
。我已将其替换为空格。
sed -e 's/~@~~@~/#/g;s/~@~/#/g;s/# */ /g' f1.txt > output.txt
结果
ABC TUV XYZ PQR 13470 Management Non-Employee IJK T 12345 12345 [email protected] B 6789 09/25/2017 11:31:22.000000 09/25/2017 3:22:03.000000
答案3
cat file |sed 's/~@~~@~~@~//g' |sed 's/~@~$//'
第一个 sed 删除字符串~@~~@~
,第二个 sed 删除最后一个~@~