Bash字符串分裂



我有一个日志文件,我正在逐行读取。

可能的输入:

" 0:00 InitAuth: authauth_statusinitauth_cheaters1auth_tags1auth_notoriety1auth_groups auth_owners"

想要输出:

$TIME = 0:00
$TYPE = InitAuth:
$DATA = authauth_statusinitauth_cheaters1auth_tags1auth_notoriety1auth_groups auth_owners

$Output[0], $Output[1], $Output[2]不管它是1个数组还是3个变量

起初我想把这行分成3个变量,并使用空格分隔符,所以我试图搜索sh替换PHP命令explode(' ', $input, 3);,但后来我发现这样的行:

"1728:32ClientUserinfoChanged: 0 nThunderBirdt3r2tlf0 f1 f2 a0a1a2"
$TIME = 1728:32
$TYPE = ClientUserinfoChanged:
$DATA = 0 nThunderBirdt3r2tlf0 f1 f2 a0a1

Time和Type info之间没有空格。所以我想知道,我现在应该如何分割文本呢?

我也是Shell的新手,我正在搜索每一个可能的命令。

下面的内容可能会帮助您获得所需的输出:

sed -r 's/([0-9]+:[0-9]{2})([a-zA-Z ]+:)(.*)/$TIME:1n$TYPE:2n$DATA:3/'

测试:

[jaypal:~] echo "1728:32ClientUserinfoChanged: 0 nThunderBirdt3r2tlf0 f1 f2 a0a1a2" | gsed -r 's/([0-9 ]+:[0-9]{2})([a-zA-Z ]+:)(.*)/$TIME:1n$TYPE:2n$DATA:3/'
$TIME:1728:32
$TYPE:ClientUserinfoChanged:
$DATA: 0 nThunderBirdt3r2tlf0 f1 f2 a0a1a2
[jaypal:~] echo "0:00 InitAuth: authauth_statusinitauth_cheaters1auth_tags1auth_notoriety1auth_groups auth_owners" | gsed -r 's/([0-9 ]+:[0-9]{2})([a-zA-Z ]+:)(.*)/$TIME:1n$TYPE:2n$DATA:3/'
$TIME:0:00
$TYPE: InitAuth:
$DATA: authauth_statusinitauth_cheaters1auth_tags1auth_notoriety1auth_groups auth_owners
[jaypal:~]

所以这是我想要的,它的工作以及我需要的,它很好。:)

谢谢Jaypal。

TIME=`echo $LINE | sed -r 's/([0-9]+:[0-9]{2})(.*)/1/'`
TYPE=`echo $LINE | sed -r 's/([0-9]+:[0-9]{2})([a-zA-Z ]+:)(.*)/2/'`
DATA=`echo $LINE | sed -r 's/([0-9]+:[0-9]{2})([a-zA-Z ]+:)(.*)/3/'`

我猜有人会截断它:D

最新更新