具有AWK的Bash oneliner在终端中运行,但不在脚本中运行 [英] Bash oneliner with AWK runs in terminal but not in script
问题描述
我有一个具有以下数据类型的文件:
I have a file with the following type of data:
4-11-11 12:59:01,C,26,668
4-11-11 12:59:31,C,26,668
4-11-11 13:00:01,C,26,668
4-11-11 13:00:31,C,26,668
4-11-11 13:01:01,C,26,668
4-11-11 13:01:31,C,26,668
,我想要以下输出:
12:59:01 26.668
12:59:31 26.668
13:00:01 26.668
13:00:31 26.668
13:01:01 26.668
13:01:31 26.668
这在以下行的术语中很好用,但是不在bash脚本中,它只是给我一个空文件。我添加了awk部分后,问题开始了。
This works just fine in the termal with the following line, but not in a bash script, it just gives me an empty file. The problem started after I added the awk part.
cut -d ' ' -f2 $file | cut -d ',' -f-1,3- | awk -f tmp.csv -F"," '{print $1 " " $2 "." $3}' | sed 's/"//' > tmp.csv
cp tmp.csv > $file
有人可以解释为什么这在脚本中不起作用吗?
Could anyone explain why this won't work in a script?
谢谢!
推荐答案
使用此 awk
会更容易:
$ awk -F'[ ,]' '{print $2, $4"."$5}' file
12:59:01 26.668
12:59:31 26.668
13:00:01 26.668
13:00:31 26.668
13:01:01 26.668
13:01:31 26.668
-
-F'[,]'
设置两个可能的定界符:空格和逗号。 -
print $ 2,$ 4。 $ 5
根据这些定界符打印第二,第四和第五字段。 -F'[ ,]'
sets two possible delimiters: space and comma.print $2, $4"."$5
prints the 2nd, 4th and 5th fields based on those delimiters.
关于您的脚本为何不起作用的原因,仅仅是因为您添加了 -f tmp.csv
Regarding why your script did not work, it is just because you added -f tmp.csv
unnecessarily.
$ cut -d ' ' -f2 a | cut -d ',' -f-1,3- | awk -F"," '{print $1 " " $2 "." $3}' | sed 's/"//'
12:59:01 26.668
12:59:31 26.668
13:00:01 26.668
13:00:31 26.668
13:01:01 26.668
13:01:31 26.668
也使用 -f tmp.csv
,然后使用> tmp.csv
这没有任何意义。请注意,文件不能同时用作stdin和stdout。
Also you use -f tmp.csv
and then > tmp.csv
which does not make sense. Note a file cannot be used as stdin and stdout at the same time.
这篇关于具有AWK的Bash oneliner在终端中运行,但不在脚本中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!