Rows to columns transposing and reformating.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rows to columns transposing and reformating.
# 8  
Old 11-28-2009
Works

It works perfectly for the test inputs a provided in earlier posts. It doesnt work for the actual inputs(I cant put my actual servers ip address in the forumSmilie)'
what do i have to modify for taking the actual input, which is in the same format but the numbers/characters will be different.
The one marked in bold has to be in the last row but it keeps coming to the first row Smilie

Code:
$ ksh -x awks.sh
+ awk /^VIRTUAL/{i=j=k=l;V[i++]=$2}
/^SERVICE/{S[j++]=$2}
/^POOL/ {P[k++]=$2}
/^MEMBER/ {M[l++]=$2}
END {print "VIRTUAL SERVICE POOL MEMBER" ;
for (i in M) printf "%-15s %-7s %-10s %s\n",V[i],S[i],P[i],M[i]} f5.dat
VIRTUAL         SERVICE POOL       MEMBER
                8184               192.168.2.4:8184
192.168.3.1     8185    TestPool3  192.168.3.2:8185
                8186               192.168.3.3:8186
192.168.1.1     8181    TestPool1  192.168.1.2:8181
                                   192.168.1.3:8181
192.168.2.1     8182    TestPool2  192.168.2.2:8182
                8183               192.168.2.3:8183


Last edited by radoulov; 11-29-2009 at 09:05 AM.. Reason: added code tags
# 9  
Old 11-28-2009
Hi Bluethunder. If you keep changing your input specifications and/or your input is not representative then it becomes a tall order to create a working script. I suggest you try and figure out the workings of the awk script and modify it so that it does work with your input..
# 10  
Old 11-28-2009
Apologies & thanks Scrutinizer.
I cant really paste the actual input as those details are sensitive information .
So i had created a sample form of ip addresses/names for the input.

The earlier script given by jsmith works perfectly for 3 columns . I do not have much knowledge on perl ,else I would have modifed it to work for 4 columns.

Much appreciated.Smilie
# 11  
Old 11-29-2009
Code:
awk 'BEGIN { fmt = "%-15s %-7s %-10s %s\n"
  printf fmt, "VIRTUAL", "SERVICE", "POOL", "MEMBER" }
/^VIRTUAL/ { 
  if (v) { print_row(); sc = mc = mx = x; split(x, s); split(x, m) }
  v = $2 }    
/^SERVICE/ { s[++sc] = $2 }
/^POOL/    { p = $2       }
/^MEMBER/  { m[++mc] = $2 } 
{ mx = sc > mc ? sc : mc  } 
END { print_row() }  
func print_row() { printf fmt, v, s[1], p, m[1]
  for (i=2; i<=mx; i++) printf fmt, x, s[i], x, m[i] }
' infile


Last edited by radoulov; 11-29-2009 at 07:46 AM..
# 12  
Old 11-29-2009
Quote:
Originally Posted by bluethunder
Apologies & thanks Scrutinizer.
I cant really paste the actual input as those details are sensitive information .
So i had created a sample form of ip addresses/names for the input.

The earlier script given by jsmith works perfectly for 3 columns . I do not have much knowledge on perl ,else I would have modifed it to work for 4 columns.

Much appreciated.Smilie
Hi. Well you could have used you actual input and only change the IP-adresses. Also what script by jsmith are you referring to, since I don't see one in this thread?
# 13  
Old 11-29-2009
Quote:
Originally Posted by Scrutinizer
Hi. Well you could have used you actual input and only change the IP-adresses. Also what script by jsmith are you referring to, since I don't see one in this thread?
The original thread has been split so I suppose he's referring to this one.
# 14  
Old 11-29-2009
Success

Thanks for the timely help rad. Your code does the job without shuffling the output.

I did test it for 15+ rows of actual inputs and it worked like a charm. I did some adjustments to the code ( not much) . Smilie

I am posting the sed arguments used in my script to format the input items.
See if I am using the correct syntax and How can I make sure that there is only one space between the col 1 & 2 ( in my input file) using SED.

