Concatenating multiple lines to one line if match pattern

Hi all,

I've been working on a script which I have hit a road block now. I have written a script using sed to extract the below data and pumped into another file:

Severity............: MAJORWARNING
Summary:
System temperature is out of normal range.
Severity............: MAJORWARNING
Summary:
Adapter at hardware path 0/4/1/0 : Received an interrupt indicating and
Elastic Store Error Storm
Severity............: MAJORWARNING
Summary:
Adapter at hardware path 0/3/1/0 : Received an interrupt indicating and
Elastic Store Error Storm
Severity............: MAJORWARNING
Summary:
System temperature is out of normal range.

I want to format it to look like this:
"Severity:.......... Summary:........"
"Severity:.......... Summary:........"
"Severity:.......... Summary:........"
"Severity:.......... Summary:........"

so that each ocurance will be in one line

Any ideas? Hope someone can help me quickly.

Thanks
phixsius.

With GNU Awk:

awk 'NF&&$1=RS$1'  RS="Severity" filename

Hi radoulov,

I ran the line, however this was the output:

Severityeverity............: MAJORWARNING
Severityummary: Adapter at hardware path 0/3/1/0 : Received an interrupt indicating and Elastic
Severitytore Error
Severitytorm
Severityeverity............: MAJORWARNING
Severityummary: Adapter at hardware path 0/4/1/0 : Received an interrupt indicating and Elastic
Severitytore Error
Severitytorm
Severityeverity............: MAJORWARNING
Severityummary:
Severityystem temperature is out of normal range.

I said GNU Awk ...

Given your sample this should work with non-Gnu Awk:

awk 'ORS=/:/?FS:RS' filename

Use nawk or /usr/xpg4/bin/awk on Solaris.

Hey mate,

Thanks alot for your suggestions. Unfort, i'm sorry, its still doesnt produce the result I'm looking...:frowning:

Think there are any other logics to achieving this?

sed -n '
:a
/Sev/ {
N
/Sev.*Sev/ !{
s/\n/ /
ta
}
P
D
}' infile

is probably close :slight_smile:

Why guess? Copy/paste the output from your terminal
so we can see what's wrong with the command(s).

or the really hacky:

echo `tr "\n" " " <infile` | sed 's/Severity/%%Severity/g' | tr -s "%" "\n"

:cool::eek:

Hey Tytalus,

Fantastic sed codes!...Just what I needed...:D...works in my own environment.

Will test it in production tmr!..

Thanks a bunch!

Cheers

Thanks for all your help mate!...appreciate it..:slight_smile:

Hm ...,
you're welcome :slight_smile:

Anyway,
given that the tr .. sed .. tr solution worked,
with nawk, gawk etc.

awk 'NR == 1 { 
  printf "%s", $0 FS
  next
} {
  printf "%s", /^Severity/ ? RS $0 FS : $0
} END {
  print ""
}' filename

For old, old awk's:

awk 'NR == 1 { 
  printf "%s ", $0 
  next 
} { 
  if ($0 ~ /^Severity/)
    printf "%s ",  RS $0
  else
    printf "%s", $0 
} END { 
  print "" 
}' filename

Hi,

Follow one is a little complicated, but should works. Please try.

sed '/Severity/i\
aa' filename | sed 's/^aa$//' > filename.temp
nawk 'BEGIN{
FS="\n"
RS=""
}
{
for (i=1;i<=NF;i++)
printf("%s ",$i)
print ""
}' filename.temp
rm filename.temp