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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required on joining one line above & below to the pattern matched string line.
# 1  
Old 07-01-2011
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
Code:
    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
Code:
        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

Code:
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 Smilie

Last edited by Franklin52; 07-02-2011 at 05:36 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 07-01-2011
Hi

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

Guru.
# 3  
Old 07-01-2011
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):

Code:
 
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! :-)
# 4  
Old 07-01-2011
Thanks to guruprasadpr & rwuerth.

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

Kindly help me Smilie
# 5  
Old 07-01-2011
Quote:
Originally Posted by krao
Thanks to guruprasadpr & rwuerth.

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

Kindly help me Smilie
Can you post the commands and your output?
# 6  
Old 07-01-2011
Hi.

With common commands in Section 2 of this script:
Code:
#!/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:
Code:
% ./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
# 7  
Old 07-02-2011
Code:
$ 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

4. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

5. Shell Programming and Scripting

ksh : need to get the 4 th line above and 2 nd below the matched pattern in the log file

I have a log file as given below 012/01/21 10:29:02 (111111) Processing Job '23_369468343464564' 2012/01/21 10:29:02 (111111) Making Job '23_369468343464564.0'... 2012/01/21 10:29:04 (111111) Jobnumber '23_369468343464564' was successful 2012/01/21 10:29:04 ... (12 Replies)
Discussion started by: rpm120
12 Replies

6. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

7. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

8. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

9. Shell Programming and Scripting

extracting matched pattern from a line using sed

I am trying to pull certain pieces of data out of a line of a file that matches a certain pattern: The three pieces that I want to pull out of this line are the only occurrences of that pattern within the line, but the rest of the line is not consistent in each file. Basically the line is... (3 Replies)
Discussion started by: ellhef
3 Replies

10. Shell Programming and Scripting

search for the matched pattern by tracing back from the line

Hi, I want to grep the line which has 'data11'.then from that line, i need to trace back and find out the immediate line which has the same timestamp of that grepped line. for eg: log file: ----------- Process - data Process - datavalue - 2345 Process - data Process - data Process... (9 Replies)
Discussion started by: Sharmila_P
9 Replies
Login or Register to Ask a Question