Scripting columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting columns
# 1  
Old 03-26-2010
Scripting columns

I've got the following data in a file - basically hundred of passes.

Code:
     Pass Id                   : PASSID1
     Pass Name              : 7 day pass 10 Gb
     Pass Group              : uk
     Short Name             : pmbbrh2
     type                       : restrict
     Validity                    : 168 hours
     Pass Id                     : BBFFF009
     Pass Name                : World pass 24hr
     Pass Group               : World
     Short Name               : CsRHhh3
     type                        : block
     Validity                    : 24 hours

What id like is something like the following.

Code:
Pass Id    Pass Name       Pass Group   Short Name     type    Validity
PASSID1  7 day pass 1 Gb   uk         pmbbrh2        restrict   168 hours
BBFFF009 World pass 24hr   EU        CsRHhh3        block     24 hours

I know this is something simple, but it actually hurts my mind trying to solve it. I cant use grep or awk or sed for this. any other ideas ?

The data in the first column is the same throughout the file but the data in the second column is sometimes the same.


Thanks in advanced

Jeffers

Last edited by vgersh99; 03-26-2010 at 04:07 PM.. Reason: code tags, please!
# 2  
Old 03-26-2010
just wondering the reasons behind such restrictions?
# 3  
Old 03-26-2010
Certain passes can only use certain protocols. if the use protocols that use p2p or skype they are restricted

thanks
# 4  
Old 03-26-2010
I think vgersh99 meant why the restrictions prohibiting the use of grep, sed, and awk.
# 5  
Old 03-26-2010
maybe its my understanding of these commands.

I've tried using awk and grep with the cut command and using ":" as the delimitar but after that i get lost.

some of the values have more than 4 or 5 words and so becomes a complete mess.
# 6  
Old 03-26-2010
Perhaps this will do:
Code:
$ awk -F'^ +| *: *' 'function pr(a) {printf("%-10s %-20s %-12s %-12s %-10s %-10s\n", a[1],a[2],a[3],a[4],a[5],a[0])}
{i=NR%6; k[i]=$2; v[i]=$3} NR==6 {pr(k)} !i{pr(v)}' data

Pass Id    Pass Name            Pass Group   Short Name   type       Validity  
PASSID1    7 day pass 10 Gb     uk           pmbbrh2      restrict   168 hours 
BBFFF009   World pass 24hr      World        CsRHhh3      block      24 hours  
PASSID1    7 day pass 10 Gb     uk           pmbbrh2      restrict   168 hours 
BBFFF009   World pass 24hr      World        CsRHhh3      block      24 hours  
PASSID1    7 day pass 10 Gb     uk           pmbbrh2      restrict   168 hours 
BBFFF009   World pass 24hr      World        CsRHhh3      block      24 hours

Cheers,
Alister
# 7  
Old 03-26-2010
Assuming the order of columns is persistent between 'Pass Id'-s and there're no missing columns.

nawk -f jeff.awk myFile

jeff.awk:
Code:
BEGIN {
  FS=" *: *"
  tab=sprintf("\t")

  width=20
}

function trim(str)
{
    sub("^([ ]*|" tab "*)", "", str);
    sub("([ ]*|" tab "*)" "$", "", str);
    return str;
}

{ $1=trim($1) }
!($1 in header){ header[$1]; headerL=(headerL)?headerL OFS sprintf("%-*s", width,$1): sprintf("%-*s", width,$1) }
$1 ~ /^ *Pass Id/ {pass=trim($2)}
{ v[pass]=(v[pass])? v[pass] OFS sprintf("%-*s", width,trim($2)) : sprintf("%-*s", width,trim($2)) }
END {
  print headerL
  for(i in v)
     print v[i]
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check the datatypes of the columns are same through shell scripting?

Hi, We have a requirement like, to check the datatypes of columns against database. After loading the sample data in to one of the database, need to compare the datatypes of the columns are matching with the provided files. Is there a way that we can achieve through shell scripting. We... (7 Replies)
Discussion started by: Samah
7 Replies

2. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. Shell Programming and Scripting

UNIX scripting for finding duplicates and null records in pk columns

Hi, I have a requirement.for eg: i have a text file with pipe symbol as delimiter(|) with 4 columns a,b,c,d. Here a and b are primary key columns.. i want to process that file to find the duplicates and null values are in primary key columns(a,b) . I want to write the unique records in which... (5 Replies)
Discussion started by: praveenraj.1991
5 Replies

5. Programming

Split the columns in Perl scripting

hi all, i have a file like thsi a 1 3;4 b 2 4;7 c 4 5;6 d 4 5;8 now i want 1st, 2nd and in 3rd column i want only 1st column the output should be like below a 1 3 b 2 4 c 4 5 d 4 5 I need it in perl; please dont write in Shell scripting (1 Reply)
Discussion started by: siva kumar
1 Replies

6. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

7. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

8. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

9. 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

10. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies
Login or Register to Ask a Question