Sponsored Content
Top Forums Shell Programming and Scripting Dropping Records for unknown reason in awk script Post 302366508 by mkastin on Friday 30th of October 2009 10:40:04 AM
Old 10-30-2009
Dropping Records for unknown reason in awk script

Hi,

I have written the following it is pretty sloppy but I don't see any reason why I should be losing 54 records from a 3.5 million line file after using it.

What I am doing:

I have a 3.5 million record file with about 80,000 records need a correction. They are missing the last data from an append because they didn't have a match. I need to insert defaulted data on these records. My script worked at intended, however I have 54 less output records than input records and I don't know why they were dropped.


Code:
#!/bin/ksh

myFile="${1}"
myOutput="${2}"

awk '{
 match_flag=substr($0,63,2);
 if (NR == 1) insert_data=substr($0,41,22);
 if (match_flag == "  ") {strt=substr($0,1,40); print strt insert_data "\ \ \ \ \ \ \ \ \ \ \ NM\ X";}
else print $0;}' "${myFile}" >> "${myOutput}"

Basically what I am doing is appending a long string a data to any records that are missing a value in position 3064-3065.

Since this file is soo large I can't really provide sample data but I'll attempt to reproduce a short version below.

Code:
INPUT:
0001  Ronald   McDonald  01 H81 0001256 0100111               V VEEEFKFS SP X
0002  Elmo     St. Elmo  02 H82 0089621  001  10 11 01 1      0000WWDFCWWSP X
0003  Cookie   Monster   01 H81 0887141    1  .  0   0  .  1  BBB000 QWFJSP X
0004  Tfer     Harris    04 H84 0985512 0000000000000000000000BBE00122933NM X
0005  Oscar    Grouche   03 H83 0364471                   110.VVMWEWGODWFDA X
0006  Dumb     Name      02 H82 0000233   111 00 1111 00000000F23202233FFDA X
0007  Butter   Face      04 H84 0014666 1111111111111111111111M012291122FDA X
0008  Ford     F150      01 H81 0000001 00111 110 110  0011 ..S1102234SSMSP X
0009  Bar      Foo       03 H83 7741668 0 1 0 1 0 1 0 1 0 1 0 P019441MEWEDA X
0010  ChoCho   Train     04 H84 0014669 1111111111111111111111POWA1224023OB X
0011  Stone    Stone     04 H84 0014566 1111111111111111111111M12301MANWEOB X
0012  Problem  Record    04 H84 0000000 

OUTPUT:
0001  Ronald   McDonald  01 H81 0001256 0100111               V VEEEFKFS SP X
0002  Elmo     St. Elmo  02 H82 0089621  001  10 11 01 1      0000WWDFCWWSP X
0003  Cookie   Monster   01 H81 0887141    1  .  0   0  .  1  BBB000 QWFJSP X
0004  Tfer     Harris    04 H84 0985512 0000000000000000000000BBE00122933NM X
0005  Oscar    Grouche   03 H83 0364471                   110.VVMWEWGODWFDA X
0006  Dumb     Name      02 H82 0000233   111 00 1111 00000000F23202233FFDA X
0007  Butter   Face      04 H84 0014666 1111111111111111111111M012291122FDA X
0008  Ford     F150      01 H81 0000001 00111 110 110  0011 ..S1102234SSMSP X
0009  Bar      Foo       03 H83 7741668 0 1 0 1 0 1 0 1 0 1 0 P019441MEWEDA X
0010  ChoCho   Train     04 H84 0014669 1111111111111111111111POWA1224023OB X
0011  Stone    Stone     04 H84 0014566 1111111111111111111111M12301MANWEOB X
0012  Problem  Record    04 H84 0000000 0000000000000000000000           NM X

File is fixed length no delimiters.

Last edited by mkastin; 11-02-2009 at 11:28 AM.. Reason: Fixing all examples and adujusting code to fit examples properly.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

why my script stopped- any reason(urgent please)

Hi Friends, iam running some scripts, which are all off suddenly stopping. Can any one tell me the reason why it happend. Is there any reason to stop the scripts. Thanks Krishna. (1 Reply)
Discussion started by: krishna9
1 Replies

2. Shell Programming and Scripting

