Reputation: 1
I'm in need of an awk
method to print after the last /
on every line of a text file. I've got a txt file which includes file directories.
~/2500andMore.txt file
/Volumes/MCU/_ 0 _ Mt Cabins Ut/Raw/Log Cabin on The Stream/DJI_0003.JPG
/Volumes/MCU/_ 0 _ Mt Cabins Ut/Raw/DJI_0022.jpg
/Volumes/MCU/_ 0 _ Mt Cabins Ut/PTMAD/Insta/RAW/IMG_1049.jpg
The idea is to copy files that are all on one directory back to the example given. I'm planning to do something like this to copy them back:
awk '{print <after the last '/' of the line>}' ~/2500andMore.txt > ~/filename.txt
cd file-directory
while IFS= read -r file && IFS= read -r all <&3; do cp $file $all; done <~/filename.txt 3<~/2500andMore.txt
Upvotes: 0
Views: 38
Reputation: 6144
Judging by your example, you don't need to put the filenames into a separate file, just change your while loop:
while IFS= read -r all; do
file=${all##*/} # Or file=$(basename "$all")
cp "$file" "$all"
done <~/2500andMore.txt
But if you really want the filenames in filename.txt
, then use:
awk -F / '{ print $NF }' ~/2500andMore.txt > ~/filename.txt
Explanation: "/" is used as a separator and $NF
represents the last field
Upvotes: 1