Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash awk-将负数转换为正数,反之亦然_Bash_Sed_Awk - Fatal编程技术网

Bash awk-将负数转换为正数,反之亦然

Bash awk-将负数转换为正数,反之亦然,bash,sed,awk,Bash,Sed,Awk,我有一个结构如下的文件: [05:58:10 08.12.1990] 125.441 [05:58:21 08.12.1990] -2.4158 ... [05:58:10 08.12.1990] -125.441 [05:58:21 08.12.1990] 2.4158 ... 其中,除了最后一列以外的所有内容都是某种时间码(每个文件都不同),我需要将最后一个数字转换为其相反的数字,如下所示: [05:58:10 08.12.1990] 125.441 [05:58:21 08.12.19

我有一个结构如下的文件:

[05:58:10 08.12.1990] 125.441
[05:58:21 08.12.1990] -2.4158
...
[05:58:10 08.12.1990] -125.441
[05:58:21 08.12.1990] 2.4158
...
其中,除了最后一列以外的所有内容都是某种时间码(每个文件都不同),我需要将最后一个数字转换为其相反的数字,如下所示:

[05:58:10 08.12.1990] 125.441
[05:58:21 08.12.1990] -2.4158
...
[05:58:10 08.12.1990] -125.441
[05:58:21 08.12.1990] 2.4158
...
我用awk试过,但由于我对awk的了解很差,所以我并没有完全做到这一点。 我首先尝试用以下方法添加负数:

awk '{$NF=" "; NF--; printf $0; NF++; if ($NF ~ /^[0-9].*/){printf "-"; print $NF}}'
但是因为我用NF-扔掉了最后一列,所以我不能用NF++取回它

在此方面的任何帮助都将不胜感激。谢谢。

试试:

awk '{$NF *= -1; print}'
尝试:

那么:

awk '  { if( $NF ~ /^-/ ){sub(/-/, "", $NF); print $0;} 
         else {sub(/^[1-9]*.[1-9]*/,"-"$NF,$NF); print $0}  } '


$ cat data 
[05:58:10 08.12.1990] 125.441
[05:58:21 08.12.1990] -2.4158
$ cat data | awk '  { if( $NF ~ /^-/ ){sub(/-/, "", $NF); print $0;} else {sub(/^[1-9]*.[1-9]*/,"-"$NF,$NF); print $0} }'
[05:58:10 08.12.1990] -125.441
[05:58:21 08.12.1990] 2.4158
编辑: 正如William Purssell所建议的,此脚本的改进版本:

cat data | awk '  { if( $NF ~ /^-/ ){sub(/-/, "", $NF); print $0;} 
         else { $NF="-"$NF; print $0}} '
[05:58:10 08.12.1990] -125.441
[05:58:21 08.12.1990] 2.4158
[05:59:30 08.13.2000] -0.94630008768741448848
那么:

awk '  { if( $NF ~ /^-/ ){sub(/-/, "", $NF); print $0;} 
         else {sub(/^[1-9]*.[1-9]*/,"-"$NF,$NF); print $0}  } '


$ cat data 
[05:58:10 08.12.1990] 125.441
[05:58:21 08.12.1990] -2.4158
$ cat data | awk '  { if( $NF ~ /^-/ ){sub(/-/, "", $NF); print $0;} else {sub(/^[1-9]*.[1-9]*/,"-"$NF,$NF); print $0} }'
[05:58:10 08.12.1990] -125.441
[05:58:21 08.12.1990] 2.4158
编辑: 正如William Purssell所建议的,此脚本的改进版本:

cat data | awk '  { if( $NF ~ /^-/ ){sub(/-/, "", $NF); print $0;} 
         else { $NF="-"$NF; print $0}} '
[05:58:10 08.12.1990] -125.441
[05:58:21 08.12.1990] 2.4158
[05:59:30 08.13.2000] -0.94630008768741448848

使用
sed

## Try to remove the negative sign.
s/\(\] \+\)-\(.*\)$/\1\2/

## If last substitution succeed, goto label 'a'.
ta

## If last substitution failed, there was not negative sign, so
## it is a positive number, I will have to add the sign just 
## before it.
s/\] \+/&-/

## Label 'a' at end of script. Here print line and read next one.
:a
script.sed的内容

## Try to remove the negative sign.
s/\(\] \+\)-\(.*\)$/\1\2/

## If last substitution succeed, goto label 'a'.
ta

## If last substitution failed, there was not negative sign, so
## it is a positive number, I will have to add the sign just 
## before it.
s/\] \+/&-/

## Label 'a' at end of script. Here print line and read next one.
:a
填充的内容

