Bash oneliner with AWK runs in terminal but not in

2019-08-22 03:19发布

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!

标签: bash awk cut
3条回答
▲ chillily
2楼-- · 2019-08-22 03:37

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 ;-)

查看更多
Viruses.
3楼-- · 2019-08-22 03:44

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.

查看更多
混吃等死
4楼-- · 2019-08-22 03:54

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"
查看更多
登录 后发表回答