|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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 |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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 0produciing: 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
|
|||
|
|||
|
Quote:
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 |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
The first line of this script suggests that it is built for Korn shell. Use it like: Code:
ksh <script name> |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tabular form in shell script | sreelu | Shell Programming and Scripting | 2 | 05-19-2011 02:51 AM |
| How to find max value in a tabular data? | classic | Shell Programming and Scripting | 4 | 08-10-2010 10:34 AM |
| How to read tabular data? | akash028 | UNIX for Dummies Questions & Answers | 1 | 09-29-2009 09:55 AM |
| converting a tabular format data to comma seperated data in KSH | Hemamalini | UNIX for Dummies Questions & Answers | 2 | 06-16-2008 04:37 AM |
| data form | harsh_guru | UNIX for Dummies Questions & Answers | 3 | 02-18-2006 09:26 AM |
|
|