Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2007
    Posts
    3

    Unanswered: 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

  2. #2
    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.

  3. #3
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

  4. #4
    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

  5. #5
    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 16:51.

  6. #6
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •