shantanuo
shantanuo

Reputation: 32326

create a csv file from file path

I need to find complete path of files in current directory and then save it to .csv file with path,folder_name

The foler_name is the last directory for e.g. basics, pca and forked in this example...

# find .

./pandas_examples/basics/Skin_NoSkin.ipynb
./pandas_examples/basics/data_visual.ipynb
./pandas_examples/pca/README.md
./pandas_examples/pca/PCA-tSNE-AE.ipynb
./pandas_examples/forked/pandas_accidents.py

Expected output:

./pandas_examples/basics/Skin_NoSkin.ipynb, basics
./pandas_examples/basics/data_visual.ipynb, basics
./pandas_examples/pca/README.md, pca
./pandas_examples/pca/PCA-tSNE-AE.ipynb, pca
./pandas_examples/forked/pandas_accidents.py, forked

This format is needed for keras machine learning task.


Update:

This seems to work if the target is found in the third column:

cat path.txt | cut -d '/' -f3 > target.txt
paste -d "," path.txt target.txt

Is there any other way?

Upvotes: 0

Views: 90

Answers (2)

hek2mgl
hek2mgl

Reputation: 158010

You may just use this:

find . | awk -F/ '{print $0","$3}'

Upvotes: 3

Luuk
Luuk

Reputation: 14929

find . | awk -F "/" 'BEGIN{ OFS=", " }{ print $0, $(NF-1) }'

-F "/" the input is separated by "/"

OFS=", " the output is separated by a comma and a space

print $0, $(NF-1) print the inputline ($0) and the last, but one, field ($(NF-1))

Upvotes: 3

Related Questions