bash awk 系统调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15823216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:32:45  来源:igfitidea点击:

awk system call

bashawk

提问by rares.urdea

I want to use awk and the system()function to move a couple of directories around.

我想使用 awk 和system()函数来移动几个目录。

I have a file that I want to process with awknames file.cfgwhich is organized in the following way:

我有一个文件,我想使用按以下方式组织的awk名称进行处理file.cfg

/path1 /path2 
/some_path /some_other_path 
and so on..

each first path is separated from the second path by a whitespace So here's how I did it:

每条第一条路径都与第二条路径用空格隔开所以这是我的做法:

awk '{system(mv -R " ")}' file.cfg

but it doesn't work and I get

但它不起作用,我明白了

sh: 0/home/my_user/path1: No such file or directory

But file.cfglooks like this:

file.cfg看起来像这样:

/home/my_user/path1 /home/my_user/path2

and there is no 0 before /home. So what am I missing here?

并且之前没有 0 /home。那么我在这里错过了什么?

回答by Joni

You have to quote the command you give to system:

你必须引用你给的命令system

awk '{system("mv -R "  " " )}' file.cfg

Currently mv -Ris interpreted as the value of variable mv minus the value of R, which is 0since neither is defined.

当前mv -R被解释为变量 mv 的值减去 R 的值,0因为两者都没有定义。

回答by sidoh

Why not just use xargs?

为什么不直接使用 xargs?

cat file.cfg | xargs -n 2 mv

This will pass tokens (separated by whitespace) from your file into mv in groups of two.

这会将标记(由空格分隔)从您的文件中以两个为一组传递到 mv 中。