在不删除 csv 文件其余部分的情况下剪切 CSV 中的部分路径

Cut part of path in CSV without deleting rest of csv file

我有这样的 CSV 文件:

/users/my/temporaryprojects/project1/Assets/file.ttf,Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype
/users/my/temporaryprojects/project2/Assets/file2.ttf,Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype

我需要将路径更改为 users/my/temporaryprojects/project1 + 其余 CSV 数据、users/my/temporaryprojects/project2 + 其余 CSV 数据等...。我试图做不同的 bash 脚本,比如

cut -d'/' -f 5- newTTF-Projects-INFO.csv >> ONETTF-Projects-INFO.csv

但不幸的是,每次脚本都会删除第一个逗号分隔值之后的所有内容:

/users/my/temporaryprojects/project1
/users/my/temporaryprojects/project2

我需要它采用这种格式:

/users/my/temporaryprojects/project1, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype
/users/my/temporaryprojects/project2, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype

我需要缩短第一个文件的路径,并保留所有其他数据。我知道每次我只需要值的前 4 个部分。我整晚都在尝试使用“cut”、“sed”、“awk”来做这件事,但我没有得到结果。每个方法都在第一个逗号之后删除值。有人可以帮帮我吗?我没有找到我的问题的任何答案。要编辑的文件有很多行,如示例中的行。

使用sed

$ sed s'~\(\(/[^/]*\)\{4\}\)[^,]*,~, ~' input_file
/users/my/temporaryprojects/project1, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype
/users/my/temporaryprojects/project2, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype
  • backreferences-capable regex 这个准系统根本不需要引擎 awk-solution

  • 直接清理 FS OFS

代码

echo "${input}" | 

mawk NF=NF FS='[/][^,\/]*[/][^,\/]*[,]' OFS=', '

           or simply just

gawk NF=NF FS='[/][^,/]*[/][^,/]*[,]' OFS=', '

输出

/users/my/temporaryprojects/project1, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype
/users/my/temporaryprojects/project2, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype
awk -F, -v level="2" '
  function dirname(f,l) {
    s = ""; n=split(f,a,"/");
    for (i=2; i<=n-l; i++) {  s = s "/" a[i] }
    return s
  }
  function basename(f) {
    n=split(f,a,"/");return a[n]
  }
  BEGIN{ OFS=FS }
  {=dirname(,level); =" "}1
' input_file

/users/my/temporaryprojects/project1, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype
/users/my/temporaryprojects/project2, Arial Unicode MS,Regular,Arial Unicode MS,ArialUnicodeMS,Version 1.01x,Monotype