Pick and print fields..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pick and print fields..
# 1  
Old 06-08-2012
Pick and print fields..

HI,
I am having a requirement to check the unavailabilty of signling links. Please help.

Thanks in advance.

I need to have output like this :

Code:
OPC=2-16-1 APC=2-14-7 12  1 12-05-01 TIME=03:49:03 12-05-01 TIME=03:53:36

Input file:
Code:
   +++ KYM 12-05-01 03:49:03 MTCE 3484 #624349 >
** REPT CSLK  TYPE=CNATL  SM=2  OPC=2-16-1     TIME=03:49:03
            12  1  UNAVAIL  AUTO  MTCE  LKER-L2    
            EXCESS ERROR RATE - SUERM
             
                         
     APC=2-14-7  SLC=1
   END OF REPORT #624349 ++-

   +++ KYM 12-05-01 03:55:13 MTCE 3484 #624842 >
   REPT CCSLK  TYPE=C7NATL  SM=2  OPC=2-16-1     TIME=03:53:36
            12  1  AVAIL  AUTO        MAX SIF=272 OCTETS
            
     SM=2  PSUPH=2-1-14    SM=2  DEN=2-0-61
                         
     APC=2-14-7  SLC=1
   END OF REPORT #624842 ++-

---------- Post updated at 08:40 PM ---------- Previous update was at 03:08 PM ----------

Hi,

Let us reduce the problem to one leg only. Please suggest how it can be fetched.

Output :
Code:
OPC=2-16-1 APC=2-14-7 UNAVAIL LKER-L2 12  1 12-05-01 TIME=03:49:03

Input :
Code:
   +++ KYM 12-05-01 03:49:03 MTCE 3484 #624349 >
** REPT CSLK  TYPE=CNATL  SM=2  OPC=2-16-1     TIME=03:49:03
            12  1  UNAVAIL  AUTO  MTCE  LKER-L2    
            EXCESS ERROR RATE - SUERM
             
                         
     APC=2-14-7  SLC=1
   END OF REPORT #624349 ++-


Last edited by Scrutinizer; 06-08-2012 at 06:55 AM.. Reason: code tags
# 2  
Old 06-08-2012
I am tempted to return the question:
How do you think it can be done?
Looks to me that you are up to writing some algorithmic pseudocode before you can translate into shell script...
# 3  
Old 06-08-2012
Quote:
Originally Posted by vbe
I am tempted to return the question:
How do you think it can be done?
Looks to me that you are up to writing some algorithmic pseudocode before you can translate into shell script...
I tried this code

Code:
gawk '/UNAVAIL/ {print x,$3,$1,$2};{x=$6" "$7};'

and got the output as-
Code:
OPC=2-16-1 TIME=03:49:03 UNAVAIL 12 1

Still some fields unable to capture are marked as RED
Code:
  +++ KYM 12-05-01 03:49:03 MTCE 3484 #624349 >
** REPT CSLK  TYPE=CNATL  SM=2  OPC=2-16-1     TIME=03:49:03
            12  1  UNAVAIL  AUTO  MTCE  LKER-L2
            EXCESS ERROR RATE - SUERM


     APC=2-14-7  SLC=1
   END OF REPORT #624349 ++-

# 4  
Old 06-08-2012
Im maybe missing something here... One record is made of 9 lines, is that so? and you need to collect data from the first second and sixth no?
# 5  
Old 06-09-2012
Quote:
Originally Posted by vbe
Im maybe missing something here... One record is made of 9 lines, is that so? and you need to collect data from the first second and sixth no?
There are 8 lines and i want to pick from 1, 2, 3 & 7th line.

I modified the code as under :
Code:
cat myfile | grep -w -B 2 -A 6 -w UNAVAIL | gawk '/UNAVAIL/ {print x,$3,$1,$2,$6};{x=$6" "$7}; /CSLK/ {print y};{y=$3}; /++-/ {print z};{z=$1}'

and got the output as under :
Code:
12-05-01
OPC=2-16-1 TIME=03:49:03 UNAVAIL 12 1 LKER-L2
APC=2-14-7

Still struggling to get in one line. Hw can this code be beautified Smilie

Thanks

---------- Post updated 06-09-12 at 09:19 PM ---------- Previous update was 06-08-12 at 10:14 PM ----------

Quote:
Originally Posted by vanand420
There are 8 lines and i want to pick from 1, 2, 3 & 7th line.

I modified the code as under :
Code:
cat myfile | grep -w -B 2 -A 6 -w UNAVAIL | gawk '/UNAVAIL/ {print x,$3,$1,$2,$6};{x=$6" "$7}; /CSLK/ {print y};{y=$3}; /++-/ {print z};{z=$1}'

and got the output as under :
Code:
12-05-01
OPC=2-16-1 TIME=03:49:03 UNAVAIL 12 1 LKER-L2
APC=2-14-7

Still struggling to get in one line. Hw can this code be beautified Smilie

Thanks

I tried undernoted code but still not successful, please suggest.

Code:
cat myfile | grep -w -B 2 -A 6 -w UNAVAIL | gawk '/UNAVAIL/ {printf x,$3,$1,$2,$6};{x=$6" "$7}; /CSLK/ {printf y};{y=$3}; /++-/ {print z};{z=$1}'

It gives the output as
Code:
12-05-01OPC=2-16-1 TIME=00:15:40APC=4-216-3

but does not include $3,$1,$2,$6.

My expected output is
Code:
OPC=2-16-1 APC=2-14-7 UNAVAIL LKER-L2 12  1 12-05-01 TIME=03:49:03

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. Shell Programming and Scripting

Pick and print

Hi Friends, I'm facing an issue. I am having a file as under : a1 b1 c1 a2 a3 a4 b4 a5 b5 and I need to get the output as under: a1 b1 a4 b4 a5 b5 i.e. to pick the columns where a and b are consecutive and ignoring rest. I was trying some thing sily as: (2 Replies)
Discussion started by: vanand420
2 Replies

7. Shell Programming and Scripting

How to print selected fields

HI, I am using below command to display the words, but i am getting awk error. Please help me out on this I am using below code i am getting error as If i use below code i am getting below OP Output from where i am trying to select the fields after delimiter "," from here i want to... (5 Replies)
Discussion started by: darling
5 Replies

8. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

9. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

10. Shell Programming and Scripting

print last 2 fields of a line

Hi everyone, happy new year! Can someone teach me how I can print the last 2 fields of every line in a file (lines don't have uniform number of fields), for example: input file: aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll mmm nnn ooo output: ccc ddd fff ggg iii jjj nnn ooo ... (2 Replies)
Discussion started by: Deanne
2 Replies
Login or Register to Ask a Question