Thu_Jun_04_09:22:50_2020=abcd:some_string=Thu_Jun_04_09:39:54_2020=efgh:some_string=Thu_Jun_04_09:51:12_2020=ijkl:some_string,some_string
我有上面的输出,但是我想要下面的格式。
Thu_Jun_04_09:22:50_2020=abcd:some_string
Thu_Jun_04_09:39:54_2020=efgh:some_string
Thu_Jun_04_09:51:12_2020=ijkl:some_string,some_string
有什么办法可以根据日期拆分行。
假设您在中输入了内容file
,则可以执行以下操作(GNU sed):
$ sed 's/=/\n/2;P;D' file
Thu_Jun_04_09:22:50_2020=abcd:some_string
Thu_Jun_04_09:39:54_2020=efgh:some_string
Thu_Jun_04_09:51:12_2020=ijkl:some_string,some_string
在s/=/\n/2
将取代第二=
带\n
,一个换行符。然后,P;
将\n
在当前模式空间(当前正在读取的内容)中将所有内容打印到第一个,并且D
将清空模式空间,以便我们可以再次开始该过程。这些记录在man sed
:
D If pattern space contains no newline, start a normal new cycle
as if the d command was issued. Otherwise, delete text in the
pattern space up to the first newline, and restart cycle with
the resultant pattern space, without reading a new line of in‐
put.
P Print up to the first embedded newline of the current pattern
space.
或者,使用GNU grep
,您可以执行以下操作:
$ grep -oP '[^=]+=[^=]+' file
Thu_Jun_04_09:22:50_2020=abcd:some_string
Thu_Jun_04_09:39:54_2020=efgh:some_string
Thu_Jun_04_09:51:12_2020=ijkl:some_string,some_string
这将查找一段非=
字符,然后是a =
,然后是另一段non- =
。该-o
告诉grep来只打印线的匹配部分,所以结果是打印你所寻找的群体。
还有一种选择是使用awk
with=
作为输入字段分隔符,并且只打印成对的字段:
$ awk -F= '{for(i=2;i<=NF;i+=2){print $(i-1)"="$i}} ' file
Thu_Jun_04_09:22:50_2020=abcd:some_string
Thu_Jun_04_09:39:54_2020=efgh:some_string
Thu_Jun_04_09:51:12_2020=ijkl:some_string,some_string
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句