If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Hi I am new to shell programming...pls help me out in solving this problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-30-07, 00:50
unix_87 unix_87 is offline
Registered User
 
Join Date: Sep 2007
Posts: 3
Hi I am new to shell programming...pls help me out in solving this problem

This is the input file
ENRLHDRHN1412334 01123456789055555678901234567890**********!!!!!!!! !!111
AUTHDXPICDDx1703VPRI 02272007
AUTHDXPICDDx1975VSEC 02272007
ENRLHDRHN1412335 011578900989012345678901234567890AAAAAAAAAABBBBBBB BBB111
AUTHDXPICDDx1975VPRI 02282007
ENRLHDRHN1412336 01123456789012345678901234567890**********!!!!!!!! !!111
AUTHDXPICDDx2345VPRI 02242007
AUTHDXPICDDx1305VSEC 02252007
ENRLHDRHN1412337 01123454321512345678901234567890**********!!!!!!!! !!111
AUTHDXPICDDx2431VPRI 02272007
AUTHDXPICDDx2975VSEC 02272007
ENRLHDRHN1412338 01123454321512345678901234567890**********!!!!!!!! !!111
ENRLHDRHN1412339 011234543215


Logic:
Read the Input file line by line.

· Generate a key, whenever you find ENRLHDR in the line, increment the key value and use the same key for AUTHDXP till you get the next ENRLHDR.

· Write the respective lines in the output files ie..line containing ENRLHDR in one file along with the key and line containing AUTHDXP in one file along with the key.

· If an ENRLHDR is encountered without an AUTHDXP record, record the key value as “#”.

· As the output file is a fixed width file, do a Left padding on the key value in order to keep the length as 10. (E.g. 0000000001,0000000012)

Output files:AUTHDXP.txt
0000000001AUTHDXPICDDx1703VPRI 02272007
0000000001AUTHDXPICDDx1975VSEC 02272007
0000000002AUTHDXPICDDx1975VPRI 02282007
0000000003AUTHDXPICDDx2345VPRI 02242007
0000000003AUTHDXPICDDx1305VSEC 02252007
0000000004AUTHDXPICDDx2431VPRI 02272007
0000000004AUTHDXPICDDx2975VSEC 02272007

ENRLHDR.txt
0000000001ENRLHDRHN1412334 01123456789055555678901234567890**********!!!!!!!! !!111
0000000002ENRLHDRHN1412335 011578900989012345678901234567890AAAAAAAAAABBBBBBB BBB111
0000000003ENRLHDRHN1412336 01123456789012345678901234567890**********!!!!!!!! !!111
0000000004ENRLHDRHN1412337 01123454321512345678901234567890**********!!!!!!!! !!111
########ENRLHDRHN1412338 01123454321512345678901234567890**********!!!!!!!! !!111
########ENRLHDRHN1412339 01123454321512345678901234567890**********!!!!!!!! !!111
Reply With Quote
  #2 (permalink)  
Old 09-30-07, 07:27
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
With GNU awk:

Code:
awk '/ENRLHDR/{
				c++
				rec=$0
				getline	
					if($0~/AUTHDXP/)
						printf "%010d%s\n",c,rec>"ENRLHDR.txt"
					else 
						printf "##########%s\n",rec>"ENRLHDR.txt" 
			}
/AUTHDXP/{
			printf "%010d%s\n",c,$0>"AUTHDXP.txt"
		}END{
				if($0~/ENRLHDR/) 
					printf "##########%s\n",$0>"ENRLHDR.txt"
}' inputfile
With nawk:

Code:
nawk '/ENRLHDR/{
				c++
				rec=$0
				getline	
				last=$0
					if($0~/AUTHDXP/)
						printf "%010d%s\n",c,rec>"ENRLHDR.txt"
					else 
						printf "##########%s\n",rec>"ENRLHDR.txt" 
			}
/AUTHDXP/{
			printf "%010d%s\n",c,$0>"AUTHDXP.txt"
		}END{
				if(last~/ENRLHDR/) 
					printf "##########%s\n",last>"ENRLHDR.txt"
}' inputfile
Use nawk or /usr/xpg4/bin/awk on Solaris.
Reply With Quote
  #3 (permalink)  
