Home > database >  Bash script deletes first row in csv during editing with 'cut'
Bash script deletes first row in csv during editing with 'cut'

Time:06-15

I have little problem with my bash script. The script is cloning repositories to my local folder and then search for files with specified extensions in those repositories (results are CSVtemporary2/ASSETS-LIST-"$dir".csv) , then edits results (cuts first 4 elements of paths, results are:/CSVtoGD2/"$dir".csv)

In the CSV there are paths to files in every single row ex. /users/krzysztofpaszta/temporaryprojects/gamex/car1.png /users/krzysztofpaszta/temporaryprojects/gamex/sound1.mp3 (...)

Every part of the script is working fine but after cutting the first 4 elements of paths the first row is deleted.. I do not know why this is happening.

So results should be:

/car1.png

/sound1.mp3 (...)

But results are:

/sound1.mp3 (...)

I do not know why this is happening.

So in other words, files CSVtemporary2/ASSETS-LIST-"$dir".csv are fine but then, files /CSVtoGD2/"$dir".csv have first row deleted.. Someone has idea why this is happening?

#!/bin/bash
rm -vrf /Users/krzysztofpaszta/CSVtoGD2/*
rm -vrf /Users/krzysztofpaszta/CSVtemporary2/*
cd /Users/krzysztofpaszta/temporaryprojects
for repo in $(cat /users/krzysztofpaszta/repolinks.csv); do
    git clone "$repo"
    dir=${repo##*/}
    find /users/krzysztofpaszta/temporaryprojects/"$dir" -name "*.fnt" -o -name "*.png" -o -name "*.ttf" -o -name "*.asset" -o -name "*.jpeg" -o -name "*.tga" -o -name "*.tif" -o -name "*.bmp" -o -name "*.jpg" -o -name "*.fbx" -o -name "*.prefab" -o -name "*.flare" -o -name "*.ogg" -o -name "*.wav" -o -name "*.anim" -o -name "*.mp3" -o -name "*.tiff" -o -name "*.otf" -o -name "*.hdr" >> /users/krzysztofpaszta/CSVtemporary2/ASSETS-LIST-"$dir".csv
    while read in ; do
      cut -d'/' -f6- >> /users/krzysztofpaszta/CSVtoGD2/"$dir".csv #| awk 'BEGIN{print"//"}1' - adding first empty row is not the solution, first row with text is still deleted 
    done < /users/krzysztofpaszta/CSVtemporary2/ASSETS-LIST-"$dir".csv
done
#rm -vrf /Users/krzysztofpaszta/temporaryprojects/*
#echo Repo deleted

CodePudding user response:

Your syntax

while read in ; do
    cut -d'/' ^f6- >> /users/krzysztofpaszta/CSVtoGD2/"$dir".csv
done < /users/krzysztofpaszta/CSVtemporary2/ASSETS-LIST-"$dir".csv

will not work as you expect.

The read command reads the first line of the csv file assigning a variable in to it. Then the remaining lines are fed to cut command via stdin.

Instead you can say;

while IFS= read -r line; do
    cut -d'/' ^f6- <<< "$line" >> /users/krzysztofpaszta/CSVtoGD2/"$dir".csv
done < /users/krzysztofpaszta/CSVtemporary2/ASSETS-LIST-"$dir".csv

Actually you don't even need to use the while loop:

cut -d'/' ^f6- < /users/krzysztofpaszta/CSVtemporary2/ASSETS-LIST-"$dir".csv > /users/krzysztofpaszta/CSVtoGD2/"$dir".csv

Besides there are many points to be improved:

  • There may be a typo in either of the pathnames /Users or /users.
  • You can use the while .. read .. loop instead of for repo in $(cat path/to.csv)
  • You don't need to create a temporary file for the output of find. You can feed the output directly to cut command via a pipeline.
  • Related