Reformat awk output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reformat awk output
# 1  
Old 02-18-2016
Reformat awk output

I need to rearrange the output but i am unable to arrange it to match the format. In the output i need
Code:
NAME=\"To

in the column .

Bash:

Code:
#!/bin/bash

cd /cygdrive/c/output/a
cat *.txt > output.txt

i=/cygdrive/c/output/a/output.csv

#echo "NE_Name, Source, Destination, OSPF_AREA_ID" >$i


awk '
/NE name:/ {
        n = $4
}
/"To/ {
        split($0, t, /[,":]/)
        print n, t[2], t[8], t[18], t[19], t[20], t[21], t[22], t[23], t[24]

}' OFS=, /cygdrive/c/output/a/output.txt >>$i


Output

Code:
AB1S2a	FAC-12-1	AREA=0.0.0.3	UNIC=N	SOAK=32	SSMRCV=G811	OSPF=Y	MSDCC=Y	NAME=\	To AB2S2a SL6 P1-1\
AB2S2a	FAC-12-1	AREA=0.0.0.3	UNIC=N	SOAK=32	SSMRCV=G811	OSPF=Y	MSDCC=Y	NAME=\	To AB3S2a SL6 P1-1\
AB2S2a	FAC-6-1-1	AREA=0.0.0.3	SOAK=32	SSMRCV=DUS-SDH	OSPF=Y	MSDCC=Y	NAME=\	To AB1S2a SL12 P1-1\	
ABHS2a	FAC-13-1-1	AREA=0.0.0.3	SOAK=32	SSMRCV=STU	OSPF=Y	MSDCC=Y	NAME=\	To ABHS1a SL4 P3-1\	
ABHS1a	FAC-12-1-1-1	TMGREF=N	NAME=\	To ABHS2b SL13 P1-1\		TRCMODE=OFF	TRCFORMAT=16-BYTE	SENDDUSFF=N	FREQ=1310
ABHS1a	FAC-12-2-1-1	TMGREF=N	NAME=\	To ABHS2c SL13 P1-1\		TRCMODE=OFF	TRCFORMAT=16-BYTE	SENDDUSFF=N	FREQ=1310

Input

Code:
spawn telnet 172.17.38.104 3083

### NE name: ABHS1a ###


"FAC-12-1-1-1:,,PROT,STBY:DCC=N,TMGREF=N,SYNCMSG=Y,SENDDUS=N,SFBER=1E-4,SDBER=1E-7,MODE=SDH,MUX=K3,SOAK=32,SSMRCV=DUS-SDH,MSDCC=N,NAME=\"To ABHS2b SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
   "FAC-12-2-1-1:,,PROT,ACT:DCC=N,TMGREF=N,SYNCMSG=Y,SENDDUS=N,SFBER=1E-4,SDBER=1E-7,MODE=SDH,MUX=K3,SOAK=32,SSMRCV=DUS-SDH,MSDCC=N,NAME=\"To ABHS2c SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
"FAC-12-1:,,,:DCC=N,AREA=0.0.0.3,TMGREF=Y,SYNCMSG=Y,SENDDUS=N,PJMON=0,SFBER=1E-4,SDBER=1E-7,MODE=SDH,WVLEN=1550.00,MUX=K3,UNIC=N,SOAK=32,SSMRCV=G811,OSPF=Y,MSDCC=Y,NAME=\"To AB2S2a SL6 P1-1\",SENDDUSFF=N,AISONLPBK=FACILITY,FOREIGNFEND=N,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
"FAC-12-1:,,,:DCC=N,AREA=0.0.0.3,TMGREF=Y,SYNCMSG=Y,SENDDUS=N,PJMON=0,SFBER=1E-4,SDBER=1E-7,MODE=SDH,WVLEN=1550.00,MUX=K3,UNIC=N,SOAK=32,SSMRCV=G811,OSPF=Y,MSDCC=Y,NAME=\"To AB3S2a SL6 P1-1\",SENDDUSFF=N,AISONLPBK=FACILITY,FOREIGNFEND=N,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
   "FAC-12-1-1-1:,,PROT,STBY:DCC=N,TMGREF=N,SYNCMSG=Y,SENDDUS=N,SFBER=1E-4,SDBER=1E-7,MODE=SDH,MUX=K3,SOAK=32,SSMRCV=DUS-SDH,MSDCC=N,NAME=\"To ABHS2b SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
   "FAC-12-2-1-1:,,PROT,ACT:DCC=N,TMGREF=N,SYNCMSG=Y,SENDDUS=N,SFBER=1E-4,SDBER=1E-7,MODE=SDH,MUX=K3,SOAK=32,SSMRCV=DUS-SDH,MSDCC=N,NAME=\"To ABHS2c SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"

# 2  
Old 02-18-2016
WHAT do you need?

---------- Post updated at 21:12 ---------- Previous update was at 21:03 ----------

Shooting in the dark, would this do:
Code:
awk '
/NE name:/      {n = $4
                }
/"To/           {gsub (/^ *"|"$/, "")
                 split($0, t, /[,:]/)
                 print n, t[1], t[7], t[17], t[18], t[19], t[20], t[21], t[22], t[23]
                }
