awk array sort


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk array sort
# 1  
Old 04-11-2012
awk array sort

I have a file as below

Input File:
Code:
5/11/2012, dir1, 134
 5/11/2012, dir2, 2341
 5/11/2012, dir3, 2134
 5/11/2012, dir4, 2334
 5/12/2012, dir1, 234
5/12/2012, dir2, 2341
5/12/2012, dir3, 2334
5/13/2012, dir1, 234
5/13/2012, dir2, 2341
5/13/2012, dir3, 2334
5/13/2012, dir4, 21134

Now I am looking for a output using awk

Expected output:
Code:
Date                     dir1                dir2                      dir3             dir4 
5/11/2012           134                 2341                    2134           2334
5/12/2012           234                 2341                    234             -
5/13/2012            234               2341                     2334           21134

Now I trying to write in awk ; taking details in array and trying to make a file but not successful

Problem in here is I need to have two group by one on date another on Directory

Last edited by chakrapani; 04-11-2012 at 05:19 AM..
# 2  
Old 04-11-2012
Could this help you ?
Code:
sort -t"," -k1,2 file1.txt -o file1.txt

OR
Code:
sort -t"," -k1,2 file1.txt > file2.txt; mv file2.txt file1.txt

Code:
awk -F"," 'NR==FNR{a[$2"|"$1]=$3;b[$2]++;next}
{if(FNR==1){printf "Date\t|";for(i in b){printf i"|";c[++k]=i}printf "\n";} for(j=1;j<=k;j++){if(c[j]==$2 && x!=$1) {printf "\n"$1"|"$3"|";x=$1;next}else if(c[j]!=$2 && x!=$1){printf "\n"$1"||";x=$1;}else if (c[j]==$2 && x==$1){printf "|"$3;x=$1;next}else if (c[j]!=$2 && x==$1){x=$1}else {x=$1;}} }END{printf "\n";}' file1.txt file1.txt

# 3  
Old 04-11-2012
Thanks Pravin but I have only one file with the data and other part is output. I updated it with input file and output file.

---------- Post updated at 03:30 AM ---------- Previous update was at 03:18 AM ----------

I am doing something like

Code:
for dir in `awk -F, '{ print $2 }' data | sort | uniq`
 do
 echo -en "$dir"
 for dt in `grep " $dir " data | awk -F, '{ print $1 }' | sort | uniq `;
  do
    val=`grep "$dir " data | grep $dt | awk -F, '{ print $3 }'`
    echo -en "\t\t$val"
  done
  echo -e "."
done

which is not working anyways. Also looking if any one has cleaner solution.

Last edited by chakrapani; 04-11-2012 at 06:42 AM..
# 4  
Old 04-11-2012
Try this out

Code:
#!/bin/bash
echo -e "\t\tDATE\tDIR1\tDIR2\tDIR3\tDIR4\n"
for date in `awk -F, '{ print $1 }' file | sort | uniq`
 do
 printf "%20s" $date
 for dir in `awk -F, '{ print $2 }' file | sort | uniq `;
  do
    val=`grep -E "$date.*$dir" file | awk -F, '{ print $3 }'`
    printf "%8d" $val
  done
  echo -e "\n"
done


Last edited by Franklin52; 04-11-2012 at 09:16 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 04-11-2012
In awk, (pseudo code)
If $2 = dir1 then print $1,\t,$3 without a new line.
Else print \t,$3.
If $2=dir4 then print \n

OK
# 6  
Old 04-17-2012
Hi ,

try this:
Code:
#!/bin/bash

h=0
declare -a str_h
declare -a str_v
res=""
old_str_d=""

for head in $(cat $1 |cut -d"," -f2|sort |uniq )
do
        str_h[$h]=$head
        let h=h+1
done

echo -en "day\t"
for a in "${str_h[@]}"
do
        echo -en "\t\t$a"
done
echo ""

(cat $1;echo 99999999)|sort -k1,3 |while read values
do
        str_date=$(echo $values|cut -d"," -f1|sed 's/ *//g')
        if [ "$str_date" == "99999999" ]
        then
                echo -n $old_str_d
                c=0
                while [ $c -lt  "${#str_v[@]}" ]
                do
                        echo -en "\t\t${str_v[$c]}"
                        str_v[$c]=""
                        let c=$c+1
                done
                echo ""
                exit
        fi

        if [ "$str_date" != "$old_str_d" ]
        then
                echo -n $old_str_d
                c=0
                while [ $c -lt  "${#str_v[@]}" ]
                do
                echo -en "\t\t${str_v[$c]}"
                        str_v[$c]=""
                        let c=$c+1
                done
                echo ""
                str=""
                old_str_d=$str_date
        fi

        if [ "$str_date" == "$old_str_d" ]
        then
                dir=$(echo $values|cut -d"," -f3|sed 's/ *//g')
                c=0
                str_tmp=""
                for a in "${str_h[@]}"
                do
                        if [ "$(echo $values|grep $a)" != ""  ]
                        then
                                str_v[$c]="$dir"
                        fi
                        let c=$c+1
                done
        fi

