Unexpected result from awk

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Unexpected result from awk
# 1  
Old 04-04-2018
Unexpected result from awk

Hello,

Giving those commands:

Code:
cat > myfile 
1 
2 
3 
^D 
cat myfile | awk '{ s=s+$1 ; print s}'

The output is:

Code:
1
3
6

It seems like this command iterates each time on a different row so $1 is the first field of each row.. But what caused it to refer to each row ?.
What I mean is, how it knows that for the second iteration for example, $1 should be the first field of the second row rather then the first field of the first row again ?

Last edited by Don Cragun; 04-04-2018 at 04:13 PM.. Reason: Add missing CODE tags.
# 2  
Old 04-04-2018
Great question...except that the inventors of awk should be the target audience of this query
# 3  
Old 04-04-2018
Quote:
Originally Posted by uniran
Hello,

Giving those commands:

Code:
cat > myfile 
1 
2 
3 
^D 
cat myfile | awk '{ s=s+$1 ; print s}'

The output is:

Code:
1
3
6

It seems like this command iterates each time on a different row so $1 is the first field of each row.. But what caused it to refer to each row ?.
What I mean is, how it knows that for the second iteration for example, $1 should be the first field of the second row rather then the first field of the first row again ?
Because:
  • that is the way awk is described to work,
  • there are no loops in your code to cause an input line in your file to be evaluated more than once,
  • if awk was designed to only process the 1st line in a file (instead of processing each line in a file), it wouldn't be able to process multi-million line input files,
  • et cetera.
What in the description of awk on your system made you think that awk should only process the first line in a file?
# 4  
Old 04-04-2018
The main code loops over all rows.
Perhaps you want to sum up each row but print only once, at the END?
Code:
awk 'BEGIN { s=0 } { s=s+$1 } END { print s }'

Or, cast an empty s to a zero
Code:
awk '{ s=s+$1 } END { print s+0 }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Duplication | awk | result

Dear forum members, I want the script to count ALA as one (an example in quotes) and return an integer as 1 and not return 5 as an integer as it does now (look bash script). So how can I upgrade my script that it first checks or after finding all instances of ALA checks whether it is the same... (25 Replies)
Discussion started by: Aurimas
25 Replies

2. Shell Programming and Scripting

awk problem - erroring out - unexpected token

can anyone help identify where the issue is here? awk 'BEGIN { c="perl -e 'print scalar(localtime("'${EPOCHTIME}'")), "\n"'"; c|getline; close( c ); print $2" "$3" "$4" "$6; }' bash: syntax error near unexpected token `(' can't seem to figure it out. i tried this: awk 'BEGIN {... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

In awk: unexpected EOF while looking for matching `"'

I am trying to get grep with awk command into variable. But facing error. Could someone pls help. $ cat test_file DEPLOYMENT="abc" # com cluster="bcn" $ grep DEPLOYMENT test_file | awk -F "\"" '{ print $2 }' abc $ a=`echo "grep DEPLOYMENT test_file | awk -F \"\\\"\" '{ print $2 }'"` ;... (6 Replies)
Discussion started by: Manasa Pradeep
6 Replies

4. Shell Programming and Scripting

AWK Looping. How can I get expected result?

Can Anyone help with looping... awk 'FNR==1{i++} {for(k=1; k<=NF; k++) A=$k} # 3 Dimension Array END{ for(i=1;i<=217;i++) # For loop 2nd File 1st and 2nd column x=0;y=0 ... (18 Replies)
Discussion started by: Akshay Hegde
18 Replies

5. UNIX for Dummies Questions & Answers

Weird: unexpected result after piping a sort

Hello, And when you think you know the basics of something, UNIX in this case, something like what I will describe below comes along.... On a Linux system, a "typical" directory with some files. Say 20. I do: > ls | sort > mylisting Now when I: > vi mylisting There is mylisting... (13 Replies)
Discussion started by: stavros
13 Replies

6. Shell Programming and Scripting

shell script - unexpected result

I hv a file --am executing a script which is giving me unexpected results COntents of file: f1 CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX: CMT_AP1:/opt/sybase/syboc125:150:ASE12_5::Y:UX f1.tmp CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX:... (2 Replies)
Discussion started by: rajashekar.y
2 Replies

7. Shell Programming and Scripting

unexpected output within a for loop using awk

Hi all, after hours of playing around with this and scouring the web I decided to ask my fellow UNIX operators as I can't wrap my head around this. First off, I want to parse an input file with tabs (I could pull this off easily with different delimiters) but I was trying to make nicer... (2 Replies)
Discussion started by: Keepcase
2 Replies

8. Shell Programming and Scripting

Unexpected sed result.

I am in the process of writing a script to change the grub password in the grub.conf file. I thought I had it figured out, but am running into an a problem I can't put my finger on. Command I am running when I find that the grub.conf file contains "password --md5". sed... (1 Reply)
Discussion started by: viRaven
1 Replies

9. HP-UX

awk to output cmd result

I was wondering if it was possible to tell awk to print the output of a command in the print. .... | awk '{print $0}' I would like it to print the date right before $0, so something like (this doesn't work though) .... | awk '{print date $0}' (4 Replies)
Discussion started by: IMTheNachoMan
4 Replies

10. Shell Programming and Scripting

unexpected pipeline result with find -exec

Hi All, I probably miss something fundamental here. I want to rename a bunch of files in subdirectories (that might contain white spaces) with names that are related. I thought following could do the job: find . -name *.sh -exec mv {} $(echo {} | sed -e 's/0/1/g') \; Now to be able to... (5 Replies)
Discussion started by: blued
5 Replies
Login or Register to Ask a Question