' OFS=, file
ABHS1a,FAC-12-1-1-1,TMGREF=N,NAME=\"To ABHS2b SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N
ABHS1a,FAC-12-2-1-1,TMGREF=N,NAME=\"To ABHS2c SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N
ABHS1a,FAC-12-1,AREA=0.0.0.3,UNIC=N,SOAK=32,SSMRCV=G811,OSPF=Y,MSDCC=Y,NAME=\"To AB2S2a SL6 P1-1\",SENDDUSFF=N
ABHS1a,FAC-12-1,AREA=0.0.0.3,UNIC=N,SOAK=32,SSMRCV=G811,OSPF=Y,MSDCC=Y,NAME=\"To AB3S2a SL6 P1-1\",SENDDUSFF=N
ABHS1a,FAC-12-1-1-1,TMGREF=N,NAME=\"To ABHS2b SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N
ABHS1a,FAC-12-2-1-1,TMGREF=N,NAME=\"To ABHS2c SL13 P1-1\",TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,FREQ=1310,LOSSB=I1,OSISDCC=N

---------- Post updated at 21:14 ---------- Previous update was at 21:12 ----------

Unfortunately, your code snippet and output sample do not match.
# 3  
Old 02-18-2016
Your problem is that you split on "
You better remove the first and last " and leave the other " intact.
I think the RudiC solution works for you.
BTW the gsub() is in fact a sub(). Saves a couple of CPU cycles.

Last edited by MadeInGermany; 02-18-2016 at 05:03 PM.. Reason: alt solution did not work
# 4  
Old 02-19-2016
Hi,

The desired output i am trying to get is as below.

Code:
AB1S2a	FAC-12-1	AREA=0.0.0.3		To AB2S2a SL6 P1-1	
AB2S2a	FAC-12-1	AREA=0.0.0.3		To AB3S2a SL6 P1-1
AB2S2a	FAC-6-1-1	AREA=0.0.0.3		To AB1S2a SL12 P1-1
ABHS2a	FAC-13-1-1	AREA=0.0.0.3		To ABHS1a SL4 P3-1

---------- Post updated at 09:50 AM ---------- Previous update was at 08:59 AM ----------

The first output that i first posted i was getting it was from my code sorry for that guys as it was not clear.

---------- Post updated at 09:53 AM ---------- Previous update was at 09:50 AM ----------

And this the input .

Code:

spawn telnet 172.17.38.104 3083

### NE name: ABHS1a ###

"FAC-12-1:,,,:DCC=N,AREA=0.0.0.3,TMGREF=Y,SYNCMSG=Y,SENDDUS=N,PJMON=0,SFBER=1E-4,SDBER=1E-7,MODE=SDH,WVLEN=1550.00,MUX=K3,UNIC=N,SOAK=32,SSMRCV=G811,OSPF=Y,MSDCC=Y,NAME=\"To AB2S2a SL6 P1-1\",SENDDUSFF=N,AISONLPBK=FACILITY,FOREIGNFEND=N,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
"FAC-12-1:,,,:DCC=N,AREA=0.0.0.3,TMGREF=Y,SYNCMSG=Y,SENDDUS=N,PJMON=0,SFBER=1E-4,SDBER=1E-7,MODE=SDH,WVLEN=1550.00,MUX=K3,UNIC=N,SOAK=32,SSMRCV=G811,OSPF=Y,MSDCC=Y,NAME=\"To AB3S2a SL6 P1-1\",SENDDUSFF=N,AISONLPBK=FACILITY,FOREIGNFEND=N,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
"FAC-6-1-1:,,,:DCC=N,AREA=0.0.0.3,TMGREF=Y,SYNCMSG=Y,SENDDUS=N,PJMON=0,SFBER=1E-4,SDBER=1E-7,MODE=SDH,MUX=K3,UNIC=N,SOAK=32,SSMRCV=DUS-SDH,OSPF=Y,MSDCC=Y,NAME=\"To AB1S2a SL12 P1-1\",LBCL=55.006,OPT=2.478,OPR=-13.904,TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,AISONLPBK=FACILITY,FOREIGNFEND=N,FREQ=1550,LOSSB=L2,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"
"FAC-13-1-1:,,,:DCC=N,AREA=0.0.0.3,TMGREF=N,SYNCMSG=Y,SENDDUS=N,PJMON=0,SFBER=1E-4,SDBER=1E-7,MODE=SDH,MUX=K3,UNIC=N,SOAK=32,SSMRCV=STU,OSPF=Y,MSDCC=Y,NAME=\"To ABHS1a SL4 P3-1\",LBCL=36.750,OPT=-3.340,OPR=-4.472,TRCMODE=OFF,TRCFORMAT=16-BYTE,SENDDUSFF=N,AISONLPBK=ALL,FOREIGNFEND=N,FREQ=1310,LOSSB=I1,OSISDCC=N,OSIMSDCC=N,:unlocked-enabled,"


