awk truncating first field output?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk truncating first field output?
# 1  
Old 10-20-2014
awk truncating first field output?

Hello,

I'm writing an Awk script to take a command line argument (student's name) and output their relevant student#, name, and marks. For some reason, awk arbitrarily removes the first digit from the student number and doesn't show me the proper output.

Here is my code:
Code:
#! /usr/bin/awk -f

BEGIN{ if (ARGC<2) exit(1);
arg = ARGV[1]
ARGV[1]="classList.txt";
FS=":";
format = "%-4s"}

$2~arg{
for (i=1;i<=NF;i++){
        if (i==1){
                printf "%-10d", $i
        }
        else if (i==2){
                printf "%-18s", $i
        }
        else{
                printf format, $i
        }
}
count++
print "\n"
}

When I search for "on", the output I get is:
Image


Here is the original data:
Image


As you can see some student numbers are missing the first digit.
Keep in mind that I'm still figuring out the formatting (especially the top row).
Any help or insight is appreciated.
# 2  
Old 10-20-2014
Using an image isn't going to help reproducing your issue, please post "copy-pastable" text.
# 3  
Old 10-20-2014
Is it possible you are dealing with a DOS/windows file using \r\n as a line separator? That'd be the only reason I could find just staring at the images.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-20-2014
Sorry,
here is the output and source in text format

Code:
 ./getRecordByName.awk on
 3958231  Claribel Cone     3   25  16  21  6   12  12  25  78

18273475  Shawn Tornton     3   25  16  21  6   12  22  43  106

 8273231  Glenn Ikonomov    10  20  11  18  5   12  24  41  86

92839115  Scott Trouchon    10  30  20  25  10  15  31  29  101

input file:
Code:
Student Number:Name:Lab1:Lab2:Lab3:Lab4:Lab5:Lab6:Exam1:Exam2:Final

18273640:Annette Adams:8:24:19:24:10:12:43:35:98
93840219:Mary Beard:9:30:19:23:10:14:29:39:87
23958552:Antoinette Brown:9:16:18:22:9:12:19:31:79
23958231:Claribel Cone:3:25:16:21:6:12:12:25:78
23958548:Ema Chang:8:22:16:23:9:x:15:28:69
23958664:Heesook Kim:6:20:14:24:7:12:x:39:98
23958115:Larry Liu:7:23:12:24:9:13:43:35:98
23958238:Brad Lee:9:27:18:22:9:12:36:38:102
18273165:Eric Ko:7:23:12:24:9:13:31:42:95
18273239:Richard Ko:9:16:18:22:9:12:28:30:96
18273475:Shawn Tornton:3:25:16:21:6:12:22:43:106
18273427:Omar Maghardi:6:20:14:24:a:12:30:40:100
18273975:Scott Yau:10:30:20:25:10:15:41:45:110
18273231:Glenn Ikonomov:10:20:11:18:5:12:24:41:86
92839115:Scott Trouchon:10:30:20:25:10:15:31:29:101
92839985:Patric Zhou:9:16:18:22:9:12:a:31:98
92839344:Zhanfang Li:10:22:20:22:10:15:24:37:102
92839342:Robert Bloomingdale:9:26:18:22:9:12:25:39:99
92839633:Patricia Garibaldi:7:23:12:24:9:13:32:42:107
92839654:Svetlana Rusova:6:20:14:24:7:12:34:41:109
92839443:Aleksei Titov:3:25:16:21:6:12:38:45:112
92839864:Xiaowei Young:10:20:11:18:5:12:29:37:96
92839428:Guan Wang:9:27:18:22:9:12:29:40:101

Number of Labs:6
Number of Exams:2

Maximum points per Lab
Lab1:10
Lab2:30
Lab3:20
Lab4:25
Lab5:10
Lab6:15

Maximum points per Exam
Exam1:50
Exam2:50

Maximum points on Final
Final:150


Quote:
Originally Posted by RudiC
Is it possible you are dealing with a DOS/windows file using \r\n as a line separator? That'd be the only reason I could find just staring at the images.
I'm logged in to a Linux server using SSH through OSX. Same error occurs when I ssh through Windows to the same server using PuTTY.
# 5  
Old 10-20-2014
You scripts works fine with the sample file provided.

A RudiC already suggested, your input file is somewhat corrupted, please post the result of this command:

Code:
grep Claribel classList.txt | od -c

There are likely unwanted characters there.
This User Gave Thanks to jlliagre For This Post:
# 6  
Old 10-20-2014
Quote:
Originally Posted by jlliagre
You scripts works fine with the sample file provided.

A RudiC already suggested, your input file is somewhat corrupted, please post the result of this command:

Code:
grep Claribel classList.txt | od -c

There are likely unwanted characters there.
No need, the post is a faithful reproduction:

