awk print lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print lines in a file
# 1  
Old 08-07-2011
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
# 2  
Old 08-07-2011
Use code tags please. Try this:
Code:
awk '{a[$1]=a[$1]?a[$1]"|"$3:$3}END{for (i in a){print i,a[i]}}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-07-2011
Code:
awk '{x=$1 FS $2;if(!a[x]){a[x]=$1 FS $NF;next}else{$0=a[x] OFS $NF}}1' OFS=\| file

# 4  
Old 08-07-2011
AWK question

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
# 5  
Old 08-07-2011
Code:
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 https://www.unix.com/shell-programmin...-question.html
# 6  
Old 08-09-2011
Quote:
Originally Posted by danmero
Code:
awk '$1=="call"{f++}f{print $4}$1=="total"{f--}' file

[COLOR="#738fbf"][SIZE=1]---------- Post updated at 02:53 PM ---------- Previous update was at 02:15 PM ----------
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines that meet conditions and have value in another file

I am trying to use awk to print lines that satisfy either of the two conditions below: condition 1: $2 equals CNV and the split of $3, the value in red, is greater than or equal to 4. ---- this is a or so I think condition 2: $2 equals CNV and the split of $3, the value in red --- this is a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to print out lines that do not fall between range in file

In the awk below I am trying to print out those lines in file2 that are no between $2 and $3 in file1. Both files are tab-delimeted and I think it's close but currently it is printeing out the matches. The --- are not part of the files they are just to show what lines match or fall into the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Print all lines in a row except the last one with awk

Hi again, is it possible to do the following using awk? input file is 4.465E+17 5.423E+16 1.218E+17 2.600E+16 9.135E+15 1.238E+14 ... 6.238E+14 desired output 4.465E+17 & 5.423E+16 & 1.218E+17 & 2.600E+16 & 9.135E+15 & 1.238E+14 & ... & (3 Replies)
Discussion started by: f_o_555
3 Replies

4. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

5. Shell Programming and Scripting

AWK and print next lines #3 thru #10

I have a output log file, that I want to extract some temperature measurement data. I want to AWK on the words "show chassis environment" in the original file, and extract that entire line, and then the 3rd to 10th lines after the one I AWK'd, into a seperate output file. Here is an example... (3 Replies)
Discussion started by: HikerLT
3 Replies

6. Shell Programming and Scripting

grep/awk to only print lines with two columns in a file

Hey, Need some help for command to print only lines with two columns in a file abc 111 cde 222 fgh ijk 2 klm 12 23 nop want the ouput to be abc 111 cde 222 ijk 2 Thanks a lot in advance!!! (3 Replies)
Discussion started by: leo.maveriick
3 Replies

7. UNIX for Dummies Questions & Answers

Awk print all lines on match?

Ok so I can use awk to match a pattern and print the whole line with print $0. Is there any way to just tell awk to print every line of output when the pattern matches? I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following... (9 Replies)
Discussion started by: MrEddy
9 Replies

8. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

9. Shell Programming and Scripting

How to print specific lines with awk

Hi! How can I print out a specific range of rows, like "cat file | awk NR==5,NR==9", but in the END-statement? I have a small awk-script that finds specific rows in a file and saves the line number in an array, like this: awk ' BEGIN { count=0} /ZZZZ/ { list=NR ... (10 Replies)
Discussion started by: Bugenhagen
10 Replies

10. Shell Programming and Scripting

How to print number of lines with awk ?

Can some body tell me how to print number of line from a particular file, with sed. ? Input file format AAAA BBBB CCCC SDFFF DDDD DDDD Command to print line 2 and 3 ? BBBB CCCC And also please tell me how to assign column sum to variable. I user the following command it... (1 Reply)
Discussion started by: maheshsri
1 Replies
Login or Register to Ask a Question