首页 > 解决方案 > 在文本文件每一行的最后一个 `/` 之后打印的 `awk` 方法

问题描述

我需要一种在文本文件每一行awk的最后一行之后打印的方法。/我有一个包含文件目录的 txt 文件。

~/2500andMore.txt 文件

/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

这个想法是将所有位于一个目录中的文件复制回给定的示例。我打算做这样的事情来复制它们:

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

标签: awk

解决方案


从您的示例来看,您不需要将文件名放入单独的文件中,只需更改您的 while 循环:

while IFS= read -r all; do
    file=${all##*/}            # Or file=$(basename "$all")
    cp "$file" "$all"
done <~/2500andMore.txt

但是,如果您真的想要 中的文件名filename.txt,请使用:

awk -F / '{ print $NF }' ~/2500andMore.txt > ~/filename.txt

说明:“/”作为分隔符,$NF代表最后一个字段


推荐阅读