Home > other >  Cut part of path in CSV without deleting rest of csv file
Cut part of path in CSV without deleting rest of csv file

Time:05-24

I have CSV file which looks like that:

/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

I need to change the path to users/my/temporaryprojects/project1 rest of CSV data, users/my/temporaryprojects/project2 rest of CSV data etc... . I was trying to do different bash scripts like

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

But unfortunately every time the script is deleting everything which is after the first comma separated value:

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

I need it to be in this format:

/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

I need to shorter the path to file which is on the first place and leave as it is every other data. I know that every time I need only 4 first parts of the value. I was trying to do this all night long with "cut", "sed", "awk" but I don't have my result. Every method is deleting values after the first comma. Could somebody help me? I did not find any answer to my question. The file to edit has a lot of rows like the one in example.

CodePudding user response:

Using sed

$ sed s'~\(\(/[^/]*\)\{4\}\)[^,]*,~\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
  • Related