我将通过一个例子尽可能地说明问题:
所以这个:
Column1,Column2,Column3,Column4
Unique,Entry2,Entry3,Entry4
Unique,Entry2,Entry3,Entry4
Unique,Entry2,Entry3,Entry4
会变成这样:
Column1,Column2,Column3,Column4
Unique-test,Entry2,Entry3,Entry4
Unique-test,Entry2,Entry3,Entry4
Unique-test,Entry2,Entry3,Entry4
对于我目录中的所有超过25,000个文件。
是否有可以实现此目的的bash脚本?
*编辑后来发现CSV以逗号分隔并被" s包围。此更改将在下面解决。
答案 0 :(得分:1)
find . -type f -name '*.csv' -exec sed -e '2,$s/,/-test,/' -i {} +
find
命令将在系统长时间内生成命令行,
sed -i
将执行 infile changes ,从第2行到文件结尾,替换第1个昏迷,
,-test,
find . -type f -name '*.csv' -exec sed -e '2,$s/",/-test",/' -i {} +
find . -type f -name '*.csv' -exec sed -e '2,$s/\(["\o47]\?,\)/-test\1/' -i {} +
这个可以使用CSV,如:
'Column1','Column2','Column3','Column4'
'Unique','Entry2','Entry3','Entry4'
'Unique','Entry2','Entry3','Entry4'
"Column1","Column2","Column3","Column4"
"Unique","Entry2","Entry3","Entry4"
"Unique","Entry2","Entry3","Entry4"
Column1,Column2,Column3,Column4
Unique,Entry2,Entry3,Entry4
Unique,Entry2,Entry3,Entry4
并且可以呈现:
'Column1','Column2','Column3','Column4'
'Unique-test','Entry2','Entry3','Entry4'
'Unique-test','Entry2','Entry3','Entry4'
"Column1","Column2","Column3","Column4"
"Unique-test","Entry2","Entry3","Entry4"
"Unique-test","Entry2","Entry3","Entry4"
Column1,Column2,Column3,Column4
Unique-test,Entry2,Entry3,Entry4
Unique-test,Entry2,Entry3,Entry4
答案 1 :(得分:0)
对于多样性,让我们看一下Home
的可能解决方案:
awk
更新:适用于引用列find /some/path -type f -name '*.csv' -exec awk -i inplace -v FS=, -v OFS=, '$1=$1"-test"' '{}' +
的简单修复:
"Unique","Entry2","Entry3","Entry4"