我有一个像这样的文件:
$ some random
$ text
00ab2c3f03$ and more
random text
1a2bf04$ more text
blah blah
代码是这样的:
sed -ne 's/\(.*\)$ and.*/\1/p' "file.txt" > "output1.txt"
sed -ne 's/\(.*\)$ more.*/\1/p' "file.txt" > "output2.txt"
这给了我这个00ab2c3f03
和这个1a2bf04
因此,它提取从行开始到shell prompt
的任何内容,并将其存储在文件中,两次用于两个不同的实例。
问题是文件有时看起来如下所示:
/dir # some random
/dir # text
00ab2c3f03/dir # and more
random text
345fabd0067234234/dir # more text
blah blah
我想制造一个通用的萃取器
hex
数字的随机数量的随机non-hex
数据。但我对sed
不太在行,我自己想出一个简单的解决办法.
发布于 2014-06-16 00:26:48
我想你想要这样的输出,
$ cat file
$ some random
$ text
00ab2c3f03$ and more
random text
1a2bf04$ more text
blah blah
/dir # some random
/dir # text
00ab2c3f03/dir # and more
random text
345fabd0067234234/dir # more text
blah blah
$ sed -ne 's/\([a-f0-9]*\).* and more.*/\1/p' file
00ab2c3f03
00ab2c3f03
$ sed -ne 's/\([a-f0-9]*\).* more text.*/\1/p' file
1a2bf04
345fabd0067234234
您也可以尝试下面的GNU sed
命令。由于您的输入中存在/
,所以我将sed分隔符更改为~
,
$ sed -nr 's~([a-f0-9]*)\/*\$*.* and more.*~\1~p' file
00ab2c3f03
00ab2c3f03
$ sed -nr 's~([a-f0-9]*)\/*\$*.* more text.*~\1~p' file
1a2bf04
345fabd0067234234
解释:
([a-f0-9]*)
-捕获所有六位数并将其存储到一个组中。/
或$
符号可能就出现在十六进制数字之后,所以在捕获组之后,正则表达式应该是\/*\$*
(/
0次或更多次,$
0次或更多次)。and more
的行。more text
的行上工作,因为op希望两个输出位于两个不同的文件中。发布于 2014-06-16 00:52:28
这在我看来更好:
sed -nr 's#([[:xdigit:]]+)[$/].*#\1#p' file
https://stackoverflow.com/questions/24235210
复制相似问题