Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.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
Regex sed在一个系统上工作,但在另一个系统上不工作_Regex_Linux_Bash_Sed - Fatal编程技术网

Regex sed在一个系统上工作,但在另一个系统上不工作

Regex sed在一个系统上工作,但在另一个系统上不工作,regex,linux,bash,sed,Regex,Linux,Bash,Sed,这是我的输入文件 [root@localhost scripts]# cat ip6hdr.txt | xargs -n4 6000 0000 005C 3320 2001 0000 0000 0000 0000 0000 0000 0100 2001 0000 0000 0000 0000 0000 0000 0200 我想将文件第一行的最后两位数字,即20更改为00 我试过这个 cat ip6hdr.txt | xargs -n4 | sed '1,1s/\([0-9]*\)

这是我的输入文件

 [root@localhost scripts]# cat ip6hdr.txt | xargs -n4
 6000 0000 005C 3320
 2001 0000 0000 0000
 0000 0000 0000 0100
 2001 0000 0000 0000
 0000 0000 0000 0200
我想将文件第一行的最后两位数字,即
20
更改为
00

我试过这个

cat ip6hdr.txt | xargs -n4 | sed '1,1s/\([0-9]*\) \([0-9]*\) \([0-9]*\) \([0-9][0-9]\)\([0-9][0-9]\).*/\1 \2 \3 \400 /' 
以前它在ubuntu上运行良好,现在在bash脚本中不在fedora上运行

我没有理由解释为什么它在一个系统上工作而在另一个系统上不工作

 [root@localhost scripts]# sed --version
 GNU sed version 4.1.5
 Copyright (C) 2003 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
 to the extent permitted by law.

如果可能的话,给我一些建议。

你应该使用
[0-9a-fA-F]
而不是
[0-9]
你很可能缺少一个
-e
来表示你的sed表达式,而且Ubuntu sed是更新的、更宽松的。更简单的表达式可能会更好一些:

.... | sed -e '1s/[0-9][0-9]$/00/'
为什么要把事情复杂化

$ cat File
6000 0000 005C 3320
6000 0000 005C 3320

$ sed '1s/..$/00/;' File
6000 0000 005C 3300
6000 0000 005C 3320

检查您的sed版本,即
sed--version
。还有,为什么不使用复杂的表达式
sed'1s/.$/00/'
?祝你好运。你的问题是什么?sed版本涉及哪些内容?@Shelleter感谢和+1的简单版本。顺便说一句,你为什么要使用如此复杂的sed?为什么不改为
sed-e'1s/[0-9a-fA-F]{2}$/20/'
(我的观点是使用
$
将其粘贴到行尾)?即使Sun3
sed
也不需要
-e
。我只在复制/粘贴解决方案时使用它(几乎是这样);-)祝大家好运!