下面的 awk
按原样执行,但它会重命名匹配 $p
(从每个文本文件中提取)的每个匹配文件中的字段,而不是添加 $x
这是要添加到目录中每个文件名的前缀(从重命名的 $1 开始)。每个 $x
后跟 _
文件名。我可以在 echo $p
中看到提取用于查找 $2
的正确值,但目录中的每个文件都未更改。并非 rename
中的每个文件都在该目录中,但它始终与 $p
匹配。也许有更好的方法,因为我不确定自己做错了什么。谢谢 :)。
重命名 制表符分隔
00-0000 File-01
00-0001 File-02
00-0002 File-03
00-0003 File-04
文件1
File-01_xxxx.txt
文件2
File-02_yyyy.txt
期望的输出
00-0000_File-01-xxxx.txt
00-0001_File-02-yyyy.txt
狂欢
for file1 in /path/to/folders/*.txt
do
# Grab file prefix
bname=`basename $file1` # strip of path
p="$(echo $bname|cut -d_ -f1,1)" # remove after second underscore
echo $p
# add prefix to matching file
awk -v var="$p" '$2~var{x=$1}(NR=x){print $x"_",$bname}' $file1 rename OFS="\t" > tmp && mv tmp $file1
done
最佳答案
这个脚本:
touch File-01-azer.txt
touch File-02-ytrf.txt
touch File-03-fdfd.txt
touch File-04-dfrd.txt
while read p f;
do
f=$(ls $f*)
mv ${f} "${p}_${f}"
done << EEE
00-0000 File-01
00-0001 File-02
00-0002 File-03
00-0003 File-04
EEE
ls -1
输出:
00-0000_File-01-azer.txt
00-0001_File-02-ytrf.txt
00-0002_File-03-fdfd.txt
00-0003_File-04-dfrd.txt
您可以使用文件作为输入 done < rename_map.txt
或 cat rename_map.txt | while
关于bash - awk 将从文件中提取的前缀添加到文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48389205/