Printing records in different format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Printing records in different format
# 1  
Old 08-17-2012
Printing records in different format

Hi all,
I have a input file say record.txt

hostname IP_address Port_No Version
A 10.10.10.1 80 6.02
B 10.10.10.2 81 6.03
C 10.10.10.3 82 6.04

row 1 has 4 field headings : hostname, IP_address, Port_No and Version.
and from 2nd row onwards the actual records start.

now i need to convert these records in a single line containing hostname and 1 property at a time,

say, something like this...

hostname=A/attribute=IP_address/value=10.10.10.1
hostname=A/attribute=Port_No/value=80
hostname=A/attribute=Version/value=6.02
hostname=B/attribute=IP_address/value=10.10.10.2
hostname=B/attribute=Port_No/value=81
hostname=B/attribute=Version/value=6.03
hostname=C/attribute=IP_address/value=10.10.10.3
hostname=C/attribute=Port_No/value=82
hostname=C/attribute=Version/value=6.04

I tried doing this using AWK but no luck...
Please help..

Thanx in advance...
# 2  
Old 08-17-2012
Code:
awk '
NR==1{for(i=1;i<=NF;i++) h[i]=$i; next}
{
 for(i=2;i<=NF;i++)
  print h[1] "=" $1 "/attribute=" h[i] "/value=" $i
}' record.txt

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 08-17-2012
Thanks

Thank you elixir, it worked...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Printing df -h output in json format

Hi All, i am trying to print the df -h ouput in json format. using below script. #!/usr/bin/env bash df -h > /tmp/sdf nawk '{print " "$1" "$2" "$3" "$4" "$5" "$6" "}' /tmp/sdf > /tmp/sdf1 nawk 'NR==1 { for (i=1; i<=NF; i++) { f = i }}{ print $(f), $(f), $(f), $(f), ... (2 Replies)
Discussion started by: sravani25
2 Replies

2. UNIX for Beginners Questions & Answers

Help in printing records where there is a 'header' in the first record ???

Hi, I have a backup report that unfortunately has some kind of hanging indent thing where the first line contains one column more than the others I managed to get the output that I wanted using awk, but just wanting to know if there is short way of doing it using the same awk Below is what... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Help required in printing in specific format

Hi All, I 'm matching two files based on the first 2 columns and then populate other fields along with subtraction of few fields. I have managed to get the output. However, is there a easier way in formatting the output as shown below instead of using additional printf for getting fixed width... (4 Replies)
Discussion started by: shash
4 Replies

4. UNIX for Dummies Questions & Answers

Printing files in a specific format

Hi, I have following files in a directory with '.meta' extension, which have data in follwoing patterns. i need to print data from these files in below metioned format. please provide a script for this solution. file names: TEST_HISTORY_MTH.meta AB_TEST_1.meta cat... (2 Replies)
Discussion started by: AAHinka
2 Replies

5. UNIX for Dummies Questions & Answers

Help with printing output format from a file

Hi, I need help in printing data in below format from file extensions with .dml, i have listed details below file name is test_temp.dml, location in /home/users/test01/test_temp.dml file content: sample_type= record decimal(",") test_type; date("DD-MM-YYYY")(",") test_date... (2 Replies)
Discussion started by: AAHinka
2 Replies

6. UNIX for Dummies Questions & Answers

How can I get certain records from this file format?

I have a file named: learn.999 that has the following format where each record is separated by ^M as follows: ... (11 Replies)
Discussion started by: mrn6430
11 Replies

7. Shell Programming and Scripting

Printing records which meet condition using awk

Below is the code nawk -F"|" 'tolower($1) ~ "abc" |"" {if (tolower($2) ~"abc"|"") print$0}' file I want the records from file whose 1st and 2nd field should be either "abc" or "null" I tried but its giving error. Appreciate help (2 Replies)
Discussion started by: pinnacle
2 Replies

8. Shell Programming and Scripting

Format & re-arrange the records

Data on my input file : Ac1n1s1c2n2s2XPd1r1e1t1d2r2e2t2d3r3e3t3d4r4e4t4RT Bh1k1p1h2k2p2NTq1y1f1m1q2y2f2m2q3y3f3m3q4y4f4m4ZN and i want the output to be: Ac1n1s1XPd1r1e1t1RT Ac1n1s1XPd2r2e2t2RT Ac1n1s1XPd3r3e3t3RT Ac1n1s1XPd4r4e4t4RT Ac2n2s2XPd1r1e1t1RT Ac2n2s2XPd2r2e2t2RT... (6 Replies)
Discussion started by: rlmadhav
6 Replies

9. Shell Programming and Scripting

Printing Formatted Records From A File

Hi All, I'm writing a script that provides a menu to manipulate records. I'm having a problem, however. The first option I'm writing is simply to display all of the records in a supplied file. The supplied file ('records') currently contains sample data, this data is: ... (3 Replies)
Discussion started by: Silentwolf
3 Replies

10. UNIX for Dummies Questions & Answers

printing out procedures in unknown format

Does Anybody know how to print procedures written in this format? .. .nr PS 11 .nr VS 14 .nr LL 6.5i .ad l .DA 1/9/91 .in 4.0i .DS Shepard D. Johnson .DE .in 0i .sp .sp .sp .LG .ce .B Pressure Sensors (P/N 16-0018-0 & 16-0018-1) Test Procedure (1 Reply)
Discussion started by: rjtrjt
1 Replies
Login or Register to Ask a Question