Output of shell in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output of shell in array
# 8  
Old 05-27-2011
Code:
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

# 9  
Old 05-28-2011
Quote:
Originally Posted by pradebban
thanks a lot Smilie, 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..
Code:
#!/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 Smilie

Code:
# cat file2
arun|manager|200000|test|test2
pradeep|engineer|10000|test|test2
karthik|teamlead|30000|test|test2

Code:
# ./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

Code:
# ./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!!

Code:
# 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

Last edited by ygemici; 05-28-2011 at 06:02 PM..
# 10  
Old 05-28-2011
Hi

shorter version:
Code:
while IFS='/' read -ra Line
do printf '%8s %12s %12s\n' "${Line[@]}"
done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. UNIX for Dummies Questions & Answers

Formatting Array Output

Hello All, Q1) I have the below code but when the email was sent out all lines are merged and coming out as a single line though my printf statement has newline "\n", How do i avoid that? Q2) In my second IF statement when i introduced the backslash "\" for continuation of a line or command, i... (10 Replies)
Discussion started by: Ariean
10 Replies

4. Shell Programming and Scripting

Output find to array

Hi I'm trying to write a shell script which finds all the .zip files in a given directory then lists them on the screen and prompts the user to select one by entering a number e.g. The available files are: 1. HaveANiceDay.zip 2. LinuxHelp.zip 3. Arrays.zip Please enter the... (4 Replies)
Discussion started by: zX TheRipper Xz
4 Replies

5. Shell Programming and Scripting

Store the output values in array

Hi, How to store the values in array from output result, EG: I have the result like this, ps, google, 1.txt, 1 sam, google, 2.txt, 2 These are the four values followed by comma in two sets. I need to store these values set by set. One set contains four values followed by comma. ... (2 Replies)
Discussion started by: KarthikPS
2 Replies

6. Shell Programming and Scripting

Output file list to array?

Hey, guys, scripting newb here. I'm trying to get a list of all .dmg files in a folder and save the output into an array. My first attempt was ARRAY= ( `ls $REIMAGEPATH | grep \.dmg$` ) However, I understand why that doesn't work now (at least I think I do). But I don't know what the... (5 Replies)
Discussion started by: nextyoyoma
5 Replies

7. Shell Programming and Scripting

Sorting awk array output?

Hi all, I have a script which produces a nice table but I want to sort it on column 3. This is the output line in the script: # Output { FS = ":"; format = "%11s %6s %-16s\n"; prinft "\n" printf ( format, "Size","Count","Who" ) } for (i in... (21 Replies)
Discussion started by: Cowardly
21 Replies

8. Filesystems, Disks and Memory

iostat output vs TPC output (array layer)

Hi Guys, I've been having some arguments with my colleagues about one thing. Always my thought was that as as far as disk performance is concern by looking at the output of the iostat command (AIX) you would be able to identify if you have a hot disk and then by moving some files out that disk... (3 Replies)
Discussion started by: arizah
3 Replies

9. Shell Programming and Scripting

Put output into an array

I'm trying to take the output of an environment that has multiple strings ex. # echo $SSH_CLIENT 192.168.1.1 57039 22 I need that IP... so I can set it to another environment. Thank you (3 Replies)
Discussion started by: adelsin
3 Replies

10. Shell Programming and Scripting

output of an array

Hi gurus, I need to set up an array like this set - A arr 'A', 'B' The output of this array should be like this 'A','B' Right now, I get the output like this 'A B' Can anyone suggest me on how to achieve this. thanks (3 Replies)
Discussion started by: ragha81
3 Replies
Login or Register to Ask a Question