Help required on joining one line above & below to the pattern matched string line.

Hi Experts,

Help needed on joining one line above & below to the pattern matched string line.

The input file, required output is mentioned below

[Input file

    ABCD   DEFG5 42.0.1-63.38.31 
       KKKK  iokl     IP Connection Available
   

    ABCD    DEFG5 42.0.1-63.38.31 
       SYSTEM        INFO    REPT COND: system alive
                 88899   POIREe QWERT POIEFVGH

   ABCD   DEFG5 42.0.1-63.38.31 
      SLK A   FFFFFF REPT-LINK-CGST: threshold level 0 to 1
                 SC=00   LLI=mmmmm                 CLASS=SL
              
   
    ABCD   DEFG5  42.0.1-63.38.31 
       IP7CONN ooooooo     Retrans
   
   ABCD   DEFG5 42.0.1-63.38.31 
        SLK A   FFFFFF RCVRY-LINK-CGST: threshold has cleared
                 SC=00   LLI=mmmmm                 CLASS=SL
                 
    ABCD  DEFG5 42.0.1-63.38.31 
       SLK  A   pcnbod     REPT-LINK-CGST: threshold level 0 to 1
                 SC=01   LLI=pcnbod                     CLASS=VL
                 
    ABCD   DEFG5  42.0.1-63.38.31 
           IP7CONN ooooooo     Retrans

    
    ABCD  DEFG5 42.0.1-63.38.31 
        SLK  A   pcnbod     RCVRY-LINK-CGST: threshold has cleared
                 SC=01   LLI=pcnbod                     CLASS=VL

Output

        ABCD   DEFG5 42.0.1-63.38.31        SLK A   FFFFFF REPT-LINK-CGST: threshold level 0 to 1                  SC=00   LLI=mmmmm                 CLASS=SL
                     
        ABCD   DEFG5 42.0.1-63.38.31          SLK A   FFFFFF RCVRY-LINK-CGST: threshold has cleared                  SC=00   LLI=mmmmm                 CLASS=SL                  
        
        ABCD  DEFG5 42.0.1-63.38.31         SLK  A   pcnbod     REPT-LINK-CGST: threshold level 0 to 1                  SC=01   LLI=pcnbod                     CLASS=VL
                     
        ABCD  DEFG5 42.0.1-63.38.31          SLK  A   pcnbod     RCVRY-LINK-CGST: threshold has cleared                  SC=01   LLI=pcnbod                     CLASS=VL

I have tried below command, but it's not working

nawk '/threshold/{print kar ;print $0;getline;print }{kar=$0}' filename |  nawk '{if (NR % 3) printf("%s", $0); else printf(" %s\n", $0)}' 

Advance Thanks :slight_smile:

Hi

awk '/threshold/{y=$0;getline; print x,y,$0;next;}{x=$0;}' file

Guru.

One thing I would say, is to do it all in one process rather than having one nawk piped into another nawk process. Depending upon how many lines of input this code needs to go through you'd could have a substantial savings in processing time.

This works for me (I don't have nawk but it should work for you):

 
awk 'BEGIN {RS=""}
/threshold/ { gsub(/\n/," ") ; print}' filename

If your data actually does have blank lines between each "record" as you've displayed it, it should work. If not ... back to the drawing board! :slight_smile:

Thanks to guruprasadpr & rwuerth.

I have tried both of the commands, but output is not comming as required.

Kindly help me :slight_smile:

Can you post the commands and your output?

Hi.

With common commands in Section 2 of this script:

#!/usr/bin/env bash

# @(#) s1	Demonstrate extract +-1, combine,

# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p printf specimen grep paste sed
set -o nounset
pe

FILE=${1-data1}

# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

# Section 2, solution.
pl " Results:"
grep -A1 -B1 'threshold' $FILE |
tee f1 |
grep -v '^--$' |
tee f2 |
paste -d" " - - - |
tee f3 |
sed 's/$/\n/' |
tee t1

# Section 3, post-solution, check results, clean-up, etc.
n1=$(wc -l <expected-output.txt)
n2=$(wc -l < t1)
pl " Comparison of $n2 created lines with $n1 lines of desired results:"
if [ ! -f expected-output.txt -o ! -s expected-output.txt ]
then
  pe " Comparison file \"expected-output.txt\" zero-length or missing."
  exit
fi
if cmp expected-output.txt t1
then
  pe " Passed -- files have same content."
else
  pe " Failed -- files not identical -- detailed comparison follows."
  if diff -B -b expected-output.txt t1
  then
    pe " Passed by ignoring whitespace differences."
  fi
fi

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
printf - is a shell builtin [bash]
specimen (local) 1.17
GNU grep 2.5.3
paste (GNU coreutils) 6.10
GNU sed version 4.1.5

 || start [ first:middle:last ]
Edges: 5:0:5 of 32 lines in file "data1"

ABCD DEFG5 42.0.1-63.38.31
KKKK iokl IP Connection Available


   ---


ABCD DEFG5 42.0.1-63.38.31
SLK A pcnbod RCVRY-LINK-CGST: threshold has cleared
SC=01 LLI=pcnbod CLASS=VL
 || end

-----
 Results:
ABCD DEFG5 42.0.1-63.38.31 SLK A FFFFFF REPT-LINK-CGST: threshold level 0 to 1 SC=00 LLI=mmmmm CLASS=SL

ABCD DEFG5 42.0.1-63.38.31 SLK A FFFFFF RCVRY-LINK-CGST: threshold has cleared SC=00 LLI=mmmmm CLASS=SL

ABCD DEFG5 42.0.1-63.38.31 SLK A pcnbod REPT-LINK-CGST: threshold level 0 to 1 SC=01 LLI=pcnbod CLASS=VL

ABCD DEFG5 42.0.1-63.38.31 SLK A pcnbod RCVRY-LINK-CGST: threshold has cleared SC=01 LLI=pcnbod CLASS=VL


-----
 Comparison of 8 created lines with 7 lines of desired results:
cmp: EOF on expected-output.txt
 Failed -- files not identical -- detailed comparison follows.
 Passed by ignoring whitespace differences.

See temporary files f? for intermediate results, man pages for other details ... cheers, drl

$ perl -00 -lne '/threshold/ && s/\n//g && print' testfile
ABCD DEFG5 42.0.1-63.38.31 SLK A FFFFFF REPT-LINK-CGST: threshold level 0 to 1SC=00 LLI=mmmmm CLASS=SL

ABCD DEFG5 42.0.1-63.38.31 SLK A FFFFFF RCVRY-LINK-CGST: threshold has clearedSC=00 LLI=mmmmm CLASS=SL

ABCD DEFG5 42.0.1-63.38.31 SLK A pcnbod REPT-LINK-CGST: threshold level 0 to 1SC=01 LLI=pcnbod CLASS=VL

ABCD DEFG5 42.0.1-63.38.31 SLK A pcnbod RCVRY-LINK-CGST: threshold has clearedSC=01 LLI=pcnbod CLASS=VL

Thanks to Yazu & drl,

drl, "grep -A1 -B1 " will not work in my server.

Yazu, when i tried the command, got ouput as below.

perl -00 -lne '/threshold/ && s/\n//g && print' uni_op   
ABCD   DEFG5 42.0.1-63.38.31       SLK A   FFFFFF REPT-LINK-CGST: threshold level 0 to 1                 SC=00   LLI=mmmmm                 CLASS=SL                     ABCD   DEFG5  42.0.1-63.38.31        IP7CONN ooooooo     Retrans      ABCD   DEFG5 42.0.1-63.38.31         SLK A   FFFFFF RCVRY-LINK-CGST: threshold has cleared                 SC=00   LLI=mmmmm                 CLASS=SL                     ABCD  DEFG5 42.0.1-63.38.31        SLK  A   pcnbod     REPT-LINK-CGST: threshold level 0 to 1                 SC=01   LLI=pcnbod                     CLASS=VL                     ABCD   DEFG5  42.0.1-63.38.31            IP7CONN ooooooo     Retrans

        ABCD  DEFG5 42.0.1-63.38.31         SLK  A   pcnbod     RCVRY-LINK-CGST: threshold has cleared                 SC=01   LLI=pcnbod                     CLASS=VL

Pl help me