awk - 在文本文件每一行的最后一个 `/` 之后打印的 `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
解决方案
从您的示例来看,您不需要将文件名放入单独的文件中,只需更改您的 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
代表最后一个字段
推荐阅读
- css - CSS @media only 屏幕查询在 360px 下不起作用
- python - 检查熊猫数据框中列中的多个条件的最有效方法是什么?
- .htaccess - 如何使用 .htaccess 重定向整个站点,除了特定的 URL?
- javascript - 如何从谷歌地图div中跳过tabIndex并使用angular8从一个输入移动到另一个
- php - 如何仅更新数据库上的选择字段
- python - 如何将文本拆分为 N-gram 并获得它们的偏移量
- gis - wfs 事务后 Geoserver null 返回
- ruby - 在 chef 中嵌套自定义资源
- javascript - Google Appscript 中的第 3 方身份验证问题
- gradle - 如何解决 @SpringBootTest 导致“未找到给定包含的测试”的问题?