Hi,
i have a file which reads,
arun/manager/200000
pradeep/engineer/10000
karthik/teamlead/30000
.....
i want an output to show,
name role salary
=======================
arun manager 200000
pradeep engineer 10000
and so on..
i want to do this with arrays:(,
kindly help.
thanks,
Pradeep.
Well, boilerplate header from 'echo' and 'tr' the '/' to a tab is a start.
Want justification? I wrote a C fixed pitch formatter, autotab.c, or you can go to Excel or HTML!
KSH, at least, does not have mutli-dimensional arrays built in.
You can write you're own code to implement them....for example, these are functions I wrote:
###################################################################################
# Main line here
###################################################################################
init_array
create_array LIST[3,3,1]
set_array_index_name LIST[2]="Name,Role,Salary"
set_array LIST[1,,]="arun,manager,200000"
set_array LIST[2,,]="pradeep,engineer,10000"
set_array LIST[3,,]="karthik,teamlead,30000"
print_array LIST
get_array LIST[1,1,1]
Ouput:
Z=1 Name Role Salary
1 arun manager200000
2pradeepengineer 10000
3karthikteamlead 30000
arun
Sounds cool, purdym. But it does no one any good unless you post the code for the functions you wrote.
I've thought about releasing the code, I spent a lot of time developing it. I can't guarantee its bug free. Perhaps someone has time see if it works on their system.
maybe try something this
# cat file
arun/manager/200000
pradeep/engineer/10000
karthik/teamlead/30000
# ./justdoit file
name role salary
=================================
arun manager 200000
pradeep engineer 10000
karthik teamlead 30000
#!/bin/bash
F="==========="
echo -e " name\trole\t salary\n $F$F$F"
while read -r l ; do
array=($(echo $l|sed 's/[/]/ /g' )) ;c=${#array[@]}
for((i=0;i<$c;i++)) ; do arrpr=(${arrpr[@]} ${array} )
done;array=();printf "%8s %12s %12s\n" ${arrpr[@]};arrpr=();done<$1
regards
ygemici
thanks a lot , it works perfect,
but, can u just go thru the code, and explain a bit ?
regards,
Pradeep
awk 'BEGIN{printf "%-20s%-20s%-20s\n", "name", "role", "salary";
print "================================================"}
{ printf "%-20s%-20s%-20s\n",$1,$2,$3}' FS=\/ infile
name role salary
================================================
arun manager 200000
pradeep engineer 10000
karthik teamlead 30000
pradebban:
thanks a lot , it works perfect,
but, can u just go thru the code, and explain a bit ?
regards,
Pradeep
firstly we write titles.(echo "")
and read line by line from inputfile.
we take first line (arun/manager/200000) to variable "l".
and we replacement "/" with space by ( sed 's/[/]/ /g' )
now we get "arun manager 200000" and assign values to array.(array=($(echo $l|sed 's/[/]/ /g' ))
now view --> array[0]=arun , array[1]=manager , array[2]=200000 .
i removed for-loop because of this is unnecessary.
write entire directory element.
and next line read and again same process until process last line.
new view..
#!/bin/bash
F="==========="
echo -e " name\trole\t salary\n $F$F$F"
while read -r l ; do
array=($(echo $l|sed 's/[/]/ /g' ));x=${array[@]}
array=();printf "%8s %12s %12s\n" $x ;arrpr=();done<$1
i try to write some more usefull
# cat file2
arun|manager|200000|test|test2
pradeep|engineer|10000|test|test2
karthik|teamlead|30000|test|test2
# ./justdoit FS='|' col1 col2 col3 col4 col5 file2
col1 col2 col3 col4 col5
=================================================================
arun manager 200000 test test2
pradeep engineer 10000 test test2
karthik teamlead 30000 test test2
# ./justdoit FS='/' col1 col2 col3 col4 file2
FS separator is maybe incorrect!!
# ./justdoit FS='|' col1 col2 col3 col4
input file is maybe missing!!
# ./justdoit FS='|' col1 col2 col3 col4 file2
usage : e.g. ./justdoit FS='|' col1 col2 ... col5 inputfile
# ./justdoit FS='|' file2
column names is maybe missing!!
# cat justdoit
#!/bin/bash
F="=============";f=${#};ff=$(eval echo '$'$f)
if [ ! -f $ff ] ; then echo "input file is maybe missing!!" ;exit 1;fi
if [ $# -eq 0 ] ; then echo "usage : e.g. $0 FS="X" col1 col2 ... colx inputfile";exit 1;
elif [ $# -eq 2 ] ; then echo "column names is maybe missing!!" ;exit 1 ;fi
FS=$(echo "$1"|sed "s/FS=\(.\)$/\1/")
[[ $(echo "$FS"|grep -o "[[:punct:]]") ]] || echo "FS is in non-punctuation characters!!"
c=$(sed -n '1s/['$FS']/\n/gp' $ff|sed -n '$=')
if [ -z $c ] ; then echo "FS separator is maybe incorrect!!" ; exit 1; fi
if [ $((c+2)) -ne $f ] ; then echo "usage : e.g. $0 FS='"$FS"' col1 col2 ... col$c inputfile";exit 1;fi
for((i=2;i<=$((c+1));i++)) ; do FF=(${FF[@]}$F) ; X=(${X[@]} "\$$i")
par=(${par[@]} %11s) ; done ;x=${par[@]}
printf "$x\n %s\n" $(eval echo "${X[@]}") " ${FF[@]}"
while read -r l ; do
array=($(echo $l|sed 's/['$FS']/ /g' ))
printf "$x\n" ${arrpr[@]};array=();done<$ff
regards
ygemici
Hi
shorter version:
while IFS='/' read -ra Line
do printf '%8s %12s %12s\n' "${Line[@]}"
done < file