[05:58:10 08.12.1990] 125.441
[05:58:21 08.12.1990] -2.4158
像这样运行:

sed -f script.sed infile
结果:

[05:58:10 08.12.1990] -125.441                                                                                                                                                                                                               
[05:58:21 08.12.1990] 2.4158

使用
sed

## Try to remove the negative sign.
s/\(\] \+\)-\(.*\)$/\1\2/

## If last substitution succeed, goto label 'a'.
ta

## If last substitution failed, there was not negative sign, so
## it is a positive number, I will have to add the sign just 
## before it.
s/\] \+/&-/

## Label 'a' at end of script. Here print line and read next one.
:a
script.sed的内容

## Try to remove the negative sign.
s/\(\] \+\)-\(.*\)$/\1\2/

## If last substitution succeed, goto label 'a'.
ta

## If last substitution failed, there was not negative sign, so
## it is a positive number, I will have to add the sign just 
## before it.
s/\] \+/&-/

## Label 'a' at end of script. Here print line and read next one.
:a
填充的内容

[05:58:10 08.12.1990] 125.441
[05:58:21 08.12.1990] -2.4158
像这样运行:

sed -f script.sed infile
结果:

[05:58:10 08.12.1990] -125.441                                                                                                                                                                                                               
[05:58:21 08.12.1990] 2.4158

这可能适合您:

sed 's/ \([0-9.]*\)$/ -\1/;t;s/\(.*\)-/\1/' file
[05:58:10 08.12.1990] -125.441
[05:58:21 08.12.1990] 2.4158
实质上:

  • 如果最后一个字段用空格分隔,则在前面加上一个
    -
  • 否则,删除最后一个减号

    • 这可能适合您:

      sed 's/ \([0-9.]*\)$/ -\1/;t;s/\(.*\)-/\1/' file
      [05:58:10 08.12.1990] -125.441
      [05:58:21 08.12.1990] 2.4158
      
      实质上:

      • 如果最后一个字段用空格分隔,则在前面加上一个
        -
      • 否则,删除最后一个减号


      这似乎管用,但它使我的数字更接近。我有0.94630008768741448848,awk将其四舍五入到-0.9463。可以使用CONVFMT变量调整stringnumber转换。但我建议继续采用另一种解决方案,即@Wisent,这似乎有效,但它使我的数字达到了四舍五入。我有0.94630008768741448848,awk将其四舍五入到-0.9463。可以使用CONVFMT变量调整stringnumber转换。但我建议继续使用另一个解决方案@wisentThank获得另一个版本,但也有一个bug。。如果我想在0和1之间转换数字,我会得到错误的数字:我有:“[05:58:21 08.12.1990]0.4158”,我会得到[05:58:21 08.12.1990]-0.4158.4158,我明白了为什么,您使用的是1-9而不是0-9。现在可以了,谢谢!在else子句中,不需要sub。只需执行$NF=“-”$NF感谢提供另一个版本,但也有一个bug。。如果我想在0和1之间转换数字,我会得到错误的数字:我有:“[05:58:21 08.12.1990]0.4158”,我会得到[05:58:21 08.12.1990]-0.4158.4158,我明白了为什么,您使用的是1-9而不是0-9。现在可以了,谢谢!在else子句中,不需要sub。只需执行$NF=“-”$NFtxr看起来非常有趣,从未听说过。我会试着多读一些,谢谢!txr看起来很有趣,从未听说过。我会试着多读一些,谢谢!感谢您使用sed解决问题,但这里的问题是我不能确定时间码是否在括号中。例如,如果时间码放在组合括号中,这将不起作用。不过还是要谢谢你的主意!感谢您使用sed解决问题,但这里的问题是我不能确定时间码是否在括号中。例如,如果时间码放在组合括号中,这将不起作用。不过还是要谢谢你的主意!我必须说sed对我来说仍然有点神秘,但这确实有效,而且它也独立于时间码的格式(如awk)。我真的认为这对sed来说是不可能的。谢谢你证明我错了,我还有很多东西要学!我必须说sed对我来说仍然有点神秘,但这确实有效,而且它也独立于时间码的格式(如awk)。我真的认为这对sed来说是不可能的。谢谢你证明我错了,我还有很多东西要学!下面的答案很好,但要继续您的“我无法用NF++取回它”,那么,为什么不将它保存到一个特殊变量,即
      myNF=NF$NF=“”;NF-。。。。打印$(myNF).
      。祝你好运。下面的答案很好,但要继续你的“我无法用NF++取回它”,那么,为什么不将它保存到一个特殊变量,即
      myNF=NF$NF=“”;NF-。。。。打印$(myNF).
      。祝你好运