Format with output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format with output
# 8  
Old 10-19-2014
Single awk can do your job, if order doesn't matter then you can process in END block also, current script reads same input twice and take care of output order.


Input
Code:
akshay@nio:/tmp$ cat infile
K123 X CATA 3
K123 Y CATA 4
K123 Z CATA 2
K123 X CATB 5
K123 Y CATB 2
K123 Z CATB 2
B65 M CATB 3
B65 N CATB 4
B85 X CATA 1
B85 Y CATA 4
TR4 X CATA 3
TR4 Y CATA 2
TR4 Z CATA 2
TR4 X CATB 2
TR4 Y CATB 3
TR4 Z CATB 2
U23 X CATA 3
U23 Y CATA 1
U23 Z CATA 2
U23 P CATA 2

Script
Code:
akshay@nio:/tmp$ cat test.awk 
       NR==1{
		key1 = "CATA"
		key2 = "CATB"
		IFS  = "/"		

		print "ITEM",key1 IFS key2
	    }

     FNR==NR{
		A[$1,$3] = sprintf("%s%s%s",A[$1,$3],$2,$4) 
		next 
	    }

	   (($1,key1) in A || ($1,key2) in A){

		print $1, \
		      (A[$1,key1] = ($1,key1) in A ? A[$1,key1] : "---") IFS \
		      (A[$1,key2] = ($1,key2) in A ? A[$1,key2] : "---")

		delete A[$1,key1] 
		delete A[$1,key2] 
	    }

Output
Code:
akshay@nio:/tmp$ awk -f test.awk infile infile
ITEM CATA/CATB
K123 X3Y4Z2/X5Y2Z2
B65 ---/M3N4
B85 X1Y4/---
TR4 X3Y2Z2/X2Y3Z2
U23 X3Y1Z2P2/---

---------- Post updated at 10:02 PM ---------- Previous update was at 09:44 PM ----------

---edit--

Processing the same in END block is as follows, which reads input file only once.

Code:
akshay@nio:/tmp$ cat test2.awk 
       NR==1{
		key1 = "CATA"
		key2 = "CATB"
		IFS  = "/"		

		print "ITEM",key1 IFS key2
	    }

            {
		A[$1,$3] = sprintf("%s%s%s",A[$1,$3],$2,$4) 
	    }

	 END{
		for(i in A)
		   {
		     split(i,I,SUBSEP)

		     if((I[1],key1) in A || (I[1],key2) in A)
		     {
			print I[1], \
		      	      (A[I[1],key1] = (I[1],key1) in A ? A[I[1],key1] : "---") IFS \
		      	      (A[I[1],key2] = (I[1],key2) in A ? A[I[1],key2] : "---")

			delete A[I[1],key1] 
			delete A[I[1],key2]
		     }

		  } 
	     }

Resulting
Code:
akshay@nio:/tmp$ awk -f test2.awk infile
ITEM CATA/CATB
TR4 X3Y2Z2/X2Y3Z2
B65 ---/M3N4
K123 X3Y4Z2/X5Y2Z2
B85 X1Y4/---
U23 X3Y1Z2P2/---


Last edited by Akshay Hegde; 10-19-2014 at 12:55 PM..
These 2 Users Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format output

Hello Team, I have the following details in a txt file (please note the spaces and tabs) C1 C2 C3 ------------------ --------------- ------------- abc, xyz 2 8 pqr 2 ... (9 Replies)
Discussion started by: H squared
9 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. UNIX for Dummies Questions & Answers

Format Output

Hello Learned People, Good evening. I have absolutely no idea as how to do this & I admit that I do not know anything in unix. I must learn this one of these days. Im a help desk incharge today & someone gave me this file to be formatted & I have a file like this. vi NewFile.txt 232... (8 Replies)
Discussion started by: RTAN
8 Replies

4. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

5. UNIX for Advanced & Expert Users

format df -k output

i am running df -k command on aix machine. i got the output like this. i need to store into file and send that file into microsoft excel.i need to allign properly. Filesystem 512 blocks Free % Used Iused %Iused Mounted on /dev/hd4 262144 126488 52% ... (9 Replies)
Discussion started by: wintercoat
9 Replies

6. Shell Programming and Scripting

Help in Getting specified output format

Hi all, I have input text file of this format objectclass:endeavor pid:12345 postalAddress:379 PROSPECT ST street:STE B l:TORRINGTON st:CT postalCode:067905238 telephoneNumber:9999999999... (2 Replies)
Discussion started by: pintoo
2 Replies

7. Shell Programming and Scripting

How to format output

Dear all, I have written a program which access database and displays the values returned by the query . There are 10 columns to be displayed in one row. But am ending with 5 lines displayed on 1st line and the next 5 in the 2nd line. Ex : 21608 10-20-2007 148 Al's Appliance... (7 Replies)
Discussion started by: uday542
7 Replies

8. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

9. Shell Programming and Scripting

format output

I have a text file that I want a script to parse and grab only the relavent stuff. No idea where to start.. This is the text file.212 0.00000 ? -> (multicast) ETHER Type=2000 (Unknown), size = 344 bytes 0: 0100 0ccc cccc 000b 5f95 0fbe 014a aaaa ........_....J..... (7 Replies)
Discussion started by: Tornado
7 Replies

10. UNIX for Dummies Questions & Answers

ls output format

below is the output is ls -l -rw-r--r-- 1 tonyt staff 3212 Apr 17 1999 file1 -rw-r--r-- 1 tonyt staff 4541 Mar 3 21:08 file2 why the date format is not the same? (6 Replies)
Discussion started by: tonyt
6 Replies
Login or Register to Ask a Question