The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merging lines using AWK senthil_is Shell Programming and Scripting 6 03-05-2008 12:40 PM
Merging lines - Tuning the script senthil_is Shell Programming and Scripting 3 03-05-2008 03:24 AM
merging two lines in a file thaduka UNIX for Dummies Questions & Answers 6 07-11-2007 09:27 AM
Merging lines into one Foxgard UNIX for Dummies Questions & Answers 8 06-19-2005 06:36 AM
Counting lines and files jorge.ferreira UNIX for Dummies Questions & Answers 6 12-11-2003 11:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-28-2008
Meert's Avatar
Meert Meert is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 8
Merging files with AWK filtering and counting lines

Hi there,

I have a couple of files I need to merge. I can do a simple merge by concatenating them into one larger file.

But then I need to filter the file to get a desired result.

The output looks like this:
Code:
TRNH 0000000010941
ORDH
OADR
OADR
ORDL
ENDT    1116399         000000003 000000001
TRLR 0000000010941 000000003 000000001
TRNH 0000000010942
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
TRLR 0000000010942 000000003 000000001
TRNH 0000000010943
ORDH
OADR
OMSG
ORDL
ORDL
ENDT    1116399         000000004 000000001
TRLR 0000000010943 000000003 000000001
TRNH 0000000010944
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
ORDH                                       
OADR                                       
OADR
ORDL                                       
ORDL                                       
ENDT    1116400         000000004 000000001
TRLR 0000000010944 000000007 000000002
and the filtering should leave the first and the last line with the TRNH and TRLR (giving the last TRLR the same sequence as the first TRNH). The rest of the TRNH and TRLR lines need to be omitted.

Then the final TRLR should represent the amount of ORDH lines and the amount of OADR, OMSG and ORDL lines.

I haven't gotten to deleting the extra TRNH and TRLR lines yet; this is the filter I have so far:

Code:
BEGIN {
#	 define two counters 
	ordh_cnt = 0;
	ordl_total_cnt = 0;
}

#  Start filter 

#  if line start with ORDH add 1 to counters 
$1 == "ORDH" {
	ordh_cnt++;
}

#  if line starts with TRLR, adjust line to reflect new count of ORDH in order
$1 == "TRLR" {
	printf "%s%9.9d%s\n", substr($0, 0, 31), ordh_cnt, substr($0, 39);
#	 line has been printed, next rule 
	next;
}

#  if line start with ORDL add 1 to counters 
$1 == "ORDL" {
	ordl_total_cnt++;
}

#  if line start with OADR add 1 to counters 
$1 == "OADR" {
	ordl_total_cnt++;
}

#  if line start with OMSG add 1 to counters 
$1 == "OMSG" {
	ordl_total_cnt++;
}

#  if line starts with TRLR, adjust line to reflect new total ORDL, OADR and OMSG in complete file 
$1 == "TRLR" {
	printf "%s%9.9d%s\n", substr($0, 0, 19), ordl_total_cnt, substr($0, 29);
#	 line has been printed, next rule 
	next;
}

#  Line has not changed, print normal line 
{
	print $0;
}
Now the amount of ORDH lines is output to my new file, so that seems to work. Yet the amount of OADR, OMSG and ORDL lines isn't corrected in the output.

The final result should look like this:

Code:
TRNH 0000000010941
ORDH
OADR
OADR
ORDL
ENDT    1116399         000000003 000000001
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
ORDH
OADR
OMSG
ORDL
ORDL
ENDT    1116399         000000004 000000001
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
ORDH                                       
OADR                                       
OADR
ORDL                                       
ORDL                                       
ENDT    1116400         000000004 000000001
TRLR 0000000010941 000000017 000000005
Any help would be greatly appreciated
  #2 (permalink)  
Old 01-28-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,859
Code:
awk 'NR == 1 { trnh = $2; print }
!/^TR(NH|LR)/ { 
  if ($1 == "ORDH") 
    ordh ++ 
  if ($1 ~ /^O(ADR|MSG|RDL)/) 
    ordl ++
  print 
} END {
printf "TRLR %s %.9d %.9d\n", trnh, ordl, ordh
}' filename
  #3 (permalink)  
Old 01-28-2008
Meert's Avatar
Meert Meert is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 8
Thank you very much, works like a charm!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0