合并行直到看到记录结束标记
merge lines until end-of-record marker is seen
这是我的部分数据,我需要从行中删除换行符,不包括那些以与格式 |HH:MM:SS
.
匹配的序列结尾的行
这是前两条记录。第一条记录以 "Reset system" 开头,第二条记录以 "Collaborator informs".
开头
Reset system password SISMED WE|Collaborator requests password reset of SISMED WEB system.
Login: John Doe
Nome: Jackie
Locat: D. XYZ – UA ABC Al
Setor/Depto: Administration
Floor: 1st
Tel./Ramal: 358-108|14/01/2015 |11:23:22
Collaborator informs that he can not open archiv ... |Collaborator informs you that you can not open files
Path: \abc\def\ghi\jkl\mno
File: ESCALAS.xls
Name: Hutch cock
Locat: D. Al Mo
Setor/Depto: Hos
Floor: 2nd
Tel./Ramal: 1521
IP: 1.5.2.14|14/01/2015 |11:26:21
我需要输出类似下面的东西
Reset system password SISMED WE|Collaborator requests password reset of SISMED WEB system.Login: John Doe Nome: Jackie Locat: D. XYZ – UA ABC Al Setor/Depto: Administration Floor: 1st Tel./Ramal: 358-108|14/01/2015 |11:23:22
Collaborator informs that he can not open archiv ... |Collaborator informs you that you can not open files Path: \abc\def\ghi\jkl\mno File: ESCALAS.xls Name: Hutch cock Locat: D. Al Mo Setor/Depto: Hos Floor: 2nd Tel./Ramal: 1521 IP: 1.5.2.14|14/01/2015 |11:26:21
有人可以帮我使用 UNIX 命令吗?
谢谢。
在原生 bash 中,旨在提高可读性而非简洁性:
#!/usr/bin/env bash
# if we were passed a filename as an argument, read from that file
# otherwise, this script reads from stdin
[[ ]] && exec <""
# ERE-syntax regex matching end-of-record marker
end_of_record_re='[|][[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}[[:space:]]*$'
buffer='' # start out with an empty buffer
while IFS= read -r line; do # while we can, read a line.
if ! [[ $line =~ $end_of_record_re ]]; then # unless it has an end marker...
buffer+=" $line" # ...add to our buffer, preceded by a space
else # if the line has an end marker...
printf '%s\n' "${buffer# }${line}" # ...print buffer except for first space
buffer= # ...and reset the buffer to be empty
fi
done
# finally, if we have trailing content, print it out.
[[ $buffer ]] && printf '%s\n' "${buffer# }"
这是我的部分数据,我需要从行中删除换行符,不包括那些以与格式 |HH:MM:SS
.
这是前两条记录。第一条记录以 "Reset system" 开头,第二条记录以 "Collaborator informs".
开头Reset system password SISMED WE|Collaborator requests password reset of SISMED WEB system.
Login: John Doe
Nome: Jackie
Locat: D. XYZ – UA ABC Al
Setor/Depto: Administration
Floor: 1st
Tel./Ramal: 358-108|14/01/2015 |11:23:22
Collaborator informs that he can not open archiv ... |Collaborator informs you that you can not open files
Path: \abc\def\ghi\jkl\mno
File: ESCALAS.xls
Name: Hutch cock
Locat: D. Al Mo
Setor/Depto: Hos
Floor: 2nd
Tel./Ramal: 1521
IP: 1.5.2.14|14/01/2015 |11:26:21
我需要输出类似下面的东西
Reset system password SISMED WE|Collaborator requests password reset of SISMED WEB system.Login: John Doe Nome: Jackie Locat: D. XYZ – UA ABC Al Setor/Depto: Administration Floor: 1st Tel./Ramal: 358-108|14/01/2015 |11:23:22
Collaborator informs that he can not open archiv ... |Collaborator informs you that you can not open files Path: \abc\def\ghi\jkl\mno File: ESCALAS.xls Name: Hutch cock Locat: D. Al Mo Setor/Depto: Hos Floor: 2nd Tel./Ramal: 1521 IP: 1.5.2.14|14/01/2015 |11:26:21
有人可以帮我使用 UNIX 命令吗?
谢谢。
在原生 bash 中,旨在提高可读性而非简洁性:
#!/usr/bin/env bash
# if we were passed a filename as an argument, read from that file
# otherwise, this script reads from stdin
[[ ]] && exec <""
# ERE-syntax regex matching end-of-record marker
end_of_record_re='[|][[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}[[:space:]]*$'
buffer='' # start out with an empty buffer
while IFS= read -r line; do # while we can, read a line.
if ! [[ $line =~ $end_of_record_re ]]; then # unless it has an end marker...
buffer+=" $line" # ...add to our buffer, preceded by a space
else # if the line has an end marker...
printf '%s\n' "${buffer# }${line}" # ...print buffer except for first space
buffer= # ...and reset the buffer to be empty
fi
done
# finally, if we have trailing content, print it out.
[[ $buffer ]] && printf '%s\n' "${buffer# }"