Help with awk or something similar


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk or something similar
# 1  
Old 03-23-2010
Help with awk or something similar

i have a file like this:

Code:
wedd01A1 1
wedd01A2 2
wedd01A3 1
wedd02A2 3
wedd02A3 4
wadd02A1 1
wadd02A2 5
wqdd01A1 3
wsdd01A3 1

i want out like this:

Code:
            A1  A2  A3
wedd01  1    2   1
wedd02  0    3   4
wadd02  1    5   0
wqdd01  3    0   0
wsdd01  0    0   1

kindly help!
# 2  
Old 03-23-2010
Here's one way to do it with Perl -

Code:
$
$
$ cat -n f0
     1  wedd01A1 1
     2  wedd01A2 2
     3  wedd01A3 1
     4  wedd02A2 3
     5  wedd02A3 4
     6  wadd02A1 1
     7  wadd02A2 5
     8  wqdd01A1 3
     9  wsdd01A3 1
$
$ ##
$ perl -lane 'BEGIN {@h=qw(A1 A2 A3)}
>             $y{$F[0]}=$F[1]; @x=unpack("A6A2",$F[0]);
>             if ($x[0] ne $prev){push @a,$x[0]} $prev=$x[0];
>             END {
>               printf("%6s %3s %3s %3s\n","",@h);
>               foreach (@a){
>                 printf("%6s %3s %3s %3s\n",
>                         $_,
>                         defined $y{$_.$h[0]} ? $y{$_.$h[0]} : "0",
>                         defined $y{$_.$h[1]} ? $y{$_.$h[1]} : "0",
>                         defined $y{$_.$h[2]} ? $y{$_.$h[2]} : "0"
>                       )
>               }}' f0
        A1  A2  A3
wedd01   1   2   1
wedd02   0   3   4
wadd02   1   5   0
wqdd01   3   0   0
wsdd01   0   0   1
$
$

Note that I have assumed the header columns "A1", "A2", "A3" to be constant (they are hard-coded in the script).

tyler_durden
# 3  
Old 03-23-2010
working bash script (can probably be optimized) :
Code:
#!/bin/bash
Init() { A1=0; A2=0; A3=0; }
Init
echo -e "\tA1\tA2\tA3"
while read L1 L2
do
    LIN=${L1:0:6}
    COL=${L1:6:2}
    [ -z "$COL" ] && continue
    OLD=${OLD:-$LIN}
    if [ "$LIN" != "$OLD" ]
    then
        echo -e "$OLD\t$A1\t$A2\t$A3"
        Init
        OLD=$LIN
    fi
    eval "$COL=\$L2"
done < infile

# 4  
Old 03-23-2010
Thanks for the answers, is it possible with awk?
# 5  
Old 03-23-2010
Code:
awk '{x=substr($1,1,6);y=substr($1,7,2);a[x FS y]=$2;b[y]=0;c[x]=0}
     END {
            {for (i in b) printf "\t%s", i}; {print ""};
            {for (i in c) { printf i;
                { for (j in b) printf "\t%s",(a[i FS j]=="")?"0":a[i FS j] }
                  print "";}
            }
     }' urfile

        A1      A2      A3
wqdd01  3       0       0
wsdd01  0       0       1
wadd02  1       5       0
wedd01  1       2       1
wedd02  0       3       4


Last edited by rdcwayx; 03-24-2010 at 12:38 AM..
# 6  
Old 03-24-2010
Code:
while(<DATA>){
	chomp;
	if(/^(.*)(A[0-9]+)\s*(.*)$/){
		$hash{$1}->{$2}=$3;
	}
}
print "	A1 A2 A3\n";
foreach my $key(keys %hash){
	my $t1=($hash{$key}->{A1})?$hash{$key}->{A1}:0;
	my $t2=($hash{$key}->{A2})?$hash{$key}->{A2}:0;
	my $t3=($hash{$key}->{A3})?$hash{$key}->{A3}:0;
	print $key," ",$t1," ",$t2," ",$t3,"\n";
}
__DATA__
wedd01A1 1
wedd01A2 2
wedd01A3 1
wedd02A2 3
wedd02A3 4
wadd02A1 1
wadd02A2 5
wqdd01A1 3
wsdd01A3 1

# 7  
Old 03-24-2010
Thanks rdcwayx , i have this error if i try to run it

Code:
awk: syntax error near line 5
awk: illegal statement near line 5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vlookup using awk non similar files

I need to vlookup and check the server not found. Source file 1 server1 server2 server3 server4 server5_root server6_silver server7 server7-test server7-temp Source file 2 server1_bronze (6 Replies)
Discussion started by: ranjancom2000
6 Replies

2. Shell Programming and Scripting

Optimizing for loop with awk or anything similar and portable

The variable COUNTPRO contains: COUNTPRO='Error__posting__message__to__EMR__Queue=0 Error__parsing__ReceiptSummary=0 xinetd__=4327 HTTP__1_1__500___=0 START__=2164 Marshaller__exception__while__converting__to__Receipt__xml=0 MessagePublisher__is__not__configured__correctly=0... (9 Replies)
Discussion started by: SkySmart
9 Replies

3. Shell Programming and Scripting

Add values of similar patterns with awk

so my output is this: session_closed=157 session_opened=151 session_closed=18 session_opened=17 there are two patterns here, but with different values. the two patterns are "session_opened" and "session_closed". i expect there will be many more other patterns. what i want to do is... (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

Create SQL DML insert statements from file using AWK or similar

Hi all. This is my first post on this forum. I've previously found great help in the huge knowledgebase that is here, but this time I have not been able to find a solution to my problem. I have a large text file that looks like this: typedef struct ABC_struct_nbr1_ { char attr1; /*... (0 Replies)
Discussion started by: Yagi Uda
0 Replies

5. Shell Programming and Scripting

awk to search similar strings and arrange in a specified pattern

Hi, I'm running a DB query which returns names of people and writes it in a text file as shown below: Carey, Jim; Cena, John Cena, John Sen, Tim; Burt, Terrence Lock, Jessey; Carey, Jim Norris, Chuck; Lee, Bruce Rock, Dwayne; Lee, Bruce I want to use awk and get all the names... (9 Replies)
Discussion started by: prashu_g
9 Replies

6. Shell Programming and Scripting

Computing the ratio of similar columns in the two files using awk script

Thanks Bartus11 for your help in the following code to compare the two files "t1" and "t2". awk 'NR==FNR{a=1;next}$2 in a{print $2}' t1 t2 First can anyone explain that what is the purpose of assigning a =1? Second, the current script is printing out the matched columns between the... (4 Replies)
Discussion started by: coder83
4 Replies

7. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

8. Shell Programming and Scripting

Help in grep function or similar using awk

I have a list of id; for example: file 1 dfghd dfghe dfgey dfgeu I have another data file that contain this ids as headers; for ex. file2 >dfghd gfdgfddl;klfkld;ld;lgl;dld'l'dv >dfghe gkwhjhsgdjdjdjhjddj >dfgey jdkjfhdjhfdkjhfdkhkdk I wanted to compare file 1 and file 2... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

9. Shell Programming and Scripting

awk, sed or similar log repair help

I have a log file that for some reason, once or two time a month, line foods are missing. This log is generated from vmstat everyminute. I dont know why sometimes it does this. Each line in the log should have 18 columns separated by one or more spaces. Good Log: (not actual log) 1 1... (8 Replies)
Discussion started by: Ikon
8 Replies

10. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies
Login or Register to Ask a Question