Put data into tabular form


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Put data into tabular form
# 1  
Old 06-10-2012
Put data into tabular form

Hi
I am having a file which is required to be presented in the under-noted output form. Please suggest.
Input:
Code:
Kapil:
apple 4
banana 6
cherry 0
Manoj:
apple 13
banana
cheery 2

Output:
Code:
         apple banana cherry
Kapil:    4       6      0
Manoj:    13             2

Thanks in advance..
# 2  
Old 06-10-2012
I am not really good at shell scripting, so this might have a few bad coding practices, but here goes:

Code:
#!/bin/ksh

typeset -A apple
typeset -A banana
typeset -A cherry

while read line
do
  if [ $(echo $line | grep -c ':') -ne 0 ]; then
    row=`echo $line | cut -d':' -f1`
  else
    col=`echo $line | awk '{print $1}'`
    val=`echo $line | awk '{print $2}'`
    case $col in
      apple)
        apple[$row]=$val
        ;;  
      banana)
        banana[$row]=$val
        ;;  
      cherry)
        cherry[$row]=$val
        ;;  
    esac
  fi  
done < infile

echo "$x,apple,banana,cherry" | tr ',' '\t'
for user in ${!apple[*]} 
do
echo $user,${apple[$user]},${banana[$user]},${cherry[$user]} | tr ',' '\t'
done

# 3  
Old 06-10-2012
Hi.

The core of this solution is to:

1) create the title line. This could be done automatically, but would obscure the real work

2) collect groups of 4 lines,

3) remove the fruit tags,

4) align the title, names and values

Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate re-arrangement with paste, sed, align.
# align: http://freecode.com/projects/align

# Section 1, setup, pre-solution, $Revision: 1.25 $".
# Infrastructure details, environment, debug commands for forum posts. 
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin" HOME=""
set +o nounset
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { : ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
C=$HOME/bin/context && [ -f $C ] && $C paste sed align
set -o nounset
pe

FILE=${1-data1}

# Display sample of data file, expected output:"
db " Section 1: display of data."
head $FILE expected-output.txt

# Section 2, solution.
pl " Results:"
db " Section 2: solution."
title=$( pe " apple banana cherry" | sed 's/  */\t/g')
paste - - - - < $FILE |
tee t1 |
sed -r 's/apple|banana|cherry//g' > t2
( pe "$title" ; cat t2 ) |
align -st |
tee f1

# Section 3, post-solution, check results, clean-up, etc.
v1=$(wc -l <expected-output.txt)
v2=$(wc -l < f1)
pl " Comparison of $v2 created lines with $v1 lines of desired results:"
db " Section 3: validate generated calculations with desired results."

pl " Comparison with desired results:"
if [ ! -f expected-output.txt -o ! -s expected-output.txt ]
then
  pe " Comparison file \"expected-output.txt\" zero-length or missing."
  exit
fi
if cmp expected-output.txt f1
then
  pe " Succeeded -- files have same content."
else
  pe " Failed -- files not identical -- detailed comparison follows."
  if diff -b expected-output.txt f1
  then
    pe " Succeeded by ignoring whitespace differences."
  fi
fi

exit 0

produciing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
paste (GNU coreutils) 6.10
sed GNU sed version 4.1.5
align 1.7.0

 db,  Section 1: display of data.
==> data1 <==
Kapil:
apple 4
banana 6
cherry 0
Manoj:
apple 13
banana
cherry 2

==> expected-output.txt <==
         apple banana cherry
Kapil:    4       6      0
Manoj:    13             2

-----
 Results:
 db,  Section 2: solution.
	apple	banana	cherry
Kapil:	 4	 6	 0
Manoj:	 13		 2

-----
 Comparison of 3 created lines with 3 lines of desired results:
 db,  Section 3: validate generated calculations with desired results.

-----
 Comparison with desired results:
expected-output.txt f1 differ: char 1, line 1
 Failed -- files not identical -- detailed comparison follows.
 Succeeded by ignoring whitespace differences.

See man pages and the perl script align at the link in the script.

Best wishes ... cheers, drB
# 4  
Old 06-11-2012
Quote:
Originally Posted by jawsnnn
I am not really good at shell scripting, so this might have a few bad coding practices, but here goes:

Code:
#!/bin/ksh

typeset -A apple
typeset -A banana
typeset -A cherry

while read line
do
  if [ $(echo $line | grep -c ':') -ne 0 ]; then
    row=`echo $line | cut -d':' -f1`
  else
    col=`echo $line | awk '{print $1}'`
    val=`echo $line | awk '{print $2}'`
    case $col in
      apple)
        apple[$row]=$val
        ;;  
      banana)
        banana[$row]=$val
        ;;  
      cherry)
        cherry[$row]=$val
        ;;  
    esac
  fi  
