awk print lines in a file

Dear All,

a.txt
A 1 Z
A 1 ZZ
B 2 Y
B 2 AA

how can i use awk one line to achieve the result:
A Z|ZZ
B Y|AA

Thanks

Use code tags please. Try this:

awk '{a[$1]=a[$1]?a[$1]"|"$3:$3}END{for (i in a){print i,a}}' file
1 Like
awk '{x=$1 FS $2;if(!a[x]){a[x]=$1 FS $NF;next}else{$0=a[x] OFS $NF}}1' OFS=\| file

Hi,
I have a file look likes this :

--->start hir
Trace file: pudwh_ora_9998.trc
Sort options: fchela exeela
*count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
*

CREATE TABLE UAC_37662_ga NOLOGGING AS SELECT DISTINCT
CRM.CI_CUST_INFO.NAP_INT_CUST_NUM FROM CRM.CI_CUST_INFO WHERE
CRM.CI_CUST_INFO.LAST_MIV_STATUS_PAC_DECSR IN ( '' )

call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 0 0.00 0.00 0 0 0 0
Execute 1 18.31 239.91 326 4999384 631 368776
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 1 18.31 239.91 326 4999384 631 368776

Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 94
***************************************************************
SELECT NAP_INT_CUST_NUM
FROM
UAC_37662_ga ORDER BY NAP_INT_CUST_NUM

call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 74 0.51 0.59 0 155 0 368776
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 76 0.51 0.59 0 155 0 368776

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 94
....
---> End hir

I would like to print only the following output :

elapsed 

----------
0.00
239.91
0.00
----------
239.91

elapsed 

----------
0.00
0.00
0.59
----------
0.59

I tryied :

cat filename.txt | awk '{print $4} '
But its not enough since i get all the output under column 4.

Thanks

awk '$1=="call"{f++}f{print $4}$1=="total"{f--}' file

---------- Post updated at 02:53 PM ---------- Previous update was at 02:15 PM ----------

@yoavbe you highjack this thread ! Please read and respect the forum rules !
Your original thread is complex Awk Question

Very creative solution Danmero.

  1. If the first field of line matches "call" , f is initialized to 1 by f++
  2. Evaluates f which is 1, hence true and prints $4
  3. Keeps printing $4 till it matches "total", once $1 == total,it decrements f to 0
  4. Since f is 0, it no longer evaluates to true, hence comes out of the loop