awk out an unknown column ?

I need a solution to awk out an unknown column. I am unable to say '{print $x}' because the location changes. I would like to find a perl or awk solution to this. I do not know either very well but am trying to delve deeper into both. I am looking for the version of pkg8 in this example. Please... (17 Replies)
Discussion started by: i9300
17 Replies

3. Shell Programming and Scripting

Finding out how long a script runs for and exit reason.

I am running a daemon program that sends texts via a connected mobile phone. I run this daemon via CLI, and it loops a few commands (checking to see if there are any new texts). It runs perfectly, the problem is, when I leave this to run on my Ubuntu Desktop, and come back to it hours later it... (2 Replies)
Discussion started by: daydreamer
2 Replies

4. Linux

Need awk script for removing duplicate records

I have huge txt file having millions of trade data. For e.g Trade.txt (first 8 lines in the file is header info) COB_DATE,TRADE_ID,SOURCE_SYSTEM_TRADE_ID,TRADE_GROUP_ID, TRADE_TYPE,DEALER_NAME,EXTERNAL_COUNTERPARTY_ID, EXTERNAL_COUNTERPARTY_NAME,DB_COUNTERPARTY_ID,... (6 Replies)
Discussion started by: nmumbarkar
6 Replies

5. Linux

Need awk script for removing duplicate records

I have log file having Traffic line 2011-05-21 15:11:50.356599 TCP (6), length: 52) 10.10.10.1.3020 > 10.10.10.254.50404: 2011-05-21 15:11:50.652739 TCP (6), length: 52) 10.10.10.254.50404 > 10.10.10.1.3020: 2011-05-21 15:11:50.652558 TCP (6), length: 89) 10.10.10.1.3020 >... (1 Reply)
Discussion started by: Rastamed
1 Replies

6. Shell Programming and Scripting

awk script for getting the selected records from a file.

Hello, I have attached one file named file.txt . I have to create a file using the awk script with the records in which 38th position is P and not V . ex it should have 00501 HOLTSVILLE NYP00501 and it should not include 00501 I R S SERVICE CENTER ... (3 Replies)
Discussion started by: sonam273
3 Replies

7. UNIX for Advanced & Expert Users

Unix script seems to be momentarily creating child process for unknown reason

Hi, I have a unix script that basically has a while loop inside which it checks Oracle database for certain records. If it finds the records, it does some processing and then goes back to the while loop. If it doesnot find any matching records, then it sleeps for 30 seconds and then goes back to... (17 Replies)
Discussion started by: waavman
17 Replies

8. Shell Programming and Scripting

Print Unknown Number of User Inputs in awk

Hello, I am new to awk and I am trying to figure out how to print an output based on user input. For example: ubuntu:~/scripts$ steps="step1, step2, step3" ubuntu:~/scripts$ echo $steps step1, step2, step3 I am playing around and I got this pattern that I want: ... (3 Replies)
Discussion started by: tattoostreet
3 Replies

9. Shell Programming and Scripting

awk to update unknown value in file using range of another

I am trying to use awk to update all the unknown values in $6 of file2, if the $4 value in file 2 is within the range of $1 of file1. If there is already a value in $6 other then unknown, it is skipped and the next line is processed. In my awk attempt below the final output is 6 tab-delimited... (6 Replies)
Discussion started by: cmccabe
6 Replies

10. Shell Programming and Scripting

SSH is failing due to unknown reason

Hi, I have setup keys between user1@server1 and user2@server2 however, the ssh is failing. server1 is Linux 3.10.0-514.6.2.el7.x86_64 #1 SMP whereas server2 is 5.10 Generic_150400-40 sun4v sparc sun4v I have checked port 22 to be open and keys to be correct. I also find the permissions... (3 Replies)
Discussion started by: mohtashims
3 Replies
A2P(1)							 Perl Programmers Reference Guide						    A2P(1)