Code:
grep Antoinette classList.txt |od -c
0000000   2   3   9   5   8   5   5   2   :   A   n   t   o   i   n   e
0000020   t   t   e       B   r   o   w   n   :   9   :   1   6   :   1
0000040   8   :   2   2   :   9   :   1   2   :   1   9   :   3   1   :
0000060   7   9  \r  \n
0000064

The file has dos ending characters

Output test adding some delimiter chars:
Code:
./getRecordByName.awk "on"
 >23958231  <<++Claribel Cone     ++3   25  16  21  6   12  12  25  78

>>18273475  <<++Shawn Tornton     ++3   25  16  21  6   12  22  43  106

 >18273231  <<++Glenn Ikonomov    ++10  20  11  18  5   12  24  41  86

>>92839115  <<++Scott Trouchon    ++10  30  20  25  10  15  31  29  101

---------- Post updated at 02:57 PM ---------- Previous update was at 02:51 PM ----------

If you use GNU sed you can remove it by:
Code:
sed -i -e 's/\r//g' classList.txt

This User Gave Thanks to Aia For This Post:
# 7  
Old 10-20-2014
Problem solved. Thanks for all your help!
It turns out there were \r's within the input file which was causing the issue.
I just added a gsub("\r","") to my awk script and the output is showing properly.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to avoid truncating in ps output ?

Hello, This is Solaris 10 (x86) bash-3.2# cat /etc/release Solaris 10 5/09 s10x_u7wos_08 X86 Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms. Assembled 30 March... (5 Replies)
Discussion started by: solaris_1977
5 Replies

2. Shell Programming and Scripting

Using awk to print output based on first field.

Hi Folks, I have one requirement, There is one file, which contains two fields. Based on first field, I need to print an output. Example will be more suitable. Input file like this. abc 5 abc 10 xyz 6 xyz 9 xyz 10 mnp 10 mnp 12 mnp 6 (2 Replies)
Discussion started by: Raza Ali
2 Replies

3. Shell Programming and Scripting

awk calculation wrong field output

The awk below is close but I can't seem to fix it to produce the desired output. Thank you :). current awk with output awk '{c1++; c2+=($2)} END{for (e in c1) print e, c1, c2}' input EFCAB5 2 50 USH2A 2 19 desired... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. UNIX for Dummies Questions & Answers

awk - output field separator

In awk, how do I print all fields with a specified output field separator? I have tried the following, which does not print the output FS: echo a b c d | awk 'BEGIN{OFS = ";"}{print $0}' (3 Replies)
Discussion started by: locoroco
3 Replies

5. Shell Programming and Scripting

nawk is truncating output

Legends, I have 2 files f1 and f2. when i use nawk to compare the difference(subtraction) from 4th column of the file, it truncates the output. can you please help to resolve this. subtraction is (4th col of f1 - 4th col of f2). but it gives only below lines out of 116. I want to print all... (7 Replies)
Discussion started by: sdosanjh
7 Replies

6. Shell Programming and Scripting

awk output field delimiter

Dear All, 1.txt (tab in between each value in a line) a b c a b c a c d you can see below, why with ~ i can output with tab, but = cannot? # awk -F'\t' '$2 ~ /b/' 1 a b c a b c # awk -F'\t' '$2 = "b"' 1 a b c a b c a b d ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

7. Shell Programming and Scripting

Using AWK to format output based on key field

I have file which contains gene lines something like this Transcript Name GO POPTR_0016s06290.1 98654 POPTR_2158s00200.1 11324 POPTR_0004s22390.1 12897 POPTR_0001s11490.1 POPTR_0016s13950.1 14532 POPTR_0015s05840.1 13455 POPTR_0013s06470.1 12344... (6 Replies)
Discussion started by: shen
6 Replies

8. Shell Programming and Scripting

Supressing and replacing the output of a field in Awk

Wondering if anybody can help with changing the output of a field. I'm needing to change the output of a field in this file: User Process ID Time Active Licences Type ChangeAdmin (Phys-agsdev/19353 212), start Wed 1/21 6:30 (linger: 1800) u414013 (Phys-agsdev/19353 1491), start Wed 1/21 12:54... (5 Replies)
Discussion started by: Glyn_Mo
5 Replies

9. Shell Programming and Scripting

how to include field in the output filename of awk

Im using awk and I want the output filename to contain the first field of the input file. Ex. 1 dddd wwwww 1 eeeee wwww 1 wwww eerrrr 2 eeee eeeeee I want the output files to be xxx1 and xxx2 Thank you (4 Replies)
Discussion started by: yahyaaa
4 Replies

10. UNIX for Dummies Questions & Answers

Truncating a number using sed/awk

Hi all, I'm trying to truncate a number like the following: 0001060407013900501048239559900600504083525826350002050354795057 I would like to create an output which puts carriage returns every so many characters, giving an output such as: 0001 060407 0139 0 05 010482395599 0060... (4 Replies)
Discussion started by: michaeltravisuk
4 Replies
Login or Register to Ask a Question