How to get row data printed in column using awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get row data printed in column using awk?
# 1  
Old 08-27-2017
How to get row data printed in column using awk?

Hi team,

I have below sample file.

Code:
$ cat sample
dn: MSISDN=400512345677,dc=msisdn,ou=NPSD,serv=CSPS,ou=servCommonData,dc=stc
structuralObjectClass: NphData
objectClass: NphData
objectClass: MSISDN
entryDS: 0
nodeId: 35
createTimestamp: 20170216121047Z
modifyTimestamp: 20170216121047Z
NPREFIX: QT
SUBSTYPE: 3
MSISDN: 466512345677

dn: MSISDN=400512345678,dc=msisdn,ou=NPSD,serv=CSPS,ou=servCommonData,dc=stc
structuralObjectClass: NphData
objectClass: NphData
objectClass: MSISDN
entryDS: 0
nodeId: 35
createTimestamp: 20170223114401Z
modifyTimestamp: 20170223114401Z
NPREFIX: QT
SUBSTYPE: 3
MSISDN: 400512345678

Here i want output file as below using awk command in linux.

Code:
MSISDN:           SUBSTYPE:  NPREFIX: modifyTimestamp:   
400512345677    3                QT          20170223114401Z
400512345678    3                QT          20170216121047Z

Pls help

Last edited by hicksd8; 04-27-2020 at 09:43 AM..
# 2  
Old 08-27-2017
Hello shanul karim,

Seems your output is not consistent like how come for MSISDN: value for modifyTimestamp:, 20170223114401Z has come? If this is a typo then following may help you in same.
Code:
awk -F'=|,| ' 'BEGIN{
  print "MSISDN:\t\tSUBSTYPE:\tNPREFIX:\tmodifyTimestamp:"
}
NR>1 && /dn: MSISDN/{
  print a["msidn"],a["subtype"],a["nprefix"],a["modifyTimestamp"];
  delete a
}
/dn: MSISDN/{
  a["msidn"]=$3;
  next
}
/SUBSTYPE/{
  a["subtype"]=$2;
  next
}
/NPREFIX/{
  a["nprefix"]=$2;
  next
}
/modifyTimestamp/{
  a["modifyTimestamp"]=$2;
  next
}
END{
  print a["msidn"],a["subtype"],a["nprefix"],a["modifyTimestamp"];
}
' OFS="\t"   Input_file

PS: If delete a is not working for you then you could traverse through array a like for(i in a){delete a[i]}.

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-27-2017 at 12:41 PM.. Reason: Added comment now for deleting the array.
# 3  
Old 08-27-2017
Try:
Code:
awk '
  BEGIN {
    RS=""
    OFS="\t"
    keys="MSISDN:\tSUBSTYPE:\tNPREFIX:\tmodifyTimestamp:"
    n=split(keys,F)
    print keys
  } 

  {
    for(i=1; i<=NF; i+=2) A[$i]=$(i+1)
    $0=""
    for(i=1; i<=n; i++) $i=A[F[i]]
    print
  }
' file

Code:
MSISDN:	SUBSTYPE:	NPREFIX:	modifyTimestamp:
400512345677	3	QT	20170216121047Z
400512345678	3	QT	20170223114401Z


Last edited by hicksd8; 04-27-2020 at 09:44 AM..
# 4  
Old 08-28-2017
thanks a lot ravinder and scrutinizer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column to Row Data.

HI Guys, I have below Input :- X L1 5 Y L1 10 Z L1 15 X L2 20 Y L2 12 Z L2 15 X L3 100 Y L3 Z L3 300 Output:- ID L1 L2 L3 X 5 10 15 Y 20 12 15 Z 100 Null 300 (11 Replies)
Discussion started by: pareshkp
11 Replies

2. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

3. Shell Programming and Scripting

awk - script help: column to row format of data allignment?

Experts Good day, I have the following data, file1 BRAAGRP1 A2X B2X C2X D2X BRBGRP12 A3X B3X Z10 D09 BRC1GRP2 LO01 (4 Replies)
Discussion started by: rveri
4 Replies

4. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Awk to add selected row column data

Looks at the most efficient way to add up the column of data based off of the rows. Random data Name-Number-ID Sarah-2.0-15 Bob-6.3-15 Sally-1.0-10 James-1.0-10 Scotty-10.7-15 So I would select all those who have ID = 15 and then add up total number read - p "Enter ID number" Num ... (3 Replies)
Discussion started by: Ironguru
3 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

9. Shell Programming and Scripting

Convert row data to column data

Hi Guys, I have a file as follows: a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: a 1 9875 b 786 989 (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies
Login or Register to Ask a Question