NAME
a2p - Awk to Perl translator SYNOPSIS
a2p [options] [filename] DESCRIPTION
A2p takes an awk script specified on the command line (or from standard input) and produces a comparable perl script on the standard output. OPTIONS Options include: -D<number> sets debugging flags. -F<character> tells a2p that this awk script is always invoked with this -F switch. -n<fieldlist> specifies the names of the input fields if input does not have to be split into an array. If you were translating an awk script that processes the password file, you might say: a2p -7 -nlogin.password.uid.gid.gcos.shell.home Any delimiter can be used to separate the field names. -<number> causes a2p to assume that input will always have that many fields. -o tells a2p to use old awk behavior. The only current differences are: o Old awk always has a line loop, even if there are no line actions, whereas new awk does not. o In old awk, sprintf is extremely greedy about its arguments. For example, given the statement print sprintf(some_args), extra_args; old awk considers extra_args to be arguments to "sprintf"; new awk considers them arguments to "print". "Considerations" A2p cannot do as good a job translating as a human would, but it usually does pretty well. There are some areas where you may want to examine the perl script produced and tweak it some. Here are some of them, in no particular order. There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always integer anyway. This is generally unneeded in perl, but a2p can't tell if the argument is always going to be integer, so it leaves it in. You may wish to remove it. Perl differentiates numeric comparison from string comparison. Awk has one operator for both that decides at run time which comparison to do. A2p does not try to do a complete job of awk emulation at this point. Instead it guesses which one you want. It's almost always right, but it can be spoofed. All such guesses are marked with the comment ""#???"". You should go through and check them. You might want to run at least once with the -w switch to perl, which will warn you if you use == where you should have used eq. Perl does not attempt to emulate the behavior of awk in which nonexistent array elements spring into existence simply by being referenced. If somehow you are relying on this mechanism to create null entries for a subsequent for...in, they won't be there in perl. If a2p makes a split line that assigns to a list of variables that looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the -n option mentioned above. This will let you name the fields throughout the script. If it splits to an array instead, the script is probably referring to the number of fields somewhere. The exit statement in awk doesn't necessarily exit; it goes to the END block if there is one. Awk scripts that do contortions within the END block to bypass the block under such circumstances can be simplified by removing the conditional in the END block and just exiting directly from the perl script. Perl has two kinds of array, numerically-indexed and associative. Perl associative arrays are called "hashes". Awk arrays are usually translated to hashes, but if you happen to know that the index is always going to be numeric you could change the {...} to [...]. Iteration over a hash is done using the keys() function, but iteration over an array is NOT. You might need to modify any loop that iterates over such an array. Awk starts by assuming OFMT has the value %.6g. Perl starts by assuming its equivalent, $#, to have the value %.20g. You'll want to set $# explicitly if you use the default value of OFMT. Near the top of the line loop will be the split operation that is implicit in the awk script. There are times when you can move this down past some conditionals that test the entire record so that the split is not done as often. For aesthetic reasons you may wish to change index variables from being 1-based (awk style) to 0-based (Perl style). Be sure to change all operations the variable is involved in to match. Cute comments that say "# Here is a workaround because awk is dumb" are passed through unmodified. Awk scripts are often embedded in a shell script that pipes stuff into and out of awk. Often the shell script wrapper can be incorporated into the perl script, since perl can start up pipes into and out of itself, and can do other things that awk can't do by itself. Scripts that refer to the special variables RSTART and RLENGTH can often be simplified by referring to the variables $`, $& and $', as long as they are within the scope of the pattern match that sets them. The produced perl script may have subroutines defined to deal with awk's semantics regarding getline and print. Since a2p usually picks correctness over efficiency. it is almost always possible to rewrite such code to be more efficient by discarding the semantic sugar. For efficiency, you may wish to remove the keyword from any return statement that is the last statement executed in a subroutine. A2p catches the most common case, but doesn't analyze embedded blocks for subtler cases. ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n-1]. A loop that tries to iterate over ARGV[0] won't find it. ENVIRONMENT
A2p uses no environment variables. AUTHOR
Larry Wall <larry@wall.org> FILES
SEE ALSO
perl The perl compiler/interpreter s2p sed to perl translator DIAGNOSTICS
BUGS
It would be possible to emulate awk's behavior in selecting string versus numeric operations at run time by inspection of the operands, but it would be gross and inefficient. Besides, a2p almost always guesses right. Storage for the awk syntax tree is currently static, and can run out. perl v5.16.2 2012-08-26 A2P(1)
All times are GMT -4. The time now is 08:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy