awk print overwrite strings

穿精又带淫゛_ 提交于 2019-12-13 21:52:20

问题


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

file1
file2
file3
...

I usually digit:

paste filename.txt | awk '{print "mv "$1" ../dir/"}' | sh

and it executes:

mv file1 ../dir/
mv file2 ../dir/
mv file3 ../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?


回答1:


Your input file contains carriage returns (\r aka control-M). Run dos2unix on it before running a UNIX tool on it.

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


来源:https://stackoverflow.com/questions/39902220/awk-print-overwrite-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!