awk打印覆盖字符串 [英] awk print overwrite strings
问题描述
我在终端中使用awk时遇到问题. 我需要将一个组中的许多文件从实际目录移动到另一个目录,并且在文本文件中有必要文件的列表,如下所示:
I have a problem using awk in the terminal. I need to move many files in a group from the actual directory to another one and I have the list of the necessary files in a text file, as:
filename.txt
filename.txt
file1
file2
file3
...
我通常是数字:
paste filename.txt | awk '{print "mv "$1" ../dir/"}' | sh
并执行:
mv file1 ../dir/
mv file2 ../dir/
mv file3 ../dir/
它通常可以工作,但是现在该命令改变了其行为,awk覆盖了第一个字符串中的最后一个字符串../dir/
,从初始位置再次开始了打印命令,获得:
It usually works, but now the command changes its behaviour and awk overwrites the last string ../dir/
on the first one, starting again the print command from the initial position, obtaining:
../dire1 ../dir/
../dire2 ../dir/
../dire3 ../dir/
,当然不能执行. 发生了什么? 我该怎么解决?
and of course it cannot be executed. What's happened? How do I solve it?
推荐答案
您的输入文件包含回车符(\r
aka control-M
).在其上运行UNIX工具之前,先在其上运行dos2unix
.
Your input file contains carriage returns (\r
aka control-M
). Run dos2unix
on it before running a UNIX tool on it.
idk您正在使用粘贴的内容,无论如何您都不应该为此使用awk,这只是一个简单的shell脚本的工作,例如经过测试后,请删除echo
:
idk what you're using paste for though, and you should not be using awk for this at all anyway, it's just a job for a simple shell script, e.g. remove the echo
once you've tested this:
$ < file xargs -n 1 -I {} echo mv "{}" "../dir"
mv file1 ../dir
mv file2 ../dir
mv file3 ../dir
这篇关于awk打印覆盖字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!