Doubt in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doubt in awk
# 1  
Old 06-17-2014
Doubt in awk

Hi,

I got a below requirement from this forum, but the solution provided was not clear.

Below is the requirement

Code:
 
Input file
 
A 1 Z
A 1 ZZ
B 2 Y
B 2 AA
 
 
Required output
 
B Y|AA
A Z|ZZ

Solution provided wa
Code:
/usr/xpg4/bin/awk '{a[$1]=a[$1]?a[$1]"|"$3:$3}END{for (i in a){print i,a[i] }}' inputfile

The portion is in red was not understandable.

What I assumption is
Code:
{a[$1]=a[$1]?a[$1]"|"$3:$3}

if a[$1] the first row fields are same for next row then including pipeline and printing field $3 , but I am sure of : (colon)

I tried below to understand the code

Code:
/usr/xpg4/bin/awk -F" " '{a[$1]=a[$1]?a[$1]:$3}END{for (i in a){print i,a[i] }}' inputfile

but getting syntax error.
# 2  
Old 06-17-2014
{a[$1]=a[$1]?a[$1]"|"$3:$3} --> If array a with index $1 contains some element then concatinate existing array element with current line $3 where separator between existing element and new element (column 3 $3 from current line read) is pipe |
else (meaning so far no such index in array a) then a[$1] equal to current line $3
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 06-17-2014
Hi Akshay,

Thanks for your explanation

As you said the pipeline is seprator then it should be in second place (as emntioned below) not in the 4th place, also not sure what colon : plays here

Code:
B |Y AA
A |Z ZZ

# 4  
Old 06-17-2014
Quote:
Originally Posted by stew
Hi Akshay,

Thanks for your explanation

As you said the pipeline is seprator then it should be in second place (as emntioned below) not in the 4th place, also not sure what colon : plays here

Code:
B |Y AA
A |Z ZZ

colon : is like else statement in ternary

Example :
A traditional if-else construct in C, Java and JavaScript is written:
Code:
if (a > b) {
    result = x;
} else {
    result = y;
}

This can be rewritten as the following statement:

Code:
result = a > b ? x : y;

I get result properly see here

Code:
$ awk '{a[$1] = a[$1] ? a[$1] "|" $3: $3}END{for(i in a) print i,a[i]}' <<EOF
A 1 Z
A 1 ZZ
B 2 Y
B 2 AA
EOF

A Z|ZZ
B Y|AA

---------- Post updated at 02:27 PM ---------- Previous update was at 02:17 PM ----------

This is the best and safest way actually I prefer.

Code:
awk '{a[$1] = ( $1 in a ) ? a[$1] "|" $3: $3}END{for(i in a) print i,a[i]}'

# 5  
Old 06-17-2014
Thanks.

I understood a bit now, but how this
Code:
a[$1]

is printing
Code:
A Z

then
Code:
"|" $3: $3

is printing
Code:
|ZZ

my understanding is

Code:
 
a[$1] "|"    $3 :  $3}
|        |    |      |
|        |    |      |
V        V    V      V
A        |    Z     ZZ
B        |    Y     AA
 
But why its printing like below
 
A Z|ZZ
B Y|AA

# 6  
Old 06-17-2014
Hope this will clear your doubts please note we are printing array elements in END block

Code:
When awk reads line number  : 1 Array a[A] = Z           row : A 1 Z                             
When awk reads line number  : 2 Array a[A] = Z|ZZ        row : A 1 ZZ                            
When awk reads line number  : 3 Array a[B] = Y           row : B 2 Y                             
When awk reads line number  : 4 Array a[B] = Y|AA        row : B 2 AA

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doubt on using AWK

DE_CODE|1{AXXANY}1APP_NAME|2{TELCO}2LOC|NY DE_CODE|1{AXXATX}1APP_NAME|2{TELCO}2LOC|TX DE_CODE|1{AXXABT}1APP_NAME|2{TELCO}2LOC|BT DE_CODE|1{AXXANJ}1APP_NAME|2{TELCO}2LOC|NJ i have out put file like below i have to convert it in the format as below. DE_CODE = AXXANY APP_NAME= TELCO LOC = NY... (4 Replies)
Discussion started by: mail2sant
4 Replies

2. UNIX for Dummies Questions & Answers

a doubt in awk

instead of writing print command in awk, i saw in some posts that we can simply write a number before we end the awk command and it will print the file. As given below: $awk '{some manipulation; print}' filename $awk '{some manipulation}1' filename I also tried replacing the... (2 Replies)
Discussion started by: PranavEcstasy
2 Replies

3. Shell Programming and Scripting

Awk doubt

I have a file sample.txt with the following contents: the following gives output as awk 'NF{s=$0; print s}' sample.txt but, awk 'NF{s=$0}{print s}' sample.txtgives output as why this difference, can someone explain me? (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. Shell Programming and Scripting

doubt on awk

I have executed the below command: find . -name "Ks*" -type f -exec ls -ltr {} \; | awk '{printf("%ld %s %d %s \n",$5,$6,$7,$8,$9)}' and here is the output: 1282 Oct 7 2004 51590 Jul 10 2006 921 Oct 7 2004 1389 Jun 4 2003 1037 May 19 2004 334 Mar 24 2004 672 Jul 8 2003 977... (6 Replies)
Discussion started by: venkatesht
6 Replies

5. Shell Programming and Scripting

doubt in awk

Hi , I have a file in the below format: 1.txt awk 'BEGIN { printf ("%1s", "man" )} ' awk 'BEGIN { printf ("%9s", "women" )} ' awk 'BEGIN { printf ("%56s", "human")} ' ## ### ## echo "$!" ## awk 'BEGIN { printf ("%1s", "aaa" )} ' awk 'BEGIN { printf ("%19s", "bbb" )} ' ... (4 Replies)
Discussion started by: jisha
4 Replies

6. Shell Programming and Scripting

AWK doubt

Hello people I have a doubt about awk... I´m using it to create a condition where I do not want to use the 0 (zero) value of a certain column. - This is the original file: string,number,date abc,0,20050101 def,1,20060101 ghi,2,20040101 jkl,12,20090101 mno,123,20020101... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

7. UNIX for Dummies Questions & Answers

doubt about awk

i have a file like this: awk.lst smith : sales : 1200 : 2 jones:it:25000 : 2 roger : it : 1500 : 2 ravi | acct | 15000 i have 3 doubts 1) when i say awk -F ":" '$2 ~ /'it'/ {print $0}' awk.lst i am not able to get jones in the ouput , is it because of space issue? 2)how to... (2 Replies)
Discussion started by: soujanya_srk
2 Replies

8. Shell Programming and Scripting

awk doubt

hi how to get the values in two columns (may be 2nd and 5th column) of a file line by line. either i want to get the two fields into different variables and use a for loop to get these values line by line. (3 Replies)
Discussion started by: Pradee
3 Replies

9. Shell Programming and Scripting

doubt in AWK

Hi all, column1 -------- 33 44 55 66 please provide the script using awk command to dispaly output 55. Help apperciated.. thanks, Nirmal (4 Replies)
Discussion started by: abnirmal
4 Replies

10. UNIX for Dummies Questions & Answers

awk doubt

I'm having a file with 5 fields. I want to sort that file according to one field no 3. How shall I do using awk programming. Any input appreciatable. regards, vadivel. (7 Replies)
Discussion started by: vadivel
7 Replies
Login or Register to Ask a Question