Using awk add preffic to dynamic order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk add preffic to dynamic order
# 1  
Old 03-20-2018
Using awk add preffic to dynamic order

Source file

Code:
portIndex:  75
portName: slot9 port11
        c0:50:96:05:9f:f2:00:f2
        c0:50:96:05:9f:f2:00:ea
        c0:50:96:05:9f:f2:01:13
        c0:50:96:05:9f:f2:01:12
        c0:50:96:05:9f:f2:01:0b
        c0:50:96:05:9f:f2:01:0a
        c0:50:96:05:9f:f2:01:03
        c0:50:96:05:9f:f2:01:02
        c0:50:96:05:9f:f2:00:fb
        c0:50:96:05:9f:f2:00:fa
portIndex:  78
portName: slot9 port14
        c0:50:96:07:cd:9d:00:86
        c0:50:96:07:cd:9d:00:96
        c0:50:96:07:cd:9d:00:7e
        c0:50:96:07:cd:9d:00:56
portIndex:  79
portName: slot9 port15
        c0:50:96:07:cd:9d:01:1e
        c0:50:96:07:cd:9d:00:fe
        c0:50:96:07:cd:9d:00:de
        c0:50:96:07:cd:9d:01:26
        c0:50:96:07:cd:9d:00:c6
        c0:50:96:07:cd:9d:00:ce
        c0:50:96:07:cd:9d:01:16
        c0:50:96:07:cd:9d:00:f6
        c0:50:96:07:cd:9d:00:a6
portIndex:  89
portName: slot9 port16
        c0:50:96:07:cd:9d:00:b6
        c0:50:96:07:cd:9d:01:2e
        c0:50:96:07:cd:9d:00:5e
        c0:50:96:07:cd:9d:00:ae
        c0:50:96:07:cd:9d:00:4e
        c0:50:96:07:cd:9d:00:66
        c0:50:96:07:cd:9d:00:e6
        c0:50:96:07:cd:9d:00:76
portIndex:  90
portName: slot9 port17
        c0:50:96:07:cd:9d:00:ee
        c0:50:96:07:cd:9d:00:d6
        c0:50:96:07:cd:9d:00:46
        c0:50:96:07:cd:9d:00:06
portIndex:  91
portName: slot9 port18
        c0:50:96:07:cd:9d:00:be

Required Output. Is it possible to add prefix of port details for the following WWn

Code:
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:00:f2
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:00:ea
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:01:13
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:01:12
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:01:0b
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:01:0a
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:01:03
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:01:02
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:00:fb
portIndex:  75 portName: slot9 port11,c0:50:96:05:9f:f2:00:fa


portIndex:  78 portName: slot9 port14,c0:50:96:07:cd:9d:00:86
portIndex:  78 portName: slot9 port14,c0:50:96:07:cd:9d:00:96
portIndex:  78 portName: slot9 port14,c0:50:96:07:cd:9d:00:7e
portIndex:  78 portName: slot9 port14,c0:50:96:07:cd:9d:00:56

---------- Post updated at 06:57 AM ---------- Previous update was at 06:02 AM ----------

I tired i am getting output like this

Code:
$ cat file-name |egrep "portIndex|portName|c0"|awk '{print $1,$2,$3}'|awk 'NF == 2{PRFX = $1 $2; next} {print PRFX ", " $0}'



Getting output like this. Upto port Index

Code:
portIndex:75, portName: slot9 port11
portIndex:75, c0:50:96:05:9f:f2:00:f2
portIndex:75, c0:50:96:05:9f:f2:00:ea
portIndex:75, c0:50:96:05:9f:f2:01:13
portIndex:75, c0:50:96:05:9f:f2:01:12
portIndex:75, c0:50:96:05:9f:f2:01:0b
portIndex:75, c0:50:96:05:9f:f2:01:0a
portIndex:75, c0:50:96:05:9f:f2:01:03




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-20-2018 at 09:50 AM.. Reason: Added / Changed CODE tags.
# 2  
Old 03-20-2018
Code:
awk '
/portIndex:/ {if (c++ > 0) print ""; port_info="" ; port_info=$0 " "; next}
/portName:/ {port_info=port_info $0 "," ; next}
{$1=$1; print port_info $0}
' source_file

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-21-2018
Thanks it was working great
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Executing if dynamic conditions in awk

Hi All, I got struck at the below point where i am unable to get the desired output after forming the dynamic conditions.Below is the design. 1. We are getting inputs from the shell arguments and storing in a variable like below. CONDITIONS="1=CT,2=US_10,3=CT_US_10" 2. After this i am... (14 Replies)
Discussion started by: cskumar
14 Replies

2. UNIX for Dummies Questions & Answers

awk : dynamic output flatfile filename

Hello, I'm using the awk command to insert empty columns on a tab delimited flatfile - which works fine - => But I'm not able to manage dynamicaly the filename of the awk output based on the source flatfile filename I have 3 source flatfile: flatfile_Jan-2016.csv flatfile_Feb-2016.csv... (3 Replies)
Discussion started by: Tipiak
3 Replies

3. Shell Programming and Scripting

How order a data matrix using awk?

is it possible to order the following row clusters from ascending to descending. thanx in advance input 1 2 4 0 1 2 4 0 3 3 3 3 1 5 1 0 1 5 1 0 6 0 0 0 5 1 1 1... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Shell Programming and Scripting

Modify line with dynamic variable in awk

Hi, I'm guessing this is probably relatively straight forward to do in awk, but I just can't get my head round it! I have a log file of the following format: 3:03:35 (lmgrd) TIMESTAMP 10/14/2011 3:20:41 (MLM) IN: "MATLAB" user1@host1.private.dns.zone 3:21:05 (MLM) IN: "MATLAB"... (2 Replies)
Discussion started by: chrissycc
2 Replies

5. Shell Programming and Scripting

how to make pattern search dynamic in awk

Hi, I have a data in a file like below - andy 22 abc 30000 wallstreet paul 30 xyz 40000 martstreet john 35 abc 50000 martstreet I want to search number of employees working in a particular company. Below query executes perfectly - awk '/abc/{ COUNT ++; }END { print "number of... (3 Replies)
Discussion started by: shell123
3 Replies

6. Shell Programming and Scripting

Dynamic command line generation with awk

Hi, I'm not an expert in awk but i need a simple script to do this: I'd like to AutoCrop PDF files. I found 2 simple script that combined together could help me to automatize :) The first utiliti is "pdfinfo" that it gives the MediaBox and TrimBox values from the pdf. The pdfinfo output... (8 Replies)
Discussion started by: gbagagli
8 Replies

7. UNIX for Advanced & Expert Users

dynamic match thru awk

hey , my i/p text looks like this, FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M NOTE Look carefully for the position FILE_TYPE,FILE_DESC... (23 Replies)
Discussion started by: manas_ranjan
23 Replies

8. Shell Programming and Scripting

Dynamic filename in awk

Hi The following code seems to work, but why am i getting an error message? cscyabl@comet:(develop)> awk 'BEGIN {FS="|"}{print $2 >> $1}' test.sum awk: A print or getline function must have a file name. The input line number is 8. The file is test.sum. The source line number is 1. ... (2 Replies)
Discussion started by: Indalecio
2 Replies

9. UNIX for Dummies Questions & Answers

Parsing XML dynamic data via awk?

I am trying to use a line of output in an XML file as input in another new XML file for processing purposes via a shell script. Since I am a newbie though, I'm not sure how to do this since the data is different everytime. I am using this technique with static data right now: echo -n "Running... (5 Replies)
Discussion started by: corwin43
5 Replies
Login or Register to Ask a Question