Code:
root@webmon# cat cmdfile
# Removes lines with | & ( symbols.
/.*(/d;/.*|/d

#Removes lines with text BIG-IP & SEPARATOR
/#SEPARATOR#/d ;/.*BIG-IP/d;/.*STANDBY/d;/^SSL/d;

#Removes lines between text Kernel & Filter
/Kernel/,/Filter/d

# Removes the empty lines/ new lines.
/^$/d

# Removes the text starting with + ending with >
s/+[^+]*>/|/g

# Removes the text starting with + ending with |
s/+[^+]*|/|/g

# Removes trailing tabspaces
s/^[ \t]*//

# Removes the texts in uppercase letters.
s/UP//;s/DISABLED//;s/ENABLED//;s/UNCHECKED//;s/|//;s/ //;s/UNIT 1//;s/ACTIVE//;s/[ ADDR DOWN ]//;s/CHECKING//;s/FORCED//;s/WILDCARD//

#s/VIRTUAL/VIRTUAL=/g ;s/MEMBER/MEMBER=/g;s/SERVICE/SERVICE=/g;s/POOL/POOL=/g;s/[' ']*//g

#Removes empty spaces.
#s/[' ']*//g

s/VIRTUAL/VIRTUAL /g;s/ SERVICE/SERVICE/g

Or

To put in simple words I just need the lines with strings VIRTUAL/SERVICE/POOL/MEMBER.

I do not require the rest of the items as they are junk strings.

Thanks.

Last edited by bluethunder; 11-29-2009 at 12:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transposing rows to columns with multiple similar lines

Hi, I am trying to transpose rows to columns for thousands of records. The problem is there are records that have the same lines that need to be separated. the input file as below:- ID 1A02_HUMAN AC P01892; O19619; P06338; P10313; P30444; P30445; P30446; P30514; AC Q29680; Q29837;... (2 Replies)
Discussion started by: redse171
2 Replies

2. Shell Programming and Scripting

transposing lines to columns

Okay folks, here's a question. I tried searching but couldn't find exactly what I needed. I have a text file (excerpt below). This text file is an extract I did from several hundred pages of datasheets using grep so I could look only at the site history for each site. The problem is that... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

3. Shell Programming and Scripting

Transposing rows and columns (pivoting) using shell scripting

Here is the contents of an input file. A,1,2,3,4 10,aaa,bbb,ccc,ddd 11,eee,fff,ggg,hhh 12,iii,jjj,lll,mmm 13,nnn,ooo,ppp I wanted the output to be A 10 1 aaa 10 2 bbb 10 3 ccc 10 4 ddd 11 1 eee 11 2 fff 11 3 ggg 11 4 hhh ..... and so on How to do it in ksh... (9 Replies)
Discussion started by: ksatish89
9 Replies

4. Shell Programming and Scripting

transposing columns into rows

Hi, I need to transpose columns of my files into rows and save it as individual files. sample contents of the file below. 0.9120 0.7782 0.6959 0.6904 0.6322 0.8068 0.9082 0.9290 0.7272 0.9870 0.7648 0.8053 0.8300 0.9520 0.8614 0.6734 0.7910 0.6413 0.7126 0.7364 0.8491 0.8868 0.7586 0.8949... (8 Replies)
Discussion started by: ida1215
8 Replies

5. Shell Programming and Scripting

Help for a Perl newcomer! Transposing data from columns to rows

I have to create a Perl script which will transpose the data output from my experiment, from columns to rows, in order for me to analyse the data. I am a complete Perl novice so any help would be greatly appreciated. The data as it stands looks like this: Subject Condition Fp1 ... (12 Replies)
Discussion started by: Sarah_W
12 Replies

6. Shell Programming and Scripting

Transposing Repeated Rows to Columns.

I have 1000s of these rows that I would like to transpose to columns. However I would like the transpose every 3 consecutive rows to columns like below, sorted by column 3 and provide a total for each occurrences. Finally I would like a grand total of column 3. 21|FE|41|0B 50\65\78 15... (2 Replies)
Discussion started by: ravzter
2 Replies

7. Shell Programming and Scripting

awk, string as record separator, transposing rows into columns

I'm working on a different stage of a project that someone helped me address elsewhere in these threads. The .docs I'm cycling through look roughly like this: 1 of 26 DOCUMENTS Copyright 2010 The Age Company Limited All Rights Reserved The Age (Melbourne, Australia) November 27, 2010... (9 Replies)
Discussion started by: spindoctor
9 Replies

8. Shell Programming and Scripting

Transposing rows into columns

I have a file like the one given below P1|V1|V2 P1|V1|V3 P1V1|V2 P2|V1|V4 P2|V2|V6 P2|V1|V4 I want it convert to P1|V1|V2|V2|V3 P2|V1|V4|V2|V6 2nd and 3rd column should be considered as together and so the tird row is duplicate Any ideas? (3 Replies)
Discussion started by: prasperl
3 Replies

9. Shell Programming and Scripting

Transposing columns with awk

I want a sweet simple time efficient awk script in online which gets output 001_r 0.0265185 0.0437049 0.0240642 0.0310264 0.0200482 0.0146746 0.0351344 0.0347856 0.036119 1.49 firstcoloumnvalue allvaluesof 'c' in one row 001_r : 002_r c: 0.0265185 N: 548 001_r : 007_r c:... (5 Replies)
Discussion started by: phoenix_nebula
5 Replies

10. Shell Programming and Scripting

awk - reformating rows into columns

looking to do the following... What the data looks like server1 02/01/2008 groups 10 server1 03/01/2008 groups 15 server1 04/01/2008 groups 20 server2 02/01/2008 users 50 server2 03/01/2008 users 75 server2 04/01/2008 users 100 server2 04/01/2008 users 125 What I would like the... (1 Reply)
Discussion started by: jmd2004
1 Replies
Login or Register to Ask a Question