Bash oneliner with AWK runs in terminal but not in script

空扰寡人 提交于 2019-12-25 02:43:24

问题


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

and I want the following output:

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

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?

Thanks!


回答1:


With this awk it can be easier:

$ 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'[ ,]' sets two possible delimiters: space and comma.
  • print $2, $4"."$5 prints the 2nd, 4th and 5th fields based on those delimiters.

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

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.




回答2:


I guess your surroundings in the script are to blame, but the whole thing can be done using just read:

while IFS=' ,' read a b c d e
do
  echo "$b $d.$e"
done < inputfile

Maybe this solves the issue on a different level ;-)




回答3:


You aren't copying the temp file; you are just erasing your original file. I'm not sure why you aren't getting an error (you should be, because cp expects two arguments).

Instead of

cp tmp.csh > $file

use

cp tmp.csv "$file"


来源:https://stackoverflow.com/questions/19981592/bash-oneliner-with-awk-runs-in-terminal-but-not-in-script

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