done

# 7  
Old 04-17-2012
Code:
#!/bin/bash

column -t infile | sort -t"," -k1,2 | \
awk -F'[, ]+' '
BEGIN {print "date dir1 dir2 dir3 dir4"}
      {
         a[$1]=a[$1]OFS$3
      }
END   {
         for(i in a) print i,a[i]
      }' | awk '$5=$5<0?"-":$NF' | column -t

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort multidimensional Array

Hello I have a problem. I create a Multidimensional Array Like this: ENTRY="$kunnum-$host" ENTRY="$host" ENTRY="# $3" for key in "${!ENTRY}"; do ENTRIES=${ENTRY} # INDEX=IP(5) donedeclare -p declare -A ENTRIES=(="unas15533" ="unas" ="# RDP-Terminal 2"... (12 Replies)
Discussion started by: Marti95
12 Replies

2. UNIX for Beginners Questions & Answers

Array length: ls and sort

Hi there, I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird. files_info="$(find $input_dir -name "*_CHR$i.info" | sort )" printf ${#files_info}"\n" #print length #--loop through... (6 Replies)
Discussion started by: genome
6 Replies

3. UNIX for Dummies Questions & Answers

Sort array elements from same field

Hi, input: line1|error_type_a@15 line1|error_type_c@10 line1|error_type_b@5 line2|error_type_f@3 line2|error_type_a@1 I would need to place all the second fields with common first field on the same line, BUT with sorted error position number: line1|error_type_b@5; error_type_c@10;... (5 Replies)
Discussion started by: beca123456
5 Replies

4. Shell Programming and Scripting

Beginner: Count & Sort Using Array's

Hi, I'm new to linux & bash so please forgive my ignorance, just wondering if anyone can help. I have a file (mainfile.txt) with comma deliminated values, like so: $1 $2 $3 613212, 36, 57 613212, 36, 10 613212, 36, 10 677774, 36, 57 619900, 10, 10 i need to split this file... (12 Replies)
Discussion started by: BigTOE
12 Replies

5. Shell Programming and Scripting

sort piping to awk array - help please

Hi I'm just learning programming and need some help. I've taken a data file which has a list of numbers eg: 3 5 32 533 13 2 And I've used sort -n and to sort and then piped it to awk to arrange into an array. #!/bin/sh sort -n data.txt | awk ' { array=$1 } (4 Replies)
Discussion started by: EL_Chemso
4 Replies

6. Shell Programming and Scripting

perl sort array by field

Hi Everyone, Any simple code can simplify the code below, please advice. Thanks # cat 2.pl #!/usr/bin/perl use warnings; use strict; my @aaaaa = <DATA>; my @uids; foreach (@aaaaa) { my @ccccc = split (",", $_); push @uids, $ccccc;... (3 Replies)
Discussion started by: jimmy_y
3 Replies

7. UNIX for Dummies Questions & Answers

Problem with sort-of array in SH

Hello to everyone! I'm really new in shell scripting and I'm experiencing a very odd problem. This is my first post in this forum, hope you can help! I know that declaring arrays in Bourne Shell is impossible. But this is where I start having problems - system administrator did not install... (8 Replies)
Discussion started by: onstock
8 Replies

8. UNIX for Dummies Questions & Answers

Sort after 2. column in array in Perl

Hey How do I sort an array numerically after the second column? My values are integers like 1, 2, 3, 4... and they are not unique, so I can't just reverse my hash and sort by keys. I wanna sort my file/array so that I get the lines with the highest value in the top - that is descending. ... (2 Replies)
Discussion started by: Banni
2 Replies

9. Shell Programming and Scripting

How To Sort Array of Hashes ??

Some one plz help me how to sort an array of hashes ..... for e.g i have an array as @AoH = ( { ques => 10, marks => 32, }, { ques => 32, marks => 22, }, { ques => 2, marks => 41, }, ); now i want to sort this array with increasing value of "ques" ..... plz... (3 Replies)
Discussion started by: coolguyshail
3 Replies

10. Shell Programming and Scripting

sort() array of strings in perl

I have a perl script with an array of clients. @arr = ("The ABC Corp", "AA Corp.", "BB Corp"); If I run @a = sort (@arr); I will get @a = ("AA Corp", "BB Corp", "The ABC Corp"); but I want @a = ("AA Corp, "The ABC Corp", "BB Corp"); How do I sort array without changing... (2 Replies)
Discussion started by: photon
2 Replies
Login or Register to Ask a Question