Formatting output of script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting output of script
# 1  
Old 12-17-2012
Question Formatting output of script

I'm looking for a way to neaten up the out put of an ldapquery. I might get one, none, or several returns for each query; and each query consists of multiple lines. And while each return will contain the same types of info, it might be in a different order, like:

uniq: 1
ip: 192.168.2.3
serial: ABC123
user: smith
lab: A

uniq: 2
serial: QWERTY321
lab: 12
ip: 192.168.3.4
user: jdoe

Given the above, I'd like to wind up with:
Code:
UNIQ  SERIAL    IP           USER    LAB
1     ABC123    192.168.2.3  jsmith  A
2     QWERTY321 192.168.3.4  jdoe    12

What would be the best tool(s) to use to get moving in that direction?
# 2  
Old 12-17-2012
You could try using awk (nawk if your on Solaris):

Code:
awk -F: '
{ for(i=1;i<NF;i+=2) {
     V[NR,$i]=substr($(i+1),2)
     l=length(V[NR,$i])
     if (length($1)>l) l=length($1)
     if (l>M[$i]) M[$i]=l
  }
}
END {
  for(C in M) printf("%-"(M[C]+1)"s",toupper(C))
  printf "\n"
  for(i=1;i<=NR;i++) {
      for(C in M) printf("%-"M[C]+1"s",V[i,C])
      printf "\n"
  }
}' RS= infile

# 3  
Old 12-19-2012
Thanks for the suggestion. But it made a horrible hash of a sample data set :-(

I've found there's another issue... not every query result necessarily contains all of the attributes. There are items in some results that are not in others.

I'm trying to think of a way to harvest all possible attributes in each set of returns, and then inject each result into an array or something and include a null value for any missing attributes. There is a finite set of possible attributes, and I could pre-build them into my script, but creating that dynamically is a better long-term solution in case attributes are added or removed in the future.
# 4  
Old 12-19-2012
try:
Code:
awk -F" *: *" '
!a[$1]++ && /./ {t[c++]=$1} $1==t[0] {r++} {v[r-1,$1]=$2}
END {
  for (i=0; i<c; i++) {printf("%-15s", toupper(t[i]))} print "";
  for (j=0; j<r; j++) { for (i=0; i<c; i++) {printf("%-15s", v[j,t[i]])} print ""; }
}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Script Output in Outlook Formatting Issue

When i execute the below shell script with 2 different Input files, for one of the data files (datafile1) my email message body in the outlook messes up and every thing comes up in one line. May i please know what i am doing wrong here or how to fix this? The only difference in data files is one is... (1 Reply)
Discussion started by: Ariean
1 Replies

2. Shell Programming and Scripting

SQL Query in Shell Script output formatting

Hi All, #!/bin/ksh call_sql () { sql=$1 sqlplus -s $sqlparam_sieb <<EOF SET ECHO OFF; SET NEWPAGE NONE; SET SQLBL OFF; SET VERIFY OFF; SET LINESIZE 2000; SET... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

3. Shell Programming and Scripting

Formatting Shell script output to Excel

I am facing a problem formatting the output of my shell script in excel. We are directing the output of the script to an excel sheet and need long integer type data printed in Excel as it is (i.e. not in the default scientific notation). Also, leading zeroes(if any) in the output are getting... (4 Replies)
Discussion started by: bornali.p
4 Replies

4. Shell Programming and Scripting

Problem in formatting output of SQL query in excel sheet in shell script

Hi Guys.. Need your help to format the output of my shell script. I am using spool command to take out put in csv file. below is my code. (for example) col USERNAME for a15 col EMAIL for a30 col FULL_NAME for a20 col LAST_LOGIN for a40 col DATE_CREATED for a40 SPOOL 120.csv... (3 Replies)
Discussion started by: Agupte
3 Replies

5. Shell Programming and Scripting

Need help with output formatting

Hey can anyone help me with newline formatting? i bet someone will ask why im rewriting du code, im trying to write a simple script that have slightly different function than du lol.... echo $(du "$dval" | tr ' ' '\n') atm with above code it will printout i've tried sed and awk... cant... (7 Replies)
Discussion started by: Nick1097
7 Replies

6. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

7. Shell Programming and Scripting

Formatting the query output using shell script

Hi All, I'm finding it difficult to format the query output that was returned in a shell script. Actually i have one shell script which does some DB stuff and depending on the result it will do some more tasks. My question here is to format the query output returned by mysql. Intitally my... (5 Replies)
Discussion started by: RSC1985
5 Replies

8. Shell Programming and Scripting

Formatting Sar Output script

Hi, We have 2 scripts below for reporting sar output which are pretty same. In first script i want to add to the program whatever is given in the comments. In second script I want to use while true to run this program every hour and everything that is in comment. Finally I want to club... (0 Replies)
Discussion started by: noorm
0 Replies

9. Shell Programming and Scripting

Formatting the output of a script

Dear readers, I have a script that counts the number of files in particular directories in my home location and displays the output. Now I have 13 directories in my home location. I am getting the output as : Avishek_dir 13 Kunal_dir 17 Shantanu_dir 18 Arup_dir 12 Pranabesh_dir 19 . .... (7 Replies)
Discussion started by: avishek007
7 Replies

10. Shell Programming and Scripting

Formatting Script Output

Hello all, Can somebody please offer some advice. I'm working on a small script to list all packages on the local and remote systems and need it in the following format. Machine Name, Package, Version Here is what i have so far but i can't seem to get the output of the hostname to loop to... (3 Replies)
Discussion started by: liketheshell
3 Replies
Login or Register to Ask a Question