awk weird error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk weird error
# 1  
Old 02-19-2015
awk weird error

Here is the awk code i wrote :

Code:
if [ -z "${delimiter}" ]; then
gawk -v field_position="$field_position" -v field_length="$field_length" -v header="$header" -v trailer="$trailer" -v lr="$lr"  '{
if(NR==1&&header=="1")
        {
        next
        }
if(NR==lr&&trailer=="1")
        {
        next
        }
cr_acn_no=substr($0,field_position,field_length)
gsub(/ /,"",cr_acn_no)
print "90002,902,03,99,,"cr_acn_no",,"NR >> "ABC_pci.txt"
k=substr("                    ",1,field_length)
tl=field_position+field_length
z=length($0)-tl+1
printf substr($0,1,field_position-1) k substr($0,tl,z) NR "\n"   >> "ABC.out"
}' $filename
else
gawk -F "${delimiter}" -v field_number="$field_number" -v header="$header" -v trailer="$trailer" -v lr="$lr" -v delimiter="$delimiter" 'BEGIN{OFS=delimiter}
{
if(NR==1&&header=="1")
        {
        next
        }
if(NR==lr&&trailer=="1")
        {
        next
        }
print "90002,902,03,99,,"$field_number",,"NR >> "ABC_pci.txt"
$field_number=""
print $0 delimiter NR >> "ABC.out"
}' $filename

The input file is 485 in length. This is a generic code developed by me. For one particular file at one record it throws a weird error :

Code:
gawk: cmd. line:15: (FILENAME=ZZZ FNR=1173226) fatal: not enough arguments to satisfy format string

The highlighted substr in red is the place where the code is failing.

After spending 4-5 hours of time debugging, i found out that the data in the input file with :

Code:
2014092520140314% ABCFDS LLALDASDL

is causing the failure. When i change the % to a value "9" the code works perfectly for the entire file. This is a weird error. And there are many other % values in the file and the code does not fail any where.

Any one has any idea about this weird error? could it be a awk bug?

Last edited by Scrutinizer; 02-20-2015 at 01:22 AM.. Reason: extra code tags
# 2  
Old 02-19-2015
printf's first argument specifies the format where % is a special character.
Code:
man printf

Quick fix:
Code:
printf "%s\n", substr($0,1,field_position-1) k substr($0,tl,z) NR  >> "ABC.out"

Or use the simple print
Code:
print  substr($0,1,field_position-1) k substr($0,tl,z) NR  >> "ABC.out"


Last edited by MadeInGermany; 02-19-2015 at 11:42 PM..
This User Gave Thanks to MadeInGermany 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

Weird awk problem

Hi, I have a simple awk script: BEGIN{} { $a=$2-$1; print $a } END{if(NR==0){ print "0" } } to which I provide the following input 2.9 14 22.2 27 (4 Replies)
Discussion started by: jamie_123
4 Replies

2. Shell Programming and Scripting

Weird Perl error using db2.

I have having a heck of a time figuring this out so any help is much appreciated. Here is the code where it seems to be dying, I bolded the part it is complaining about: $sth = $dbh->prepare( $query ) or die "error with query\n"; $sth->execute() or die "error executing query ...\n"; while(... (1 Reply)
Discussion started by: savigabi
1 Replies

3. Ubuntu

Need help with a weird sudo error.

I'm fairly new to unix and I was trying to change the name of my host and my user. I changed the name in /hostname using this: gksudo gedit /etc/hostname I then tried changing the name back but it still gave the same error: {env_reset,... (1 Reply)
Discussion started by: H3jck
1 Replies

4. Shell Programming and Scripting

awk weird problem.

awk 'BEGIN{print 1.2.3.4}' 1.20.30.4 Can anyone explain why has extra "0" in the IP address? (3 Replies)
Discussion started by: newoz
3 Replies

5. Shell Programming and Scripting

awk print behavior weird

Hi Experts I am facing a weird issue while using print statement in awk. I have a text file with 3 fields shown below: # cat f1 234,abc,1000 235,efg,2000 236,jih,3000 # When I print the third column alone, I dont face any issue as shown below: # awk '{print $3 }' FS=, f1 1000 2000... (5 Replies)
Discussion started by: guruprasadpr
5 Replies

6. Shell Programming and Scripting

Weird unbalanced quotes error

hi all, i am writing a wrapping script to burn subtitle into video file using transcode. I got this very weird error: code: inFile="movie.avi" subFile="sub.srt" outFile="movie_sub.avi" strExc="-i $inFile -x 'mplayer=-sub $subFile' -w $vidBR -o $outFile -y xvid" echo "transcode $strExc"... (2 Replies)
Discussion started by: tduccuong
2 Replies

7. Shell Programming and Scripting

Weird Interpretation by Awk

Hi, I am not sure what I am doing wrong but I am messing up some logic here. The input file is something like this: *___String Type A Here___String Type B Here___123 *___ ___String Type B Here___123 *___ ___String Type B Here___123 *___ ... (6 Replies)
Discussion started by: Legend986
6 Replies

8. Shell Programming and Scripting

awk, sed, grep...weird style

my desired output is like this: so the thing is, I only need to show every of this part out but the frequency of that data is not fixed, so sometimes it may have 4 lines, or 6 lines or whatever in that file. However, the last line will always have empty space/line below it. (13 Replies)
Discussion started by: finalight
13 Replies

9. UNIX for Advanced & Expert Users

Weird Awk issue

Hi All, a bit of a weird one here. I'm trying to pass a variable into an awk command, and I keep getting an error. I have the line nawk -F"," -v red=$random_variable '{print $red}' $w_dir/$file_name > $w_dir/${column_name} that keeps failing with the error nawk: can't open file {print... (17 Replies)
Discussion started by: Khoomfire
17 Replies

10. UNIX for Advanced & Expert Users

Weird scenario with Awk

Guys, this one is rather odd. I've got an array of numbers, and I'm trying to select only the records with the string "Random" in the 4th column. I'm using awk in this format: awk '{ if (( $6 -eq Random )) print $0 }' For some odd reason, this is simply giving me the list of all the entries... (4 Replies)
Discussion started by: Khoomfire
4 Replies
Login or Register to Ask a Question