done < infile

echo "$x,apple,banana,cherry" | tr ',' '\t'
for user in ${!apple[*]} 
do
echo $user,${apple[$user]},${banana[$user]},${cherry[$user]} | tr ',' '\t'
done

Thanks Buddy..but it does not gave the expected output. It am using the bash shell in CYGWIN_NT-5.1 environment.

Output given is as under :
Code:
frr: line 3: typeset: -A: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
frr: line 4: typeset: -A: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
frr: line 5: typeset: -A: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
        apple   banana  cherry
0       13              0

# 5  
Old 06-11-2012
The first line of this script suggests that it is built for Korn shell. Use it like:

Code:
ksh <script name>

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Required to get out put of log in tabular format in email body

Dears Please support I have out put in text file and look like below fixed inquiries - Click on MAX suffix http://server:port/app User Details http://server:port/app Audit User Detail Action hhttp://server:port/app fixed inquiries - Click on MAX suffix http://server:port/app User Details ... (13 Replies)
Discussion started by: mirwasim
13 Replies

2. Shell Programming and Scripting

Fetching the required data out of a tabular form

Hello Gurus, I am trying to fetch a required number of lines from an output of a command which is in tabular form. Below is the command for reference along with how the result is being shown on UNIX shell. /usr/openv/volmgr/bin/vmquery -b -p 5 The result of the above command is as... (6 Replies)
Discussion started by: Ali Sarwar
6 Replies

3. Shell Programming and Scripting

Dispaying output in tabular form

hi, I have a script which is dispaying the output as below: Column 3:value1 Column 4:value 4 column 8:value 8 column 9:value 9 column 13:value 13 Column 3:value10 Column 4:value 40 column 8:value 80 column 9:value 90 column 13:value 103 However,I need the output in tabular... (5 Replies)
Discussion started by: Vivekit82
5 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

Transpose Data form Different form

HI Guys, I have data in File A.txt RL03 RL03_A_1 RL03_B_1 RL03_C_1 RL03 -119.8 -119.5 -119.5 RL07 RL07_A_1 RL07_B_1 RL07_C_1 RL07 -119.3 -119.5 -119.5 RL15 RL15_A_1 RL15_C_1 RL15 -120.5 -119.4 RL16... (2 Replies)
Discussion started by: asavaliya
2 Replies

7. UNIX for Dummies Questions & Answers

Put data in tabular form..

Dear Friends, I have a file as under : +++ ME 12-06-13 18:16:20 A RED FEW AND ROW1 1MN FEL AS HI FI BV LR TS HR ES MR * 0 13296 0 120 1 15 KS RR 10 0 +++ ME 12-06-13 18:26:20 A RED FEW AND ROW2 1MN FEL AS... (2 Replies)
Discussion started by: vanand420
2 Replies

8. Shell Programming and Scripting

Tabular form in shell script

hi, I need to mention the data in tabular form in shell script. :confused: Input as below: Health check (heading1) CPU/Memory Utilization of pc on server (h2) 1214 of rpc3 is exceeds 0.3 % (data) CPU Utilization is normal for rpc/33 on 2673 CPU Utilization is normal for rpc/33 on... (2 Replies)
Discussion started by: sreelu
2 Replies

9. UNIX for Dummies Questions & Answers

How to read tabular data?

Hello, I have a log file which contains data in tabular format(3 columns(total, posted, rejected) and 2 rows(close, total)) as below. TOTAL POSTED REJECTED CLOSE 3 3 0 TOTAL 3 3 0 I have to search for all Total... (1 Reply)
Discussion started by: akash028
1 Replies

10. UNIX for Dummies Questions & Answers

converting a tabular format data to comma seperated data in KSH

Hi, Could anyone help me in changing a tabular format output to comma seperated file pls in K-sh. Its very urgent. E.g : username empid ------------------------ sri 123 to username,empid sri,123 Thanks, Hema:confused: (2 Replies)
Discussion started by: Hemamalini
2 Replies
Login or Register to Ask a Question