Old 09-30-07, 11:28
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Another solution :
Code:
$ cat unix_87.sh
awk '
/^ENRLHDR/ {
   enrlhdr[++count] = sprintf("##########%s", $0);
   authdxp = 0;
}
/^AUTHDXP/ {
   if (! authdxp) {
      sub(/^##########/, sprintf("%0.10d", ++key), enrlhdr[count]);
      authdxp = 1 ;
   }
   printf("%0.10d%s\n", key, $0) > "AUTHDXP.txt";
}
END {
   for (i=1; i<=count; i++)
      print enrlhdr[i] > "ENRLHDR.txt";
}
' unix_87.dat
$ cat unix_87.dat
ENRLHDRHN1412334 01123456789055555678901234567890**********!!!!!!!! !!111
AUTHDXPICDDx1703VPRI 02272007
AUTHDXPICDDx1975VSEC 02272007
ENRLHDRHN1412335 011578900989012345678901234567890AAAAAAAAAABBBBBBB BBB111
AUTHDXPICDDx1975VPRI 02282007
ENRLHDRHN1412336 01123456789012345678901234567890**********!!!!!!!! !!111
AUTHDXPICDDx2345VPRI 02242007
AUTHDXPICDDx1305VSEC 02252007
ENRLHDRHN1412337 01123454321512345678901234567890**********!!!!!!!! !!111
AUTHDXPICDDx2431VPRI 02272007
AUTHDXPICDDx2975VSEC 02272007
ENRLHDRHN1412338 01123454321512345678901234567890**********!!!!!!!! !!111
ENRLHDRHN1412339 011234543215
$ unix_87.sh
$ cat ENRLHDR.txt
0000000001ENRLHDRHN1412334 01123456789055555678901234567890**********!!!!!!!! !!111
0000000002ENRLHDRHN1412335 011578900989012345678901234567890AAAAAAAAAABBBBBBB BBB111
0000000003ENRLHDRHN1412336 01123456789012345678901234567890**********!!!!!!!! !!111
0000000004ENRLHDRHN1412337 01123454321512345678901234567890**********!!!!!!!! !!111
##########ENRLHDRHN1412338 01123454321512345678901234567890**********!!!!!!!! !!111
##########ENRLHDRHN1412339 011234543215
$ cat AUTHDXP.txt
0000000001AUTHDXPICDDx1703VPRI 02272007
0000000001AUTHDXPICDDx1975VSEC 02272007
0000000002AUTHDXPICDDx1975VPRI 02282007
0000000003AUTHDXPICDDx2345VPRI 02242007
0000000003AUTHDXPICDDx1305VSEC 02252007
0000000004AUTHDXPICDDx2431VPRI 02272007
0000000004AUTHDXPICDDx2975VSEC 02272007
$
__________________
Jean-Pierre.
Reply With Quote
  #4 (permalink)  
Old 10-01-07, 12:55
unix_87 unix_87 is offline
Registered User
 
Join Date: Sep 2007
Posts: 3
Thank you very much, radoulov and aigles

I wil try it out.....can u please suggest me how to improve my shell programming skills especially in the file manipulation part and so also i am weak in the awk programming....can u suggest me some good books so that i can improve myselves in that
Reply With Quote
  #5 (permalink)  
Old 10-01-07, 13:40
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
Effective AWK Programming by Arnold Robbins(it's free):
http://www.gnu.org/manual/gawk/gawk.pdf

Advanced Bash-Scripting Guide by Mendel Cooper(also free):
http://tldp.org/LDP/abs/abs-guide.pdf

Last edited by radoulov; 10-01-07 at 15:51.
Reply With Quote
  #6 (permalink)  
Old 10-01-07, 13:49
unix_87 unix_87 is offline
Registered User
 
Join Date: Sep 2007
Posts: 3
Thank you very much radoulov and i would try to improve in the coming months.

Thank you very much radoulov and i would try to improve in the coming months.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On