Last edited by adgjmpt; 02-19-2016 at 11:09 AM.. Reason: Wrong format Sorry for the mess up
# 5  
Old 02-19-2016
Therer's no FAC-6-1-1 in your input sample, nor a FAC-13-1-1. There's just two AREA=0.0.0.3 in input, not four like in output.
I can't see any code that converts your input to your output. I'm afraid, I can't help further.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Alter awk script to reformat output

Data: 0,mfrh_green_screen,1455432969,37540,/prod/test/system/sys/unikixmain.log,3.0M,mfrh_green_screen,3120660,0,36964--37540 0,mfrh_green_screen,1455433269,38100,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3164223,0,37540--38100... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Using awk to reformat file output

Hi there. I need to reformat a large file. Here is a sample of the file. NETIK0102_UCS_Boot_a,NETIK0102_UCS_Boot_b 5200 2438 70G 5200 2439 70G NETIK0102_UCS_HBA0_a,NETIK0102_UCS_HBA1_b,NETIK0102_UCS_HBA2_a,NETIK0102_UCS_HBA3_b 2673 19D7 55G 2673 19C0 30G 2673 19F5 120G... (5 Replies)
Discussion started by: kieranfoley
5 Replies

4. Shell Programming and Scripting

Use search pattern to reformat the output

I have below file listing ] ls -1 *.txt MISTradesReport_141105_d130240_VOLCKER_EMEA_LOANIQ_FEED_2013-12-24.txt MISTradesReport_141106_d130240_VOLCKER_NA_LOANIQ_FEED_2013-12-24.txt MISTradesReport_141107_d130240_VOLCKER_EMEA_CDS_CRDI_FEED_2013-12-24.txt... (4 Replies)
Discussion started by: krg.sati
4 Replies

5. Shell Programming and Scripting

awk reformat file

Hello: When I tried a perl-oneliner to re-format fasta file. infile.fasta >YAL069W-1.334 Putative promoter CCACACCACACCCACACACC ACACCACACCCACACACACA ACAGCCCTAATCTAACCC >YAL068C-7235.2170 Putative ABC sequence TACGAGAATAATTT ACGTAAATGAAGTT TATATATAAA >gi|31044174|gb|AY143560.1|... (15 Replies)
Discussion started by: yifangt
15 Replies

6. Shell Programming and Scripting

awk to reformat text

I have this input and want output like below, how can I achieve that through awk: Input: CAT1 FRY-01 CAT1 FRY-04 CAT1 DRY-03 CAT1 FRY-02 CAT1 DRY-04 CAT2 FRY-03 CAT2 FRY-02 CAT2 DRY-01 FAT3 DRY-12 FAT3 FRY-06 Output: category CAT1 item FRY-01 (7 Replies)
Discussion started by: aydj
7 Replies

7. Shell Programming and Scripting

need awk or sed help to reformat output

We have the following output: server1_J00_data_20120711122243 server1_J00_igs_20120711122243 server1_J00_j2ee_20120711122243 server1_J00_sec_20120711122243 server1_J00_data_20120711131819 server1_J00_igs_20120711131819 server1_J00_j2ee_20120711131819 server2_J00_data_20120711122245... (10 Replies)
Discussion started by: ux4me
10 Replies

8. UNIX for Advanced & Expert Users

Script to reformat output

Hi colleagues, I have the followind script. db2 -x "select substr(TBSPACE,1,20) TABLESPACE from syscat.tables where tabschema = 'SCHEMA' and tabname like '%XXXX' group by TBSPACE order by TBSPACE" | awk '{print $1}' | while read tablespace do db2 "list tablespaces show detail" |grep -p -w... (5 Replies)
Discussion started by: systemoper
5 Replies

9. UNIX for Advanced & Expert Users

reformat ps output

I often use "ps -ef" command to list all running processes. Now i want to customize the output to show only 2 parts: CMD and UID as below: /bin/bash /usr/bin/run-parts /etc/cron.weekly root /usr/sbin/httpd apache /usr/sbin/httpd apache /usr/sbin/httpd apache I use ps -ef | awk '{print $8"... (3 Replies)
Discussion started by: fongthai
3 Replies

10. Shell Programming and Scripting

help reformat data with awk

I am trying to write an awk program to reformat a data table and convert the date to julian time. I have all the individual steps working, but I am having some issues joing them into one program. Can anyone help me out? Here is my code so far: # This is an awk program to convert the dates from... (4 Replies)
Discussion started by: climbak
4 Replies
Login or Register to Ask a Question