generate tabular output from an input text file in unix shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting generate tabular output from an input text file in unix shell scripting
# 1  
Old 04-01-2011
generate tabular output from an input text file in unix shell scripting

Hi,

I have the output (as below) which i want it to be in a table.

For e.g.

space utilization in PSE on path /logs is 0%
space utilization in PSE on path /logs/tuxedo/tuxlsp is 16%
space utilization in PSE on path /ldvarlsp/lsp/log is 37%
space utilization in PSE on path /home is 6%
space utilization in MOCK2 on path /logs is 2%
space utilization in MOCK2 on path /logs/tuxedo/tuxlsp is 85%

I want the output to be in table format say where 1st column is the server name (here PSE/MOCK2), 2nd column is path name, 3rd column is space utilization and so on.

in my code.. name of server, path name and space utlization value is stored in a variable.


Table can be with simple border.

Pleaseeeeeeee help....


Thanks in Advance Smilie

PK
# 2  
Old 04-01-2011
Code:
kent$ cat t.txt
space utilization in PSE on path /logs is 0%
space utilization in PSE on path /logs/tuxedo/tuxlsp is 16%
space utilization in PSE on path /ldvarlsp/lsp/log is 37%
space utilization in PSE on path /home is 6%
space utilization in MOCK2 on path /logs is 2%
space utilization in MOCK2 on path /logs/tuxedo/tuxlsp is 85%

kent$ awk '{print "|"$4"#|"$7"#|"$9"# |"}' t.txt|column -s"#" -t |awk 'BEGIN{line=""}{l=length($0);if(length(line)==0)for(i=1;i<l;i++)line=line"-";print line;print $0}END{print line}'
-------------------------------------
|PSE    |/logs                |0%    |
-------------------------------------
|PSE    |/logs/tuxedo/tuxlsp  |16%   |
-------------------------------------
|PSE    |/ldvarlsp/lsp/log    |37%   |
-------------------------------------
|PSE    |/home                |6%    |
-------------------------------------
|MOCK2  |/logs                |2%    |
-------------------------------------
|MOCK2  |/logs/tuxedo/tuxlsp  |85%   |
-------------------------------------

# 3  
Old 04-01-2011
Code:
 $ ruby -ne 'print $_.gsub(/space.*in\s+|on.*path\s+|is\s+/," | ")' file
 | PSE  | /logs  | 0%
 | PSE  | /logs/tuxedo/tuxlsp  | 16%
 | PSE  | /ldvarlsp/lsp/log  | 37%
 | PSE  | /home  | 6%
 | MOCK2  | /logs  | 2%
 | MOCK2  | /logs/tuxedo/tuxlsp  | 85%

# 4  
Old 04-01-2011
A ksh:
Code:
while read k k k serv  k k path k sp
do
   printf "|%-6s|%25s|%4s|\n" "${serv}" "${path}" "${sp}"
done<infile

# 5  
Old 04-01-2011
a bit verbose, but generic and configurable....
nawk -v f='4 7 9' -f pk.awk myFile
pk.awk:
Code:
BEGIN {
  pad=2
  if (!f) f="4 7 9"
  fn=split(f, fa, FS)
}
{
  for(i=1; i in fa;i++) {
    a[FNR]=(i==1)?$fa[i]:a[FNR] OFS $fa[i]
    w[i]=(w[i]>length($fa[i]))?w[i]:length($fa[i])
  }
}
END {
  for(i=1;i in a;i++) {
    n=split(a[i],tA, OFS)
    for(j=1;j in tA;j++)
       printf("%-*s%c", w[j]+pad, tA[j], (j==n)?ORS:"")
  }
}


Last edited by vgersh99; 04-01-2011 at 10:16 AM..
# 6  
Old 04-03-2011
Something a bit simpler (but not as sophisticated):
Code:
awk '{printf "%-15s|%-32s|%4s\n",$4,$7,$9}' file

# 7  
Old 04-04-2011
Hi All... thanks for all your amazing replies Smilie

@kato - ur code is working absolutely fine Smilie

@sk1418 - i liked ur code for the output format... but when i tried to use it in my script it is giving error for "column -s" command

error diaplayed is :
./log_test4.ksh[23]: column: not found

i also tried to man column but it seems it is not supported here

pht025a2:/home/oper/pk264w/log_script> man column
No manual entry for column.

This is what i used...

awk '{print "|"$4"#|"$7"#|"$9"# |"}' mail_log_status.log | column -s"#" -t | awk 'BEGIN{line=""}{l=length($0);if(length(line)
==0)for(i=1;i<l;i++)line=line"-";print line;print $0}END{print line}'



Can You please halp me again Smilie

Thanks in Advance Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

2. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

3. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

4. Shell Programming and Scripting

Displaying log file pattern output in tabular form output

Hi All, I have result log file which looks like this (below): from the content need to consolidate the result and put it in tabular form 1). Intercomponents Checking Passed: All Server are passed. ====================================================================== 2). OS version Checking... (9 Replies)
Discussion started by: Optimus81
9 Replies

5. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

6. Shell Programming and Scripting

a shell script to generate an excel sheet from a text file..

hi, i have a text file that looks like this! i want to generate an excel sheet out of it, removing all the junk data except the addresses that look like . Arrow Electrical Services Rotating Machinery, Electrical Contracting & Mining Specialists Onsite maintenance, breakdown... (8 Replies)
Discussion started by: vemkiran
8 Replies

7. Shell Programming and Scripting

Unix Shell scripting -How to skip User Standard input section from another script

All, problem Description: For example: I have two shell scripts(executables). let name it as script1 and script2.I'm trying to execute script1 from script2. while executing script2, script1 is asking for manual input(input from keyboard). Now i need to know how I can skip this user input... (3 Replies)
Discussion started by: techie99
3 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

how to use data in unix text file as input to an sql query from shell

Hi, I have data in my text file something like this. adams robert ahmed gibbs I want to use this data line by line as input to an sql query which i run by connecting to an oracle database from shell. If you have code for similar scenario , please ehlp. I want the output of the sql query... (7 Replies)
Discussion started by: rdhanek
7 Replies

10. UNIX for Dummies Questions & Answers

Unix Shell Scripting -- update employees not present in input file

ALL, My shell script takes a employee file as input. I have to identify the list of employees not in the input file and update their status in the database. Approach I followed: by traversing through the input file add all the emplid's to a variable. update the status of employees not in... (2 Replies)
Discussion started by: sailussr
2 Replies
Login or Register to Ask a Question