SED前pression,焦XX:因连字符范围无效年底错误 [英] sed expression , char xx: Invalid range end error caused by hyphen

查看:168
本文介绍了SED前pression,焦XX:因连字符范围无效年底错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个文件:

2014-08-01 20:13:17.666 xxxxxxxxxx
2014-08-01 20:13:17.666 xxxxxxxxxx
2014-08-01 20:13:17.666 xxxxxxxxxx
......

我试图用去除微秒sed的:

I am attempting to remove the microseconds using sed:

GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.

以下是与错误信息失败的sed:-e前pression#1,烧焦38:无效的范围到底

The following is failing with error message "sed: -e expression #1, char 38: Invalid range end"

sed 's/\([0-9][0-9\- :]*\)\.[0-9]\{3\}/\1/g' < a.csv

然而,六a.csv和搜索

However, vi a.csv and searching for

\([0-9][0-9\- :]*\)\.[0-9]\{3\}

正常工作。

的根本原因是逃脱连字符。如果我删除了逃跑连字符的sed不会抱怨,但它不会匹配预期的格局。我试图逃避连字符无济于事的不同方法。

The rootcause is the escaped hyphen. If I remove the escaped hyphen, sed won't complain but it won't match the intended pattern. I have tried different ways of escaping the hyphen to no avail.

解决方法是明确地写出整个日期时间格式如下:

The workaround is to explicitly write out the entire date-time format as follows:

sed 's/\([0-9][0-9]*-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]\)\.[0-9]\{3\}/\1/g'

解决方法似乎丑陋和繁琐。我不知道底层RE引擎是SED和vi不同。不过,我喜欢

The workaround seems ugly and cumbersome. I do realize the underlying RE engine is different between sed and vi. However, I like to


  1. 明白为什么连逃跑的连字符SED
  2. 失败
  3. 如何修改为RE SED,使其更加优雅。

相关,但不是由, sed的错误&QUOT解决;无效的范围到底&QUOT;

推荐答案

在字符类,连字符必须是第一个或最后一个。试图用一个反斜杠逃脱它不工作(,而增加了反斜杠对类)。

In a character class, the hyphen must be first or last. Attempting to escape it with a backslash does not work (and instead adds the backslash to the class).

有多个 SED 方言和工作方式不同其他多个正则表达式实现,但在这种情况下,诊断是相当琐碎。与此修复程序:

There are multiple sed dialects and multiple other regex implementations which work differently, but in this case, the diagnostic is fairly trivial. And the fix:

sed 's/\([0-9][-0-9 :]*\)\.[0-9]\{3\}/\1/' < a.csv

(我也删除了 / G 标志,因为它似乎是多余的在这里。当然,你必须每这种模式的行不超过一个发生?)

(I also removed the /g flag because it appears to be redundant here. Surely, you have no more than one occurrence per line of this pattern?)

这篇关于SED前pression,焦XX:因连字符范围无效年底错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