gpt4 book ai didi

linux - 如何在 bash 中将文本剪切成单独的行?

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:12 26 4
gpt4 key购买 nike

我有一个像这样的文本文件:

10/22/2013  00:11:12 ioy_I2           dtgfd14_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:11:19 ioy_I2           dtgfd24_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:11:26 ioy_I2           dtgfd23_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:11:32 ioy_J2           dtgfd13_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:11:39 ioy_J2           dtgfd14_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:11:46 ioy_J2           dtgfd24_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:11:53 ioy_J2           dtgfd23_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:12:00 ioy_L2           dtgfd13_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:12:08 ioy_L2           dtgfd14_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:12:15 ioy_L2           dtgfd24_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:12:22 ioy_L2           dtgfd23_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:12:29 ioy_N2           dtgfd13_1           TC  (OFF)weqw########��kjhk6           10/22/2013  00:12:37 ioy_N2           dtgfd14_1           TC  

我必须清除这个文件,原始文件是一个二进制文件,我想将它转换为日志文件,与此相同:

10/22/2013  00:11:12 ioy_I2           dtgfd14_1           TC  (OFF)weqw  kjhk6           
10/22/2013 00:11:19 ioy_I2 dtgfd24_1 TC (OFF)weqw kjhk6
10/22/2013 00:11:26 ioy_I2 dtgfd23_1 TC (OFF)weqw kjhk6
10/22/2013 00:11:32 ioy_J2 dtgfd13_1 TC (OFF)weqw kjhk6
10/22/2013 00:11:39 ioy_J2 dtgfd14_1 TC (OFF)weqw kjhk6
10/22/2013 00:11:46 ioy_J2 dtgfd24_1 TC (OFF)weqw

最佳答案

这看起来像是 sed 的工作。只需将其设置为在特定模式后插入换行符即可:

sed 's|\([0-1][0-9]/[1-31]/2013\)|\n\1|g' myfile > mynewfile

它是如何工作的:

sed                    the stream editor. Learn more with "man sed"s                      the sed command we're running in "substitute"\( \)                  designates a capture group so we can reference it with \1 later[0-1][0-9]/[1-31]/2013 this regex will match most dates. Modify it to suit your needs.\n\1                   replace the previous pattern with itself prefixed by a newlineg                      continue after the first match

请注意,我使用管道 (|) 而不是斜线 (/) 作为分隔符,这在 sed 中是典型的。这样我就不必转义日期正则表达式模式中的所有斜杠。

有大量关于正则表达式的资源,但我碰巧喜欢 this one .

关于linux - 如何在 bash 中将文本剪切成单独的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24209997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com