subro subro - 6 days ago 6
Bash Question

merge lines until end-of-record marker is seen

Here is part of my data, I need new line characters to be removed from the lines, excluding those lines ending with a sequence matching the format

|HH:MM:SS
.

Here are the first two records. First record started with "Reset system" and second with "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


I need output some thing like below

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


Can some body please help me with UNIX commands.

Thank you.

Answer

In native bash, aiming for readability over terseness:

#!/usr/bin/env bash

# if we were passed a filename as an argument, read from that file
# otherwise, this script reads from stdin
[[ $1 ]] && exec <"$1"

# 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# }"
Comments